2D 游戏物理,正确执行
我隐隐怀疑自己做错了。现在它可以工作了,只要重力将物体拉向地面,但我在操纵物体的速度时遇到了困难。
这是一个球跳跃并落向地面。
我有另一个名为“jump”的函数,它只是将 jSpeed
添加到它的 yVel
我可以增加重力
,并且它下落得更快。
我可以提高jSpeed
速度,它会上升得更久,但不会更快
,但我无法让它更快地完成所有事情。它看起来慢得令人痛苦,这可能是也可能不是因为我的模拟器平均以 11 fps 运行。
这只是我的模拟器的问题,还是我这边的问题?
float time = elapsedTime/1000F;
if (speed < maxSpeed){
speed = speed + accel;
}
if(mY + mVelY < Panel.mHeight){ //0,0 is top-left
mVelY += (speed);
}
if (!(mY + height >= Panel.mHeight)){
mVelY = mVelY + gravity ;
}
mX = (float) (mX +(mVelX * time));
mY = (float) (mY + (mVelY * time));
I have a sneaking suspicion I'm doing this wrong. It works now, to the extend that gravity pulls the object down toward the ground, but I'm having trouble manipulating the speed of the object.
What this is, is a ball jumping and falling towards the ground.
I have another function called "jump" that just adds jSpeed
to it's yVel
I can increase gravity
, and it falls faster.
I can increase the jSpeed
speed, and it'll rise up longer, but not faster
But I can't get it to do everything faster. It just looks painfully slow, which may or may not be because of my emulator running at 11 fps, on average.
Is it just my emulator, or is it something on my end?
float time = elapsedTime/1000F;
if (speed < maxSpeed){
speed = speed + accel;
}
if(mY + mVelY < Panel.mHeight){ //0,0 is top-left
mVelY += (speed);
}
if (!(mY + height >= Panel.mHeight)){
mVelY = mVelY + gravity ;
}
mX = (float) (mX +(mVelX * time));
mY = (float) (mY + (mVelY * time));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您在这里有正确的总体想法,但您的代码中有很多内容令人困惑。
我的问题主要是关于可变速度 - 在我看来,你的球正在通过可变速度和加速度加速,直到达到最大速度。与此相反的是重力将球向下拉(加速)。
现在通常这并不是您所描述的“跳跃”方式。因此,对我来说,当玩家点击“跳跃”时,您应该将 YVel 设置为 jspeed,然后让方程的重力部分将其拉回 - 也就是说,如果您删除了代码:
那么它会上升一点然后松开动量到重力并向下返回 - 正如上面的代码不断向上推动它直到它到达顶部,并且一旦它开始下降就将它推回顶部。
我想知道关于速度和 maxspeed 的代码是否应该作用于 XVel 而不是您编码的 YVel - 这会更有意义。
I think you have the right general ideas here but a lot about your code is confusing.
My issues are mostly about the variable speed - it seems to me that your ball is being accelerated up by the variables speed and accel until reaching a maximum speed. Opposing this is gravity pulling (accelerating) the ball down.
Now typically this isn't how a 'jump' the way you describe it is. So for me when the player hits 'jump' you should set the YVel to jspeed and just let the gravity part of the equation bring it back down - that is if you deleted the code:
Then it would go up for a bit and then loose momentum to the gravity and come back down - where as this code above keeps pushing it upwards until it hits the top, and as soon as it starts descending pushes it back up.
I wonder if the code around speed and maxspeed is supposed to act on XVel and not YVel as you have coded - that would make more sense.