Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

[ad_1]

I am utilizing Box2D and rebounding a field every time it hits the underside or prime of the display. I am utilizing world positions right here; the utmost is 20.0f which is the worth of boundarySize:

void Recreation::ClampScreenY( Field* 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 );
    }

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

I have been testing this on a field with angularVelocity = 0 and angle = 0 and it really works. Nonetheless this rebound impact just isn’t working for bins which might be rotating or which have an angle != 0. Why is that this taking place? I am getting out of bounds drawing. There’s clearly one thing flawed with this math.

Angle = 0 works effective and rebounds completely:

Successful bounce

Angle != 0 crashes:

Angled box with corner just touching border

The ClampScreenY perform is known as within the UpdateModel perform I created, which is chargeable for updating the logic of the sport.

After the sport calls the UpdateModel, it calls the ComposeFrame which pulls all objects (I simply have one right here):

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

The decision to box->Draw() crashes when colliding with prime of the window within the state of affairs of the second picture (when angle != 0). Having a look on the 4 vertices’ values, I see that:

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

So y > boundarySize. This should not be taking place for the reason that Clamp perform was presupposed to handle this. What provides?

Possibly after I name SetPosition, it solely updates the field within the subsequent iteration?

[ad_2]

Leave a Reply

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