- After the player hits a brick, then the code should place him "back", according to the position that he was coming.
I´m working only with X,Y axis (2D style). The next code works ok when the string is set correctly, but not "rock solid" when the player is moving in diagonal and collides with a brick, or changes direction while colliding, etc... All results in ugly player movements... So... Can anyone help, making a code more "solid", please?
OR any other way, better, different, simple, complicated, whatever method... Will be welcome! Thanks!
Code: Select all
// Depending on the user ARROW KEYS pressed, the "playerIsFacing" string will hold "Left", "Right", "Up", "Down":
std::string playerIsFacing = "Left";
// The function that avoids player overlap the bricks:
void playerCollided(IAnimatedMeshSceneNode* Player, IAnimatedMeshSceneNode* Brick) {
// Get bounding boxes:
aabbox3df bb1 = Player->getTransformedBoundingBox();
aabbox3df bb2 = Brick->getTransformedBoundingBox();
// Get player dimensions: ("dim1" will hold the dimensions of the player. X = width, Y = height)
vector3df dim1 = bb1.getExtent();
// Get positions:
vector3df pos1 = Player->getAbsolutePosition();
vector3df pos2 = Brick->getAbsolutePosition();
// A collision between the player and a brick was detected ?...
if(bb1.intersectsWithBox(bb2)) { // Yes!...
if(playerIsFacing == "Left" ){ pos1.X = (pos2.X+dim1.X); }; // Places the player to Left position of the brick (its width)
if(playerIsFacing == "Right"){ pos1.X = (pos2.X-dim1.X); }; // Places the player to Right position of the brick (its width)
if(playerIsFacing == "Up" ){ pos1.Y = (pos2.Y-dim1.Y); }; // Places the player to Up position of the brick (its height)
if(playerIsFacing == "Down" ){ pos1.Y = (pos2.Y+dim1.Y); }; // Places the player to Down position of the brick (its height)
// Placing the player:
Player->setPosition(pos1);
};
};