Python物理库? [英] Python physics library?

查看:327
本文介绍了Python物理库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有适用于Linux的Python良好的最新物理库?我只是使用PyGame进入Python,但是PyGame缺少物理库并不酷.我花了大约两个小时的时间来寻找一个好的物理库,但这就像在试图获取石油.我似乎做不到.

Are there any good up-to-date physics libraries for Python that are for Linux? I'm just getting into Python using PyGame, but PyGame's lack of a physics library isn't cool. I spent about two hours trying to find a good physics library but it's like trying to grab oil; I can't seem to do it.

我根本不需要物理引擎.我要做的只是编程一个对象,使其跳起来",然后掉到地上.似乎发生了一些简单的冲突(我认为PyGame可以处理),但实际的跳转计算使我很困惑.如果事实证明没有可用的好物理库,那么这个问题似乎很简单,我可能会尝试找到一个基本的加速度方程式和一个重力方程式,然后尝试应用这些方程式...我想避免可以做到这一点.

I barely need a physics engine at all; all I want to do is program an object to 'jump' up and then fall back to the ground. There seems to be some simple collisions going on (which PyGame can handle, I think) but it's the actual jump calculation that's stumping me. If it turns out that there aren't any good ususable physics libraries, the problem seems simple enough that I might just try to find a basic acceleration equation and a gravity equation and try to apply those... I'd like to avoid having to do that, though.

感谢您的帮助.

推荐答案

基本的运动学方程式就是您所需要的.即使已经回答了问题,但如果我是您,我仍然会手动完成,只是因为使用图书馆似乎有点过分.启动速度方程:

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

您的跳跃希望计算角色的y位置,因此只需使用该方程式即可计算y位置,并以初始速度和加速度计算玩具.重力引起的标准加速度为-9.8米每秒^ 2(至少在地球表面上-这是不同的行星,不同的行星).无论您的角色位于什么位置,都从初始位置开始;如果地面为0,则从0开始.

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.

所以:

y = vt + (-4.9)t^2

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

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!

着陆时如何处理.

因此,在现实世界中,加速度是由不平衡力引起的.重力始终作用在您身上,但是当您站在地面上时,重力就会被地面的法向力"抵消并抵消.只要您站立的地面足够坚固以支撑您的体重,地面就会向后推并抵抗重力,并且您不会向下加速.因此,在游戏中,如果您实际上并没有模拟力,则只需在角色接触地面时将加速度从-9.8更改为0.如果地面平坦,则跳到position = initial position

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

这篇关于Python物理库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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