像笨鸟先飞或喷气背包喜悦平稳飞行运动骑重力和Accelaration [英] Smooth fly movement like flappy bird or jet pack joy ride with gravity and Accelaration

查看:282
本文介绍了像笨鸟先飞或喷气背包喜悦平稳飞行运动骑重力和Accelaration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的游戏中,当你点击/点击屏幕字符飞行。保持敲击的字符将飞(有些什么类似笨鸟先飞和喷气背包)。然而,运动不顺畅可言,因为喷气背包的。

下面是我的code的样本。

Varaible动初始化

  maxSpeed​​Limit = spriteHeight / 10;
速度= maxSpeed​​Limit / 2; //最大速度的一半

触摸事件

 公共无效onTapOrClick(INT行动){
    如果(动作== UP){
        sprite.up = TRUE;
    }
    其他{
        sprite.up = FALSE;
    }
}

从游戏循环称为雪碧更新

 公共无效更新(){
    如果(向上){
        Ÿ - =速度; //起飞
    }
    其他{
        Y + =速度; //飞下来
    }
    如果(速度< maxSpeed​​Limit){
        速度++; //可能是添加**速度/加速度吱吱方式**
    }
}

我觉得速度++ 不是一个平滑的方式来提高速度,我不知道,但可能会增加一些的时间相关变量递增可以改善它,而且加入了重力将使它更现实,但我不知道该怎么做,我看过一些博客,第一件事我不能用正确的关键字搜索,第二件事是他们是这么难了解,因为它们包含了平台相关的codeS。请帮助。

我在做这个游戏在Android中,但在任何语言code被接受(HTML5,JavaScript中,的Andr​​oid,Flash或任何)。

问:如何加速度和重力添加到对象(精灵),它飞在用户点击或单击并落在发布
一些类似于喷气背包兜风(仅上下运动)

更新时间:

@scottt提醒后,我已经实现了 DY + =重力+ flapp <​​/ code>我现在能感觉到重力,但有2个问题。


  1. 我的屏幕高度是480,和雪碧立即触摸上(Y = 0),下(Y = 480)的边界,监守我认为它不断增加精灵的速度。

  2. 当它接触地面,似乎很havey,并采取太多的时间来解除精灵在空中。

一些如何应该有一些限制DY其中不断被添加到y的位置。

下面是更新code。

  INT降速= 1;
INT upSpeed​​ = -2;
INT DY = 0;
私人无效更新(){
    如果(扑){
        upSpeed​​ = -2; //如果飞行速度
    }
    其他{
        upSpeed​​ = 0;
    }    DY + =降速+ upSpeed​​;
    如果(DY&LT; -10){
        DY = -10; //限制上升速度
    }
    否则如果(DY→8){
        DY = 8; //极限重力
    }
    Y + =颐; // y中的位置增加价值
    如果(γ&GT; GAME_HEIGHT - sprite.getHeight()){
        Y = GAME_HEIGHT - sprite.getHeight(); //重新设置Y,如果触摸地面
        DY = 0; //重新设置速度,否则就让它非常沉重上升
    }
    否则如果(γ℃,){
        Y = 0; //如果触摸上限重置ÿ
        DY = 0; //重新设置速度,否则要花费很多时间下降(因为这将是负数)
    }
}


解决方案

重力只是一个向下的加速度。加速依次只是速度(定向速度)的变化。对于下面的讨论中,我做如下假设让事情变得更简单:


  • 您的水平速度是恒定

  • 输入窃听被称为扑

  • 重力可以正常工作

  • 简单化,而不是一种科学的方法/措辞是我们的目的确定

有关每次通过更新循环,所有的各种加速度必须被求和,总添加到当前速度。在你的情况,有2个可能的加速度,重力和可能的震荡。重力是恒定的并且是负值(是作品向下),而只扑攻丝如为正期间发生(向上)。

让我们设置重力每个环路-10像素,攻为每环+25像素,初始高度为500。一些初步的定义是:

 静态最终诠释比重= -10; //持续向下的加速度
静态最终诠释扑= 25; //向上加速时isFlapping是真实的布尔isFlapping = FALSE; //是鸟扑INT DY = 0; //当前垂直速度
Y = 500; //当前垂直位置

通过每一次循环中,没有扑的速度计算将是:

  DY + = +重力拍打;

所以,第一次通过,该速度计算将是 DY = 0 +(-10)+ 0 = -10 。第二次, DY = -10 +(-10)+ 0 = -20 。第五次, DY = -40 +(-10)= -50 。每次经过,下降速度也比之前的时间10个以上。

的高度是简单的。每次通过,该高度由垂直加速度的量发生变化。所以:

  Y + = DY;

所以,第一次通过,其高度将是 Y = 500 +(-10)= 490 。第二次, Y = 490 +(-20)= 470 。而第五次, Y = 400 +(-50)= 350 。因为下跌的速度,每次通过增加,小鸟就会直线下降的速度越来越快,直到图示!

这就是扑用武之地。每次循环时间,其中扑发生,+25将施加到DY计算。所以,让我们假设禽流开工6迭代扑。该DY计算将是 DY = -50 +(-10)+ 25 = -35 ,高度应为 Y = 350 +(-35 )= 315 。接下来的时间,通过将使 DY = -35 +(-10)+ 25 = -20 ,高度应为 Y = 350 +( - 20)= 295 。还在下降,但是速度比较慢。该时间后:即 DY = -20 +(-10)+ 25 = -5 Y = 295 +( - 5)= 290 。之后的时间终于显示了高度的增益: DY = -5 +(-10)+ 25 = 10 Y = 290 + 10 = 300

所有这一切说,你一定会需要用玩数字游戏,直到你得到一个满意的结果。

TLDR:你不想直接利用重力和扑改变高度。相反,你要使用重力和拍打计算速度为每一次迭代,然后用它来调整高度。

I am developing a simple game in which a character fly when you tap/click the screen. keep tapping the character will fly (some what similar to flappy bird and jet pack). However the movement is not smooth at all, as of jet pack.

Here is sample of my code.

Varaible Initilization

maxSpeedLimit  = spriteHeight/10;
speed = maxSpeedLimit/2; //half of the max speed

touch event

public void onTapOrClick(int action) {
    if (action == UP) {
        sprite.up= true;
    }
    else {
        sprite.up = false;
    }
}

Sprite update called from game loop

public void update() {
    if (up) {
        y -= speed; //fly up
    }
    else {
        y += speed;// fly down
    }
    if (speed < maxSpeedLimit) {
        speed++; // May be cheep way to add **velocity/acceleration**
    }   
}

I think speed++ is not a smooth way to increase the speed, I am not sure but may be adding some time related variable to increment may improve it, moreover adding a gravity will make it more realistic but i have no idea how to do it, I have read few blogs, first thing I am not able to search with the correct keyword, and second thing is they are so hard understand because they contains platform related codes. Please help.

I am making this game in android, but code in any language is accepted (HTML5, javascript, Android, Flash or any).

Q: How to add acceleration and gravity to an object (sprite) which fly when user tap or click and fall on release? Something similar to jet pack joyride (only up and down movement)

UPDATED

After @scottt advise I have implemented the dY += gravity + flapp I can feel now gravity, however there are 2 issues.

  1. My screen height is 480, and sprite immediately touch upper(y=0) and lower(y=480) boundary, becuase i think it keep increasing the speed of the sprite.
  2. when it touches the ground, it seems very havey and take much time to lift the sprite up in the air.

Some how there should be some limit to dY which is constantly being added to y location.

Here is update code.

int downSpeed = 1;
int upSpeed = -2;
int dy = 0;
private void update() {
    if (flapping) {
        upSpeed = -2; //if flying speed
    }
    else {
        upSpeed = 0;
    }

    dy  +=  downSpeed + upSpeed;
    if (dy < -10) {
        dy = -10; //limit for rise speed
    }
    else if (dy > 8) {
        dy = 8; //limit for gravity
    }
    y += dy; // add value in y location
    if (y > GAME_HEIGHT - sprite.getHeight()) {
        y = GAME_HEIGHT  - sprite.getHeight(); // reset y, if touch ground
        dy = 0; //reset speed, otherwise it make it very heavy to rise
    }
    else if ( y < 0) {
        y = 0; //reset y if touch upper limit
        dy = 0; //reset speed, otherwise take much time to fall (as it would be in negative)
    }
}

解决方案

Gravity is just a downward acceleration. An acceleration is in turn just a change in velocity (directed speed). For the following discussion, I make the following assumptions to keep things simpler:

  • your horizontal speed is constant
  • input tapping is called 'flapping'
  • gravity behaves normally
  • simplistic, rather than a scientific approach/verbiage is OK for our purposes

For each pass through the update loop, all of the various accelerations must be summed and the total added to the current speed. In your situation, there are 2 possible accelerations, gravity and possibly flapping. Gravity is constant and is a negative value (is works downward), while flapping only occurs during tapping as is positive (upwards).

Let's set gravity to -10 pixels per loop, tapping to be +25 pixels per loop, and initial height to 500. Some initial definitions are:

static final int gravity = -10;   // constant downward acceleration
static final int flapping = 25;   // upward acceleration whenever isFlapping is true

Boolean isFlapping = false;   // Is the bird flapping

int dY = 0; // current vertical speed
y = 500;    // current vertical position

Each time through the loop, without flapping, the speed calculation would be:

dY += gravity + flapping;

So the first time through, the speed calculation would be dY = 0 + (-10) + 0 = -10. The second time, dy = -10 + (-10) + 0 = -20. The 5th time, dy = -40 + (-10) = -50. Each time through, the downward speed is 10 more than the time before.

The height is simple. Each time through, the height changes by the amount of vertical acceleration. So:

y += dY;

So the first time through, the height would be y = 500 + (-10) = 490. The second time, y = 490 + (-20) = 470. And the 5th time, y = 400 + (-50) = 350. Because the rate of falling increases each time through, the bird will plummet faster and faster until splat!

That's where flapping comes in. Each time through the loop where flapping is occurring, a +25 will be applied to the dY calculation. So lets assume the bird starts flapping in 6th iteration. The dy calculation would be dy = -50 + (-10) + 25 = -35 and the height would be y = 350 + (-35) = 315. The next time through would give dy = -35 + (-10) + 25 = -20 and the height would be y = 350 + (-20) = 295. Still falling, but more slowly. The time after: that dy = -20 + (-10) + 25 = -5 and y = 295 + (-5) = 290. The time after that finally shows a gain in height: dy = -5 + (-10) + 25 = 10 and y = 290 + 10 = 300.

All that said, you'll definitely need to play with the numbers until you get a satisfying result.

TLDR: You don't want to change the height directly using gravity and flapping. Instead you want to use gravity and flapping to calculate the speed for each iteration and then use that to adjust the height.

这篇关于像笨鸟先飞或喷气背包喜悦平稳飞行运动骑重力和Accelaration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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