开发者

How does this bouncing off object after collision detection code work?

Recently I was just reading the book XNA 4.0 Game Developmeny by Example. In one the of the chapters this code is written for bouncing (reflecting) the objects after collision detection:

private void BounceAsteroids(S开发者_如何学Cprite asteroid1, Sprite asteroid2)
{

    Vector2 cOfMass = (asteroid1.Velocity + asteroid2.Velocity) / 2;
    Vector2 normal1 = asteroid2.Center - asteroid1.Center;
    normal1.Normalize();
    Vector2 normal2 = asteroid1.Center - asteroid2.Center;
    normal2.Normalize();

    asteroid1.Velocity -= cOfMass;
    asteroid1.Velocity = Vector2.Reflect(asteroid1.Velocity, normal1);
    asteroid1.Velocity += cOfMass;

    asteroid2.Velocity -= cOfMass;
    asteroid2.Velocity = Vector2.Reflect(asteroid2.Velocity, normal2);
    asteroid2.Velocity += cOfMass;
}

Can anybody explain me this code with some diagram or some example which is easy to visualize. I am not able to visualize this code. If anybody knows any book where I can learn this type of physics stuff useful for game programming then also I'll be very grateful.


this is trying to simulate a bounce.

the Normalize and center calculations are making the blobs start the bounce when their edges touch, as opposed to when their centers might hit.

then the velocity is changed according to some calculation involving masses of each asteroid.

the Reflect function no doubt calculates some angles so things go in the proper direction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜