为什么我的重力会起作用?
I can't figure out a smart way to get gravity. Now with this it detects which block the character is over but it does't drop to that block!
Is there a better way to do gravity? I'd like to do this without a game library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你所说的“获得重力”是什么意思;你的问题不清楚。我假设如果您可以检测块何时结束,则可以使用以下公式:
其中
s
是时间t
时的距离,u
是初始速度(在您的情况下为零),a
是加速度(在地球上为 9.8m/s2)。本质上,您将根据在t
时获得的值来调整对象的顶部位置(因此对象的原始顶部位置 + s(t)
)。我想你会使用某种动画循环。也许是一个setInterval
。也许其他在 Javascript 动画方面有更多经验的人可以提出实现这一点的最佳方法。然而,这将是您用来计算对象在t
时间(如果它掉落)的位置的公式。I don't know what you mean by "get gravity"; your question is unclear. I assume that if you can detect when the block is over, you can use the following formula:
Where
s
is the distance at timet
,u
is the initial velocity (which in your case would be zero), anda
is the acceleration (on Earth this is 9.8m/s2). Essentially you would be adjusting the top position of your object based on the value you get at timet
(sooriginal top position of object + s(t)
). I would imagine you would use some sort of animation loop. Perhaps asetInterval
. Maybe others with more experience in Javascript animation can chime in about the best way to implement this. However, this would be the formula that you would be using to figure out where the object is at timet
, if it falls.平台游戏中的重力基本上是这样的:
现在要跳跃,可以简单地执行以下操作:
如果您愿意(并且了解 C),您可以在此处查看我的基本平台游戏(带有移动平台):
http://github.com/BonsaiDen/Norum/blob/master/来源/character.c
Basically gravity in a platformer goes like this:
Now to jump one could simply do this:
You can, if you want(and understand C), check out my game for a basic platformer(with moving platforms) here:
http://github.com/BonsaiDen/Norum/blob/master/sources/character.c