C# 物体掉落的弹跳/投掷物理原理
我以为我可以通过在互联网上进行一些搜索来找到这个,但我发现的所有东西都只是球从墙上反弹,以解决诸如乒乓球或其他任意问题之类的问题。我正在制作一款 2D 地牢爬行游戏,当我杀死敌人并且他们掉落战利品时,我希望该物品飞出,就好像它刚刚被扔到空中一样,并随机落在该单位所在的瓷砖上。
我一直在尝试自己解决这个问题,但我无法弄清楚,这可能被问了很多,如果有人可以帮助我,我将非常感激。
按要求编辑:
好吧,当怪物被摧毁时,我会在它所在的图块内选择一个随机位置,让我们将该位置称为 endLoc,将怪物的位置称为 startLoc。然后,我会找到这两个位置之间的中心 x 点,并将 y 减少 20(因为这是我希望项目上升的像素数),所以我们将这个变量称为 launchLoc:
launchLoc = new Vector2(startLoc.X + ((endLoc.X - startLoc.X) / 2), startLoc.Y - 20)
我认为这会产生正确的向量。
所以现在我需要从 startLoc 启动该项目,到 launchLoc,然后让它返回到 endLoc。这就是令人困惑的地方,我不知道如何为此制作一个现实的弧线。最终结果将使该项目像沿着高斯移动一样移动,就好像它被扔到空中一样。
我试图做到这一点,以便在每个间隔期间,速度增加 startLoc 和 launchLoc 之间 X 差异的 120 倍,增加倍数,但我无法让它很好地工作。我不确定这是否是最好的方法。我使用第 120 个,因为 y 值为 20,并且该项目每个间隔向上移动 1 个像素,因此 1 到 20 加起来得到 120,这将使 x 移动不断增加,就像它被抛出一样。
顺便说一句,这是二维的,我希望有帮助。
I thought I'd be able to find this with some searching on the internet but everything I find is just balls bouncing off walls for something like pong or another arbitrary question. I'm making a 2D dungeon crawler game and when I kill enemies and they drop loot I want the item to come flying out as if it had just been thrown in the air and land a random point on the tile the unit was on.
I've been trying to figure this out myself but I can't figure it out, this is probably asked a lot, I'd be really grateful if someone could help me out.
EDIT AS REQUESTED:
Ok well when a monster would be destroyed I would choose a random location within the tile it's in, let's call this location endLoc and the monster's location startLoc. I would then find the center x point between these two locations and decrease the y by 20 ( because that's how many pixels i want the item to go up by), so let's called this variable launchLoc:
launchLoc = new Vector2(startLoc.X + ((endLoc.X - startLoc.X) / 2), startLoc.Y - 20)
I think that produces the right Vector.
So now I would need to launch the item from startLoc, to launchLoc, then have it come back down to endLoc. This is where it gets confusing and I'm not sure how to make a realistic arc for this. The end result would have the item move like it moved along a gaussian, as if it was thrown into the air.
I tried to make it so during each interval, the velocity is increased by 120th, of the X difference, between the startLoc and launchLoc, by an incrementing multiple, but I couldn't get it to work very well. I'm not sure if this was the best way to do. I use 120th because the y value is 20, and the item moves up 1 pixel every interval, so 1 to 20 added up gives 120, this would make the x movement constantly increase, like it was thrown up.
This is in 2D btw, I hope that helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从时间 t0 (v(t0)) 和位置 (<强>p(t0))。可以假设重力产生恒定加速度 (a(t0) = <0, -9.8 m/s2 >,尽管你的值可能不同)直到物体落地。因此,从一个时间片到下一个时间片的运动的一般形式是:
要弄清楚何时停止该运动,您需要弄清楚对象的路径何时与它反弹的表面相交。您必须对所有可以合理预期发生这种情况的表面执行此操作。因此,对于具有直线方程
Ux + Vy + T = 0
的平面,您可以将位置向量分解为其分量,如下所示:Then use the quadratic formula to find
tc
wherep(tc)
satisfies the line equation:Chose the branch such that tc > t0. From there it's simple to figure out where the object will collide with the surface. You can then update the velocity vector and position vector based on the behavior of the bounce. If the plane is axially aligned (ie, it's a horizontal plane with normal vector parallel to the Z axis), then just flip the sign of the Z component of the velocity vector and multiply the whole velocity vector by some damping factor d, where 0≤d<1 to damp out the velocity. Then repeat until some predetermined time has passed or the velocity reaches some minimal amount (your call on that).
对于任意方向的平面来说,这变得有点困难。您需要计算碰撞的入射角并反映关于平面法线的速度矢量。我不会在这里详细介绍,因为我怀疑您可能对此不感兴趣。
You start with an initial velocity vector at time t0 (v(t0)) and position (p(t0)). Gravity can be assumed to produces a constant acceleration (a(t0) = <0, -9.8 m/s2>, though your value may differ) until the object lands. So the general form of the motion for going from one timeslice to the next is:
To figure out when to stop that motion, you need to figure out at what time the object's path will intersect the surface against which it bounces. You'll have to do this for all of the surfaces for which this can reasonably be expected to happen. So for a plane with line equation
Ux + Vy + T = 0
you break the position vector into its components, as in:Then use the quadratic formula to find
tc
wherep(tc)
satisfies the line equation:Chose the branch such that tc > t0. From there it's simple to figure out where the object will collide with the surface. You can then update the velocity vector and position vector based on the behavior of the bounce. If the plane is axially aligned (ie, it's a horizontal plane with normal vector parallel to the Z axis), then just flip the sign of the Z component of the velocity vector and multiply the whole velocity vector by some damping factor d, where 0≤d<1 to damp out the velocity. Then repeat until some predetermined time has passed or the velocity reaches some minimal amount (your call on that).
It becomes a bit more difficult with arbitrarily oriented planes. You will need to calculate the angle of incidence of the collision and reflect the velocity vector about the plane normal. I won't go into the details here, as I suspect you're probably not interested in it.