Page 1 of 1

Collision (Very simple)

Posted: Wed Mar 14, 2018 3:52 pm
by jorgerosa
- I´m trying to do a simple collision detection, and avoid to overlap the two nodes (player and brick).
- 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);
    };
 
}; 

Re: Collision (Very simple)

Posted: Wed Mar 14, 2018 10:27 pm
by CuteAlien
Just from code I'm wondering about some stuff:
- Do you want the player to be a axis aligned box in collision? So even when he is rotated the box should be unrotated? Or do you want to rotate the player-box? Or maybe would you even prefer some circle (or sphere?). Btw. better to work with radius than diameters. You kinda want to add half-width of player + half-width of box then it still works if they don't have the same size.
- Why care about playerIsFacing? Instead of "left" you can check for example: if (pos1.X>= pos2.X && pos.X < pos2.X + some_width_for_example_width_player_plus_width_box) to see if the player is to the right of the box but inside it.

Might help if you have a video to see how it looks exactly right now (you can record one for example with OBS).