碰撞检测代码后从物体弹起的代码是如何工作的?
最近我正在读《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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是试图模拟反弹。
标准化和中心计算使斑点在其边缘接触时开始反弹,而不是在它们的中心可能撞击时开始反弹。
然后根据涉及每个小行星质量的一些计算来改变速度。
毫无疑问,反射函数会计算一些角度,以便事情朝着正确的方向发展。
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.