Home Game Development c++ – Box2D manual clamping detection for different angles of a box

c++ – Box2D manual clamping detection for different angles of a box

0
c++ – Box2D manual clamping detection for different angles of a box

[ad_1]

I have a box object, which I created with Box2d. I set its angular velocity and angle to 0, so that it doesn’t rotate for now.

I’m checking every frame to see if the box is hitting the top screen and if it is, I clamp it and make it bounce in the opposite direction.

(also I know I should probably use the dot product here, but this is just a simple example with a linear velocity set to 0 in the x, and 7 in the y, so that the box only goes up or down).

Anyways, I do it like so:

void Game::ClampScreenY( Box* pBox)
{
    bool isColliding = false;
    auto pos = static_cast<b2Vec2>( pBox->GetPosition() );
    auto aabb = pBox->GetAABB();
    
    if( pos.y + boxSize >= boundarySize )
    {
        pos.y = boundarySize - boxSize;
        isColliding = true;
    }
    else if( pos.y - boxSize <= -boundarySize )
    {
        pos.y = -boundarySize + boxSize;
        isColliding = true;
    }

    if( isColliding )
    {
        auto vel = static_cast< b2Vec2 >( pBox->GetVelocity() );
        vel.y *= -1;
        pBox->SetVelocity( vel );
        pBox->SetPosition( pos );
    } 

Now this works if the box angle is 0 like in the following image:

screenshot of working case

This is fine, it works as expected.

But if I change the angle of the box to be different than 0 like so:

screenshot of failure case

My program crashes. How can I fix this, and why exactly is this happening? I thought abound using the Axis Aligned Bounding Box and somehow work this through, I’m just not sure how to use the AABB to help me fix this issue.

[ad_2]

Previous article [PlayStation 5] Smoots Pinball Review
Next article Fortnite’s new map is too fancy for my tastes
Hello there! My name is YoleeTeam, and I am thrilled to welcome you to AmazonianGames.com. As the premier destination for all things related to Amazon Games' universe, we are dedicated to providing you with the most exciting and immersive gaming experiences out there. From captivating visuals to exhilarating gameplay, our website is packed with comprehensive insights, updates, and reviews to keep you ahead of the game. Whether you're a seasoned gamer or new to the scene, I am here to guide you through this virtual frontier. If you have any questions or suggestions, feel free to reach out to me at john@yoleesolutions.com. Embark on your Amazon gaming journey today with AmazonianGames.com and let the adventure begin!