游戏物理 - 不是很生活 [英] Game Physics - Not very life-like

查看:168
本文介绍了游戏物理 - 不是很生活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个简单的游戏(cocos2d),我有一个小物理引擎移动精灵,以便它可以跳跃和站在平台上。



要更新位置(垂直运动),我在每次更新中使用基本运动学方程:




  • position = oldPosition + velocity(delta)+1/2(gravity)(delta)^ 2

  • velocity = oldVelocity +(gravity)(delta)



由于某种原因,游戏看起来不像生活般。看起来在弧的顶部附近需要很长时间,尽管我有多么重力。如果我想让我的sprite跳同样的高度,但减速和加速更快,但仍然跳高,像以前一样高,我该怎么办?我希望这是有道理的。

解决方案

这里的问题是你的物理错误!



在你的sprite的位置上调用update方法时,你应该首先设置加速度。我认为重力是一个常数这个游戏,所以你只需要设置一次。 (-9.81,或类似的东西。)



然后你想通过velocity = old_velocity + acceleration * time更新y方向的字符速度。 >

完成此操作后,您可以用类似的方式更新位置:position = old_position + velocity * time。



你正在使用更新的位置是有效的,只有delta是总的经过时间,而不是时间步! (我假设delta是一个时间步骤,因为这是物理游戏通常被编程。)



我希望这有助于!如果你想知道更多,检查suvat方程,你会看到,你可以计算最终位置,如果你知道一个初始速度和恒定加速度,在你的游戏将有一个速度,当你做跳跃和碰撞,所以它不惊讶,它是不现实的!任何其他问题,只是评论,我会尽力帮助你进一步。



编辑:我重新编程了你在这里做了一些使用OpenGL绘制的框。你的更新位置的方法似乎不工作。如果 dt 或时间步长是1.0d,我们不知道为什么。然后当它们与某物碰撞时,它们停在一起而不是反弹。我不知道为什么。



但是,我还有一个框在屏幕上使用我描述的物理:
v = u + a * t s = s_last + v * t
框符合预期并正确退回。由于简化,能量在弹跳中消失。


In a simple game (cocos2d) I have a made a small physics engine to move a sprite so that it can jump and stand on platforms ect.

To update the position (vertical movement) I use basic kinematics equations in each update:

  • position = oldPosition + velocity(delta) +1/2(gravity)(delta)^2
  • velocity = oldVelocity + (gravity)(delta)

For some reason the game doesn't seem very life-like. It seems to take a long time near the top of an arc, despite how great I make gravity. If I want my sprite to jump the same height, but decelerate and accelerate more quickly, but still jump just as high as before, how should I do that? I hope that makes sense.

解决方案

Problem here is you have made a mistake with your physics! But that is okay, because it is an easy fix.

When calling your update method on your sprite's position, you should first set the acceleration. I assume gravity is a constant for this game, and so you only need to set it once. (-9.81, or something similar.)

You then want to update the velocity of the character in the y direction by velocity = old_velocity + acceleration * time.

After doing this you then update the position in a similar way: position = old_position + velocity * time.

The equation you are using to update the position is valid only if delta is the total elapsed time, and not a time-step! (I have assumed delta is a time step, because that's how physics games are usually programmed.)

I hope this helps! If you want to know more, check out suvat equations, you will see that you can compute a final position if you know an initial velocity and a constant acceleration, where as your game will have a velocity that varies when you do jumping and collisions, and so it does not surprise that it isn't realistic! Any more questions, just comment and I will try to help you further.

EDIT: I reprogrammed exactly what you have done here with some boxes drawn using OpenGL. Your method of updating the position does not seem to work. The boxes only seem to fall correctly if the dt or timestep is 1.0d, and I am not sure why. Then when they collide with something, they stop all together instead of bouncing. I am not sure exactly why.

However, I also have another box on the screen which uses the physics I described: v = u + a*t and s = s_last + v*t The box falls exactly as expected and bounces correctly. Due to a simplification, energy is lost in the bouncing.

这篇关于游戏物理 - 不是很生活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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