Ok.
I've been working on a pacman game using C++, and OGRE. Things have been going good, but I have run across a problem with the collision detection of pacman hitting the walls.
Before I explaing, here is some of the source code.
PacpersonObj Class Header
// PacPersonObj.h
// PacPersonObj class definition (represents a PacPersonObj in the game).
#ifndef PacPersonObj_H
#define PacPersonObj_H
#include <Ogre.h> // Ogre class definition
using namespace Ogre; // use the Ogre namespace
const int RADIUS = 5; // the radius of the Ball
class PacPersonObj
{
public:
// constructor
PacPersonObj( SceneManager *sceneManagerPtr );
~PacPersonObj(); // destructor
void movePac( const Vector3 &direction ); // move a PacPersonObj
void addToScene(); // add a PacPersonObj to the scene
void movePac2( Real time ); // move the pacperson across the screen
void setDirection(Vector3);
private:
SceneManager* sceneManagerPtr; // pointer to the SceneManager
SceneNode *nodePtr; // pointer to a SceneNode
String name; // name of the PacPersonObj
//int x;// x-coordinate of the PacPersonObj
// int y;
void reverseHorizontalDirection() ; // change horizontal direction
void reverseVerticalDirection(); // change vertical direction
int speed; // speed of the Ball
Vector3 direction; // direction of the Ball
}; // end of class PacPersonObj
#endif // PacPersonObj_H
PacpersonObj Class .cpp
void PacPersonObj::movePac2( Real time)
{
nodePtr->translate( ( direction * ( speed * time ) ) ); // move pac
Vector3 position = nodePtr->getPosition(); // get pac's new position
// get the positions of the four walls
Vector3 topPosition = sceneManagerPtr->
getSceneNode( "WallTop" )->getPosition();
Vector3 bottomPosition = sceneManagerPtr->
getSceneNode( "WallBottom" )->getPosition();
Vector3 leftPosition = sceneManagerPtr->
getSceneNode( "WallLeft" )->getPosition();
Vector3 rightPosition = sceneManagerPtr->
getSceneNode( "WallRight" )->getPosition();
const int WALL_WIDTH = 20; // the width of the walls
// check if the Pac hit the left side
if ( ( position.x - RADIUS ) <= leftPosition.x + ( WALL_WIDTH / 2 ) )
{
nodePtr->setPosition(0,0,0); // move pac
Pacperson::updateScore( Players::PLAYER2 ); // update the score
} // end if
// check if the Pac hit the right side
if (
( position.x + RADIUS ) >= rightPosition.x - ( WALL_WIDTH / 2 ) )
{
nodePtr->setPosition( (rightPosition.x - ( WALL_WIDTH / 2 ) - RADIUS ),
position.y , position.z );
Pacperson::updateScore( Players::PLAYER1 ); // update the score
} // end else
// check if the Pac hit the bottom wall
else if (
( position.y - RADIUS ) <= bottomPosition.y + ( WALL_WIDTH / 2 ) &&
direction.y < 0 )
{
// place the Pac on the bottom wall
nodePtr->setPosition( position.x,
( bottomPosition.y + ( WALL_WIDTH / 2 ) + RADIUS ), position.z );
} // end else
// check if the Pac hit the top wall
else if (
( position.y + RADIUS ) >= topPosition.y - ( WALL_WIDTH / 2 ) &&
direction.y > 0 )
{
// place the Pac on the top wall
nodePtr->setPosition( position.x,
( topPosition.y - ( WALL_WIDTH / 2 ) - RADIUS ), position.z );
} // end else
} // end function movePac
void PacPersonObj::reverseHorizontalDirection()
{
direction *= Vector3( -1, 1, 1 ); // reverse the horizontal direction
} // end function reverseHorizontalDirection
// reverse the Ball's vertical direction
void PacPersonObj::reverseVerticalDirection()
{
direction *= Vector3( 1, -1, 1 ); // reverse the vertical direction
} // end function reverseVerticalDirection
Class that implements