碰撞检测代码后从物体弹起的代码是如何工作的?

发布于 2024-11-11 14:15:24 字数 800 浏览 4 评论 0原文

最近我正在读《XNA 4.0 Game Developmeny by Examples》这本书。在其中一章中,这段代码是为了在碰撞检测后弹跳(反射)对象而编写的:

private void BounceAsteroids(Sprite 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;
}

任何人都可以用一些图表或一些易于可视化的示例向我解释这段代码。我无法想象这段代码。如果有人知道任何一本书,我可以在其中学习这种对游戏编程有用的物理知识,那么我也会非常感激。

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(Sprite 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-11-18 14:15:24

这是试图模拟反弹。

标准化和中心计算使斑点在其边缘接触时开始反弹,而不是在它们的中心可能撞击时开始反弹。

然后根据涉及每个小行星质量的一些计算来改变速度。

毫无疑问,反射函数会计算一些角度,以便事情朝着正确的方向发展。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文