为什么我的重力会起作用?

发布于 2024-09-11 03:39:34 字数 187 浏览 0 评论 0原文

http://davzy.com/gameA/

我想不出一种获得重力的聪明方法。现在,它可以检测到角色结束了哪个块,但它不会落到该块!

有没有更好的方法来实现重力?我想在没有游戏库的情况下做到这一点。

http://davzy.com/gameA/

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 技术交流群。

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

发布评论

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

评论(2

傾城如夢未必闌珊 2024-09-18 03:39:34

我不知道你所说的“获得重力”是什么意思;你的问题不清楚。我假设如果您可以检测块何时结束,则可以使用以下公式:

s(t) = ut + 1/2at2

其中 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:

s(t) = ut + 1/2at2

Where s is the distance at time t, u is the initial velocity (which in your case would be zero), and a 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 time t (so original top position of object + s(t)). I would imagine you would use some sort of animation loop. Perhaps a setInterval. 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 time t, if it falls.

初见终念 2024-09-18 03:39:34

平台游戏中的重力基本上是这样的:

var currentGrav = 0.0;
var gravAdd = 0.5; // add this every iteration of the game loop to currentGrav
var maxGrav = 4.0; // this caps currentGrav

var charPosY = getCharPosY(); // vertical position of the character, in this case the lower end
var colPosY = getColDow(); // some way to get the vertical position of the next "collision"

for(var i = 0; i < Math.abs(Math.ceil(currentGrav)); i++) { // make sure we have "full pixel" values
    if (charPosY == colPosY) {
       onGround = true;
       break; // we hit the ground
    }
    onGround = false;
    charPosY++;
}

现在要跳跃,可以简单地执行以下操作:

if (jumpKeyPressed && onGround) {
    currentGrav = -5.0; //
}

如果您愿意(并且了解 C),您可以在此处查看我的基本平台游戏(带有移动平台):
http://github.com/BonsaiDen/Norum/blob/master/来源/character.c

Basically gravity in a platformer goes like this:

var currentGrav = 0.0;
var gravAdd = 0.5; // add this every iteration of the game loop to currentGrav
var maxGrav = 4.0; // this caps currentGrav

var charPosY = getCharPosY(); // vertical position of the character, in this case the lower end
var colPosY = getColDow(); // some way to get the vertical position of the next "collision"

for(var i = 0; i < Math.abs(Math.ceil(currentGrav)); i++) { // make sure we have "full pixel" values
    if (charPosY == colPosY) {
       onGround = true;
       break; // we hit the ground
    }
    onGround = false;
    charPosY++;
}

Now to jump one could simply do this:

if (jumpKeyPressed && onGround) {
    currentGrav = -5.0; //
}

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

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