c++ – Clamping place and reflecting velocity of a field crashes when the field is angled

[ad_1]

I’ve a field object, which I created with Box2d. I set its 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 of the display and whether it is, I clamp it and make it bounce in the other 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:

screenshot of working case

That is nice, it really works as anticipated.

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

screenshot of failure case

My program crashes.

How can I repair this, and why precisely is that this occurring?

I considered utilizing an Axis Aligned Bounding Field and by some means work this by way of, I am simply unsure use the AABB to assist me repair this situation.

edit

So I’ve attempt to make a fast edit:

if( aabb.upperBound.y >= boundarySize )
{
    pos.y = boundarySize - boxSize;
    isColliding = true;
}

however this ended up making the field with angle 0, which was bouncing nice till then, it is caught on the prime now. Looks like this if-statement is at all times executing if I exploit the AAAB.

[ad_2]

Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *