Python物理库?

发布于 2024-11-15 18:16:27 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

z祗昰~ 2024-11-22 18:16:28

Pymunk 是您可能想了解的另一个有前景的工具。

Pymunk is another promising one that you might want to take a look at.

允世 2024-11-22 18:16:28

试试pyODE,它是开放动态引擎的python绑定。

您可以在此处找到更多信息

Try pyODE, it is the python binding of open dynamic engine.

You can find more information here

天荒地未老 2024-11-22 18:16:28

您只需要基本的物理运动学方程即可。即使问题已经得到解答,如果我是你,我仍然会手动完成,因为使用库似乎有点矫枉过正。开始速度方程:

velocity = initial velocity + (acceleration * time) 

从那里,我们积分以找到位置:

position = initial position + (initial velocity * time) + (acceleration * time^2)/2

您的跳跃正在计算角色的 y 位置,因此只需使用该方程来计算 y 位置和玩具的初始速度和加速度。标准重力加速度为 -9.8 米每秒^2(至少在地球表面上 - 不同的行星有所不同)。无论你的角色在什么位置,都从初始位置开始,如果地面对你来说是 0,则从 0 开始。

因此:

y = vt + (-4.9)t^2

选择一个初始速度 v 值来提供跳跃开始时的向上运动,t 应该是自他开始跳跃以来经过的游戏时间。

您只需要一行代码即可完成此操作,无需任何库!

编辑:处理着陆时要做什么。

所以在现实世界中,加速度是由不平衡力引起的。重力始终作用在您身上,但是当您站在地面上时,重力会被地面的“法向力”抵消并抵消。只要你站立的地面足够坚固,能够支撑你的体重,地面就会向上推并抵抗重力,你就不会加速向下。因此,在游戏中,如果您实际上并未模拟力,只需在角色接触地面时将加速度从 -9.8 更改为 0 即可。如果地面平坦,则当位置=初始位置时,跳跃结束

The basic physics kinematic equations are all you need. Even though the questions was already answered, if I were you, I'd still do it by hand just because using a library seems like overkill. Start the equation for velocity:

velocity = initial velocity + (acceleration * time) 

From there, we integrate to find position:

position = initial position + (initial velocity * time) + (acceleration * time^2)/2

Your jump is looking to calculate the y position of the character, so just use that equation to calculate y position and toy with initial velocity and acceleration. Standard acceleration due to gravity is -9.8 meters per second^2 (at least on the surface of the earth - it's different son different planets). Start with initial position whatever your character is at, or 0 if the ground is 0 to you.

So:

y = vt + (-4.9)t^2

Pick an initial velocity v value to provide the upward movement when the jump starts, and t should be the elapsed game time since he started jumping.

You only need one line of code to do this, no libraries necessary!

EDIT: dealing with what to do when you land.

So in the real world, acceleration is caused by unbalanced forces. Gravity is always acting on you, but when you're standing in the ground, it's being countered and canceled out by the "normal force" of the ground. As long as the ground you're standing on is strong enough to support your weight, the ground will push back up and counter gravity and you will not accelerate downward. So in your game, if you're not actually simulating forces, just change the acceleration from -9.8 to 0 when your character is touching the ground. If the ground is flat, your jump ends when position = initial position

蓝眸 2024-11-22 18:16:28

根据ODE网站的安装说明,ODE包本身现在包含基于 CPython 的 Python 绑定;并且 pyODE 绑定被认为已过时。安装说明包含在上页中。

默认情况下,这是针对 Python 2 的,但我也能够以最少的工作量 (Mac OS X) 使此绑定适用于 Python 3。我什至可以运行教程。

这可能超出了主题,但仅供记录,以下是我必须更改的内容:

  1. 我必须通过取消注释 #defines 来更改 OpCode.h >sqrt、sincosasinacos(第 33-37 行,文件版本:3月20日, 2001)。这是我需要的一个丑陋的黑客,因为如果没有这个 ODE 本身就无法使用双精度算术进行编译,如果我们可以信任 ODE 页面上的文档,则需要与 python 绑定一起使用。
  2. 我必须通过在第 18 行后添加以下行来更改 setup.py

    # bugfix: 在Python3中read()返回字节,需要转换
    # 转为字符串
    尝试: 
        ode_cflags = [x.decode("utf-8") for x in ode_cflags]
        ode_libs = [x.decode("utf-8") for x in ode_libs]
    除了:
        # 在Python2中我们继续
        经过
    
  3. 要运行教程,请在 demos 目录中我用过

    <前><代码>$ 2to3 -w *.py

它。

According to the ODE website's installation instructions, the ODE package itself now contains Python bindings based on CPython; and the pyODE bindings are considered obsolete. Installation instructions are included in the above page.

By default this is for Python 2, but I was able to make this binding work with Python 3, too, with a minimum amount of work (Mac OS X). I could even run the tutorials.

This might be out of topic, but just for the record, here is what I had to change:

  1. I had to change OpCode.h by uncommenting the #defines for sqrt, sin, cos, asin and acos (lines 33-37, file version: March 20, 2001). This is an ugly hack that I needed because without this ODE itself did not compile with double precision arithmetic, which one needs to use with the python bindings if we can trust the documentation on the ODE page.
  2. I had to change setup.py by adding the following lines after line 18:

    # bugfix: in Python3 read() returns bytes, which need to be converted
    # to strings
    try: 
        ode_cflags = [x.decode("utf-8") for x in ode_cflags]
        ode_libs = [x.decode("utf-8") for x in ode_libs]
    except:
        # in Python2 we just continue
        pass
    
  3. To run the tutorials, in the demos directory I used

    $ 2to3 -w *.py
    

That's it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文