XNA碰撞检测-Vector2.Reflect-帮助计算圆精灵的法线-C# [英] XNA Collision Detection - Vector2.Reflect - Help Calculating the Normal of a Circle Sprite - C#

查看:176
本文介绍了XNA碰撞检测-Vector2.Reflect-帮助计算圆精灵的法线-C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法确定如何在2d空间中计算运动圆的法线.我已经达到了计算对象速度的法线(方向速度)的程度,但这就是我的大学代数思维过热的地方,任何我正在使用的2D Circles中心点,半径,速度和位置.

I'm having trouble wrapping my mind around how to calculate the normal for a moving circle in a 2d space. I've gotten as far as that I'm suppose to calculate the Normal of the Velocity(Directional Speed) of the object, but that's where my college algebra mind over-heats, any I'm working with to 2d Circles that I have the centerpoint, radius, velocity, and position.

最终,我想使用Vector2.Reflect方法从本练习中获得更真实的物理效果.

Ultimately I'm wanting to use the Vector2.Reflect Method to get a bit more realistic physics out of this exercise.

提前谢谢.

添加了一些尝试建议的代码(无济于事),可能会误解建议.在这里,我使用的是篮球和棒球,因此使用的是底脚和篮球.我也有Position和Velocity,它们被添加到位置以创建运动.

Added some code trying out suggestion(with no avail), probably misunderstanding the suggestion. Here I'm using a basketball and a baseball, hence base and basket. I also have Position, and Velocity which is being added to position to create the movement.

if ((Vector2.Distance(baseMid, basketMid)) < baseRadius + basketRadius)
{
    Vector2 baseNorm = basketMid - baseMid;
    baseNorm.Normalize();
    Vector2 basketNorm = baseMid - basketMid;
    basketNorm.Normalize();
    baseVelocity = Vector2.Reflect(baseVelocity, baseNorm);
    basketVelocity = Vector2.Reflect(basketVelocity, basketNorm);
}

basePos.Y += baseVelocity.Y;
basePos.X += baseVelocity.X;
basketPos.Y += basketVelocity.Y;
basketPos.X += basketVelocity.X;
basketMid = new Vector2((basketballTex.Width / 2 + basketPos.X), (basketballTex.Height / 2 + basketPos.Y));
baseMid = new Vector2((baseballTex.Width / 2 + basePos.X), (baseballTex.Height / 2 + basePos.Y));

推荐答案

首先进行反思.如果我没看错代码,则Vector2.Reflect的第二个参数是曲面的法线.水平地板的法线为(0,1),速度为(4,-3)的球击中并以速度(4,3)飞走.是对的吗?如果那不正确,那么我们将不得不更改if语句的主体. (请注意,您可以通过设置basketNorm = -baseNorm来节省一些周期.)

First the reflection. If I'm reading your code right, the second argument to Vector2.Reflect is a normal to a surface. A level floor has a normal of (0,1), and a ball with velocity (4,-3) hits it and flies away with velocity (4,3). Is that right? If that's not right then we'll have to change the body of the if statement. (Note that you can save some cycles by setting basketNorm = -baseNorm.)

现在是物理.就像所写的那样,当两个球碰撞时,每个球都会弹起,就像碰到与两个球体相切的玻璃墙一样,这是不现实的.想象一下打台球:一个快速的红色球击中一个静止的蓝色球死点.红球会反弹并离开蓝球吗?不,蓝色的球会被撞掉,而红色的球会失去大部分速度(在理想情况下都是如此).炮弹和高尔夫球以相同的速度朝相反的方向移动,正面碰撞.他们俩会平等反弹吗?不,炮弹将继续,几乎不会注意到撞击的影响,但是高尔夫球会反转方向并飞得比过去更快.

Now the physics. As written, when the two balls collide, each bounces off as if it had hit a glass wall tangent to both spheres, and that's not realistic. Imagine playing pool: a fast red ball hits a stationary blue ball dead center. Does the red ball rebound and leave the blue ball where it was? No, the blue ball gets knocked away and the red ball loses most of its speed (all, in the perfect case). How about a cannonball and a golf ball, both moving at the same speed but in opposite directions, colliding head-on. Will they both bounce equally? No, the cannonball will continue, barely noticing the impact, but the golf ball will reverse direction and fly away faster than it came.

要理解这些碰撞,您必须了解动量(并且,如果您想要的碰撞不是完全弹性的,例如豆袋碰撞时,您还必须了解能量).一本基本的物理教科书将在较早的章节中介绍.如果您只想模拟这些东西,请使用质量中心框架:

To understand these collisions you have to understand momentum (and if you want collisions that aren't perfectly elastic, like when beanbags collide, you also have to understand energy). A basic physics textbook will cover this in an early chapter. If you just want to be able to simulate these things, use the center-of-mass frame:


Vector2 CMVelocity = (basket.Mass*basket.Velocity + base.Mass*base.Velocity)/(basket.Mass + base.Mass);

baseVelocity -= CMVelocity;
baseVelocity = Vector2.Reflect(baseVelocity, baseNorm);
baseVelocity += CMVelocity;

basketVelocity -= CMVelocity;
basketVelocity = Vector2.Reflect(basketVelocity, basketNorm);
basketVelocity += CMVelocity;

这篇关于XNA碰撞检测-Vector2.Reflect-帮助计算圆精灵的法线-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆