基于角度+弹跳的“手榴弹”投影

时间:2014-07-17 08:33:48

标签: optimization physics game-physics physics-engine cartesian-coordinates

我在合成高级对象投影公式方面遇到了一些麻烦。我已经找到了一些基本的物理模拟公式,例如:

  • 对象速度 x += cos(angle); y += sin(angle); *其中角度可以通过鼠标位置或棕褐色(...目标值和初始值)获得

但只能根据角度直接行进。

  • 重力 Yvelocity = Yvelocity - gravity; if(!isHitPlatform) { Obj.y += YVelocity }

  • 跳出// No point if we've not been sized... if (height > 0) { // Are we bouncing... if (bounce) { // Add the vDelta to the yPos // vDelta may be postive or negative, allowing // for both up and down movement... yPos += vDelta; // Add the gravity to the vDelta, this will slow down // the upward movement and speed up the downward movement... // You may wish to place a max speed to this vDelta += gDelta; // If the sprite is not on the ground... if (yPos + SPRITE_HEIGHT >= height) { // Seat the sprite on the ground yPos = height - SPRITE_HEIGHT; // If the re-bound delta is 0 or more then we've stopped // bouncing... if (rbDelta >= 0) { // Stop bouncing... bounce = false; } else { // Add the re-bound degregation delta to the re-bound delta rbDelta += rbDegDelta; // Set the vDelta... vDelta = rbDelta; } } } }

我需要帮助方式来组合这三个公式来创建一个有效且轻量级的算法,该算法允许将对象投影到由角度确定的拱形中,但在停止之前继续反弹几次,所有这些都是每个点之间可接受的不连续量。 *注意:通过af(x)= -x ^ 2公式确定手榴弹会随着斜率增加而产生更大的跳跃不连续性,迫使您反转公式以找到x = + -y值(确定是否+或 - ,检查边界)。

1 个答案:

答案 0 :(得分:1)

类似的东西:

class granade
{
    private static final double dt = 0.1; // or similar

    private double speedx;
    private double speedy;
    private double positionx;
    private double positiony;

    public granade(double v, double angle)
    {
        speedx = v * Math.cos(angle);
        speedy = v * Math.sin(angle);

        positionx = 0;
        positiony = 0;
    }

    public void nextframe()
    {
        // update speed: v += a*dt
        speedy -= gravity* dt;

        // update position: pos += v*dt
        positionx += speedx * dt;
        double newpositiony = positiony + speedy*dt;

        // bounce if hit ground
        if (newpositiony > 0)
            positiony = newpositiony;
        else {
            // bounce vertically
            speedy *= -1;
            positiony = -newpositiony;
        }
    }

    public void draw() { /* TODO */ }
}

OT:避免使用Math.atan(y / x),使用Math.atan2(y,x)