c++ – Box2D handbook clamping detection for various angles of a field

c++ – Box2D handbook clamping detection for various angles of a field

[ad_1]

I’ve a field object , which I created with box2d, I set it is angular velocity and angle to 0, in order that it would not rotate for now.

I am checking each body to see if the field is hitting the highest display and whether it is, I clamp it and make it bounce in the wrong way.

(additionally I do know I ought to in all probability use the dot product right here, however that is only a easy instance with a linear velocity set to 0 within the x, and seven within the y, in order that the field solely goes up or down).

Anyhow, I do it like so:

void Sport::ClampScreenY( Field* 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 field angle is 0 like within the following picture:
enter image description here

that is wonderful, it really works as anticipated.

But when I modify the angle of the field to be totally different than 0 like so:

enter image description here

my program crashes. How can I repair this, and why precisely is that this taking place? I believed abound utilizing the Axis Aligned Bounding Field and someway work this by means of, i am simply undecided the best way to use the aabb to assist me repair this problem.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply