I still can't get the camera to collide with the object. when I show the objects bounds they are tiny and below the object. I have tried using update object but it's not helping.
// Filename: Camera.cpp
// Written by: R.J. Moody
//Parts derived from....
//First Person Movement Made Easy
//Class Written by Adam Hersh (Heyufool1)
//Collisions Written by Paul Johnston
// Created: 17/02/2010
// Last modified: 28/02/2010
#include "DarkGDK.h"
#include "SC_Collision.h"
#include "Camera.h"
#include "Sounds.h"
// Constructor definition.
Camera::Camera()
{
}
Camera::Camera( int num )
{
//Set camera Id to be created as passed when constructor called.
Camera::cameraId = num;
//Make camera and give it the id of the value held in cameraNumber.
dbMakeCamera( Camera::cameraId );
Camera::MaskID = num; //The object number to use as a movement mask.
Camera::Radius = 10; //The radius of the movement mask (the movement mask is a sphere).
Camera::LevelGroup = 1; //The group that the movement will be checking collisions against.
Camera::Gravity = -0.2f; //The strength of the gravity (negative numbers go down and positive go up).
Camera::Slope = 0.5f; //The slope, don't really have to worry about this one, just leave it as 0.5f.
Camera::MouseSensitivity = 0.3f;
Camera::Ground = 1; //The mouse sensitivity (the higher the number the more sensitive).
Camera::PlayerHeight = 60.0f; //How high in the air the player will be looking from (height from the movement mask).
Camera::PlayerSpeed = 3.0f; //How fast the player moves (not in pixels).
Camera::PlayerJumpHeight = 20.0f; //How high the player jumps (not in pixels).
}
void Camera::setupCamera()
{
//Make the player's mask
dbMakeObjectSphere ( Camera::MaskID, Camera::Radius );
//Position the mask in the air just to avoid it getting stuck in the floor on creation
dbPositionObject ( Camera::MaskID, 0, -40, 0 );
//Hide that mask so it can't be seen during gameplay
dbHideObject ( Camera::MaskID );
//Set the mask up with sparky's so it is ready to be used for collisions
SC_SetupObject ( Camera::MaskID, 0, 1 );
//Position camera.
dbPositionCamera( Camera::cameraId, 0, 0, 0 );
//Set camera to look straight forward when game starts.
//Camera::cameraAngleX = 0;
//Camera::cameraAngleY = 180;
//Turn on automatic camera collision.
//dbAutomaticCameraCollision( Camera::cameraId , 2 , 1 );
//Put mouse in centre of screen to start game.
dbPositionMouse ( dbScreenWidth()/2 , dbScreenHeight()/2 );
//Set cameras filed of view.
dbSetCameraFOV( Camera::cameraId , 90 );
//Load footsteps sounds.
dbLoadSound( "Media/Player/Footsteps.wav" , SOUND_FOOTSTEPS );
dbSetSoundVolume( SOUND_FOOTSTEPS , 90 );
dbLoadSound( "Media/Player/Footsteps2.wav" , SOUND_FOOTSTEPS2 );
dbSetSoundVolume( SOUND_FOOTSTEPS2 , 95 );
}
void Camera::updateCamera()
{
//Get the player's mask's previous X angle
float _PrevXAngle=dbObjectAngleX(Camera::MaskID);
//Rotate the player's mask along the Y axis by movement along the X axis with the Mouse then multiply that by the mouse sensitivity variable
dbYRotateObject(Camera::MaskID,dbObjectAngleY(Camera::MaskID)+dbMouseMoveX()*Camera::MouseSensitivity);
//Rotate the player's mask along the X axis by movement along the Y axis with the Mouse then multiply that by the mouse sensitivity variable
dbXRotateObject(Camera::MaskID,dbObjectAngleX(Camera::MaskID)+dbMouseMoveY()*Camera::MouseSensitivity);
//Limit the the rotation on the X axis to avoid the player from looking in a 360 rotation (on the X axis)
if (dbObjectAngleX(Camera::MaskID)>90 || dbObjectAngleX(Camera::MaskID)<-90)dbXRotateObject(Camera::MaskID,_PrevXAngle);
//Get the old x, y, and z variables from the player's mask. These variables will be used to check for collisions from the old point and the new point
float oldx=dbObjectPositionX(Camera::MaskID);
float oldy=dbObjectPositionY(Camera::MaskID);
float oldz=dbObjectPositionZ(Camera::MaskID);
//Get the player's mask's Y rotation. This will only be used for moving the player forward/backward or side-to-side
float angy=dbObjectAngleY(Camera::MaskID);
//These are the x and z movement vectors of the player
float VX = 0;
float VZ = 0;
//if player is jumping or falling then apply gravity
//if not attempt to keep the player stuck to the floor
if ( Camera::VY == 0 )
{
Camera::VY += 10 * Camera::Gravity;
}
else
{
Camera::VY += Camera::Gravity;
}
//Movement
switch( dbScanCode() )
{
//If 'W' is pressed.
case 17:
//Move Camera forward
VX += dbSin ( angy ) * Camera::PlayerSpeed;
VZ += dbCos ( angy ) * Camera::PlayerSpeed;
if( dbSoundPlaying( SOUND_FOOTSTEPS ) != 1 && dbSoundPlaying( SOUND_FOOTSTEPS2 ) != 1 )
{
//Play footsteps sound.
dbPlaySound( SOUND_FOOTSTEPS2 );
}
break;
//If 'S' is pressed.
case 31:
//Move Camera backward.
VX -= dbSin ( angy ) * ( Camera::PlayerSpeed * 2 / 3 );
VZ -= dbCos ( angy ) * ( Camera::PlayerSpeed * 2 / 3 );
if( dbSoundPlaying( SOUND_FOOTSTEPS ) != 1 && dbSoundPlaying( SOUND_FOOTSTEPS2 ) != 1 )
{
//Play footsteps sound.
dbPlaySound( SOUND_FOOTSTEPS );
}
break;
//If 'A' is pressed.
case 30:
//Move Camera left.
VX -= dbCos ( angy ) * ( Camera::PlayerSpeed * 2 / 3 );
VZ += dbSin ( angy ) * ( Camera::PlayerSpeed * 2 / 3 );
if( dbSoundPlaying( SOUND_FOOTSTEPS ) != 1 && dbSoundPlaying( SOUND_FOOTSTEPS2 ) != 1 )
{
//Play footsteps sound.
dbPlaySound( SOUND_FOOTSTEPS );
}
break;
//If 'D' is pressed.
case 32:
//Move Camera right.
VX += dbCos ( angy ) * ( Camera::PlayerSpeed * 2 / 3 );
VZ -= dbSin ( angy ) * ( Camera::PlayerSpeed * 2 / 3 );
if( dbSoundPlaying( SOUND_FOOTSTEPS ) != 1 && dbSoundPlaying( SOUND_FOOTSTEPS2 ) != 1 )
{
//Play footsteps sound.
dbPlaySound( SOUND_FOOTSTEPS );
}
break;
default:
dbStopSound( SOUND_FOOTSTEPS );
dbStopSound( SOUND_FOOTSTEPS2 );
break;
}
//If in air stop footsteps.
if ( !Camera::Ground )
{
dbStopSound( SOUND_FOOTSTEPS );
dbStopSound( SOUND_FOOTSTEPS2 );
}
//only jump if on ground
if ( Camera::Ground )
{
if ( dbSpaceKey ( ) )
{
Camera::VY += abs ( Camera::Gravity * Camera::PlayerJumpHeight );
}
}
//this would be the player's final position without collision
float x = oldx + VX;
float y = oldy + Camera::VY;
float z = oldz + VZ;
//first handle gravity - seperated from horizontal movement
//to achieve a more realistic effect
//Method: simple - cast a sphere vertically down, if it hits the level then
// position the object there (sticky collision) then move
// on to horizontal movement
// more complex - if we were to only use the simple method the player would be
// allowed to climb almost vertical slopes. Alternative is to
// get the normalY direction to work out how flat the gorund
// below the player is, 0-slope# is flatter, slope#-1 is steeper.
// if it's flat, use sticky collision, if it's steep slide the
// player down the slope. Changing slope# determines how steep the
// player can climb. NOTE: this also effects stairs.
int collide = SC_SphereCastGroup ( Camera::LevelGroup, oldx, oldy, oldz, oldx, oldy + Camera::VY, oldz, Camera::Radius, 0 );
//If there was a collision
if ( collide > 0 )
{
//how flat is this ground
float ny = SC_GetCollisionNormalY ( );
if ( dbAbs ( ny ) > Camera::Slope )
{
//FLAT, stick
oldy = SC_GetStaticCollisionY ( );
}
else
{
//STEEP, slide
x -= oldx;
z -= oldz;
oldx = SC_GetCollisionSlideX ( );
oldy = SC_GetCollisionSlideY ( );
oldz = SC_GetCollisionSlideZ ( );
x += oldx;
z += oldz;
}
//ny < 0 means the player has hit a ceiling rather than a floor
if ( ny > Camera::Slope )
{
//only on ground if standing on flat ground
Camera::Ground = 1;
Camera::VY = 0;
}
else
{
Camera::Ground = 0;
//if player has hit a flat ceiling then stop vy# movement
if ( ny<-Camera::Slope )
{
Camera::VY = Camera::Gravity;
}
}
}
else
{
//nothing below player, not on ground, add vertical speed to player
oldy += Camera::VY;
Camera::Ground = 0;
}
//handle horizontal movement as sliding
//player only collides with group 1 (level) objects and moves freely through others
collide = SC_SphereSlideGroup ( Camera::LevelGroup, oldx, oldy, oldz, x, oldy, z, Camera::Radius, 0 );
if ( collide > 0 )
{
//if hit, reposition player, halt movement vector
x = SC_GetCollisionSlideX ( );
oldy = SC_GetCollisionSlideY ( );
z = SC_GetCollisionSlideZ ( );
VX = 0;
VZ = 0;
}
//position the player
dbPositionObject ( Camera::MaskID, x, oldy, z );
//update the player's position in sparky's
SC_UpdateObject ( Camera::MaskID );
//position and rotate the camera
dbPositionCamera ( Camera::cameraId , dbObjectPositionX( MaskID ), dbObjectPositionY ( Camera::MaskID ) + PlayerHeight, dbObjectPositionZ ( Camera::MaskID ) );
dbRotateCamera (Camera::cameraId , dbObjectAngleX ( Camera::MaskID ), dbObjectAngleY ( Camera::MaskID ), dbObjectAngleZ ( Camera::MaskID ) );
/*
switch( dbScanCode() )
{
//If 'W' is pressed.
case 17:
//Move Camera forward.
dbMoveCamera( Camera::cameraId , 3 );
if( dbSoundPlaying( SOUND_FOOTSTEPS ) != 1 && dbSoundPlaying( SOUND_FOOTSTEPS2 ) != 1 )
{
//Play footsteps sound.
dbPlaySound( SOUND_FOOTSTEPS2 );
}
break;
//If 'S' is pressed.
case 31:
//Move Camera backward.
dbMoveCamera( Camera::cameraId , -2 );
if( dbSoundPlaying( SOUND_FOOTSTEPS ) != 1 && dbSoundPlaying( SOUND_FOOTSTEPS2 ) != 1 )
{
//Play footsteps sound.
dbPlaySound( SOUND_FOOTSTEPS );
}
break;
//If 'A' is pressed.
case 30:
//Move Camera right.
dbYRotateCamera ( Camera::cameraId , dbCameraAngleY( Camera::cameraId ) - 90 );
dbMoveCamera( Camera::cameraId , 2 );
dbYRotateCamera ( Camera::cameraId , dbCameraAngleY( Camera::cameraId ) + 90 );
if( dbSoundPlaying( SOUND_FOOTSTEPS ) != 1 && dbSoundPlaying( SOUND_FOOTSTEPS2 ) != 1 )
{
//Play footsteps sound.
dbPlaySound( SOUND_FOOTSTEPS );
}
break;
//If 'D' is pressed.
case 32:
//Move Camera left.
dbYRotateCamera ( Camera::cameraId , dbCameraAngleY( Camera::cameraId ) + 90 );
dbMoveCamera( Camera::cameraId , 2 );
dbYRotateCamera ( Camera::cameraId , dbCameraAngleY( Camera::cameraId ) - 90 );
if( dbSoundPlaying( SOUND_FOOTSTEPS ) != 1 && dbSoundPlaying( SOUND_FOOTSTEPS2 ) != 1 )
{
//Play footsteps sound.
dbPlaySound( SOUND_FOOTSTEPS );
}
break;
default:
dbStopSound( SOUND_FOOTSTEPS );
dbStopSound( SOUND_FOOTSTEPS2 );
break;
}
//If camera moved up into the air (ie. trying to fly) this will place it back on the floor.
dbPositionCamera( Camera::cameraId , dbCameraPositionX(Camera::cameraId) , 0 , dbCameraPositionZ(Camera::cameraId) );
//Set the max view to prevent camera flippin over.
if (Camera::cameraAngleX > 90 && Camera::cameraAngleX < 180)
{
Camera::cameraAngleX = 90;
}
if (Camera::cameraAngleX < 270 && Camera::cameraAngleX > 180 )
{
Camera::cameraAngleX = 270;
}
//Update variables camera angles.
Camera::cameraAngleX = dbWrapValue ( Camera::cameraAngleX + dbMouseMoveY ( ) * 0.3f );
Camera::cameraAngleY = dbWrapValue ( Camera::cameraAngleY + dbMouseMoveX ( ) * 0.3f );
//Rotate the camera to the new angles.
dbYRotateCamera ( Camera::cameraId , Camera::cameraAngleY );
dbXRotateCamera ( Camera::cameraId , Camera::cameraAngleX );
*/
}
//Load objects etc... (This needs tidying up.)
dbLoadObject( "Media/world.x" , 10 );
dbRotateObject( 10 , 90 , 0 , 0 );
dbPositionObject( 10 , 0 , 0 , 0 );
SC_SetupComplexObject ( 10, 1, 2 );
//dbSetObjectCollisionToPolygons(10);
//Load ammocrate
dbLoadObject( "Media/ammo crate/ammocrate.x" , 50 );
dbScaleObject( 50 , 2000 , 2000 , 2000 );
dbRotateObject( 50 , 180 , 0 , 0 );
dbPositionObject( 50 , 80 , -30 , 80 );
SC_SetupObject(50, 1, 0);
SC_DrawObjectBounds(50);
SC_UpdateObject(50);
//dbSetObjectCollisionToBoxes( 50 );
I hope the code has copied properly. I can't check it and edit it because my posts have to be approved.