Home Game Development c++ – How do I make a proper rebounce in a box with an angle different than 0

c++ – How do I make a proper rebounce in a box with an angle different than 0

0
c++ – How do I make a proper rebounce in a box with an angle different than 0

[ad_1]

I’m using box2d and rebouncing a box whenever it hits the bottom or top of the screen, i’m using world positions here, the maximum is 20.0f which is the value of boundarySize:

void Game::ClampScreenY( Box* pBox)
{
    bool isColliding = false;
    auto pos = static_cast<b2Vec2>( pBox->GetPosition() );
    auto aabb = pBox->GetAABB();
    auto angle = pBox->GetAngle();


    if( aabb.upperBound.y >= boundarySize )
    {
        pos.y = boundarySize - boxSize;
        isColliding = true;
    }
    else if( aabb.lowerBound.y <= -boundarySize  )
    {
        pos.y = -boundarySize + boxSize;
        isColliding = true;
    }
    
    if( isColliding && !pBox->GetWasColliding())
    {
        auto vel = static_cast<b2Vec2>( pBox->GetVelocity() );
        vel.y *= -1;
        pBox->SetVelocity( vel );
        pBox->SetPosition( pos );
    }

    // update
    pBox->SetWasColliding( isColliding );
}

I’ve been testing this on a box with angularVelocity = 0 and angle = 0 and it works. However this rebounce effect is not working for boxes that are rotating or that have an angle != 0. Why is this happening? I’m getting out of bounds drawing. There’s obviously something wrong with this math.

Angle = 0 works fine and rebounces perfectly:

enter image description here

Angle != 0 crashes:

enter image description here

The ClampScreenY function is called in the UpdateModel function I created which is resposible for updating the logic of the game.

After the game calls the UpdateModel, it calls the ComposeFrame which draws all objects(i just have one here):

void Game::ComposeFrame()
{
       auto vertices = box->GetVertices(); // for debugging
       box->Draw();
}

The call to box->Draw() crashes when colliding with top of the window in the situation of the image 2 (when the angle != 0). Taking a look at the 4 vertices values I see that

vertice[2] {x=-0.651163101 y=20.7524509 }

so the y > boundarySize, this shouldn’t really be happening since the Clamp function was supposed to take care of this. What gives?

Maybe when I call SetPosition it only updates the box in the next iteration?

[ad_2]

Previous article Neptunia: Sisters VS Sisters Out Next Month On Switch
Next article Arkham Trilogy On Switch Pays Tribute To Beloved Actor
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!