如何强制像愤怒的小鸟一样的游戏物体弹丸轨迹

时间:2017-02-08 13:23:44

标签: java android jbox2d

我是一名新的Android游戏开发者。我想强制我的游戏物体像愤怒的小鸟一样移动到射弹轨迹。

Related example here和我的例子你可以看到图像。

here

我使用jbox2d一点点代码,但我不知道如何使它工作。

private static class ProjectileEquation {

    public float gravity;
    public Vector2 startVelocity = new Vector2();
    public Vector2 startPoint = new Vector2();

    public float yogx(float t) {
        return startPoint.x + t * startVelocity.x;
    }

    public float yogy(float t) {

        float u = (float) -4.97741939970554;
        float v = (float) 30.88079;
        float tt = 1/6f;  
        float n = (u+v)/2*tt;

        return startPoint.y + n * t *startVelocity.y + 0.5f *(n*n+n) 
            * t * t * gravity;
    }
}

private static class Controller {
    public float power = -66f;
    public float angle = 585f;      
}
}

public float P2M(float xPixels) {
return xPixels * metersPerPixel;
}

public float M2P(float xMetres) {
return xMetres * pixelsPerMeter;
}

public float MaxHeight(){

 if (projectileEquation.startVelocity.y > 0){

        return projectileEquation.startPoint.y;   
 }
    return projectileEquation.startPoint.y;
    }

public void Update() {
projectileEquation.startVelocity.set(controller.power,controller.power);
projectileEquation.startVelocity.rotate(controller.angle);
 //     bd = bodies;

}



@Override
protected void onDraw(Canvas c) {
    super.onDraw(c);

    MaxHeight();

    Update();

    float t = 0f;
    x = c.getWidth()/2;
    y = c.getHeight()-20;

    float timeSeparation = this.timeSeparation;    

    for (int i = 0; i < trajectoryPointCount; i++) {

        float x = this.x +  projectileEquation.yogx(t);
        float y = this.y +  -projectileEquation.yogy(t);

        t += timeSeparation;

        if(A) {
            c.drawBitmap(bmp, x, y, null);
        }else if(B) { 

            float bX = M2P(body.getPosition().x);
            float by = c.getHeight() - M2P(body.getPosition().y); 
            body.setActive(true);

            c.drawCircle(bX, by, M2P(body.m_fixtureList.m_shape.m_radius), circlePaint);
        }
    }
}  

public class jbox2d {

    public float targetFps = 30.0f;
    public float timeStep = (1.0f / targetFps);
    private int interations = 2;
    public Body body;
    private World world;
    private BodyDef bodydef;

    public void Create(){

        Vec2 gravity = new Vec2(0,-10f);
        boolean doSleep = true;
        world = new World(gravity, doSleep);
    }

    public void addboll() {

        bodydef = new BodyDef();
        bodydef.type = BodyType.DYNAMIC;
        bodydef.allowSleep = true;


        bodydef.position.set(8.24f,10f);

        body = world.createBody(bodydef);
        body.setActive(false);
        float tt = 0f;     

        Vec2 stx = new Vec2();
        Vec2 sty =new Vec2();

        for (int it = 0; it < 20; it++) { 

            stx = M2P(projectileEquation.startVelocity.x);
            sty = M2P(projectileEquation.startVelocity.y);

            stx = M2P(projectileEquation.yogx(tt));
            sty = -M2P(projectileEquation.yogy(tt));

            body.setLinearVelocity(new Vec2(stx,sty));
            body.applyLinearImpulse(body.getLinearVelocity(), body.getWorldCenter());

            tt += timeSeparation;
        }

        CircleShape circle = new CircleShape();
        circle.m_radius = (float).42;

        FixtureDef fixturedef = new FixtureDef();
        fixturedef.density = 1.0f;
        fixturedef.restitution  = 0.9f;
        fixturedef.friction = 0.2f;
        fixturedef.shape = circle;
        body.createFixture(fixturedef);

    }

    public void update(){
        world.step(timeStep, interations, interations);
    }   
}

0 个答案:

没有答案