恒定的Box2D体移动到在某一时刻 [英] Constant box2d body moving to point at a time

查看:127
本文介绍了恒定的Box2D体移动到在某一时刻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我Box2D的身体在同一时间(定速)移动从(0,0)这个机构(100,100),它可以。怎么可能?我想这code:this.body.setLinearVelocity(新Vector2(1,0));但它正在不停。

I have box2d body in Andengine.I wanna move this body from (0,0) to (100,100) at a time(constant speed).How can it be possible? I tried this code: this.body.setLinearVelocity(new Vector2(1, 0)); but it is moving non-stop.

推荐答案

我想沿着pdefined路径$ P $ Body.setTransform(...)移动是使用最简单的方法。这样,我们基本上忽略所有的力,摩擦,扭矩,碰撞等,并直接设置体的位置。

I guess the easiest way to move along a predefined path would be to use Body.setTransform(...). This way we basically ignore all forces, friction, torque, collisions etc and set the position of the body directly.

我不知道Andengine,所以这只是伪code:

I don't know Andengine, so this is just pseudocode:

public void updateGameLoop(float deltaTime) {
    Vector2 current = body.getPosition();
    Vector2 target = new Vector2(100, 100);

    if (!current.equals(target)) {
        float speed = 20f;
        Vector2 direction = target.sub(current);
        float distanceToTarget = direction.len();
        float travelDistance = speed * deltaTime;

        // the target is very close, so we set the position to the target directly
        if (distanceToTarget <= travelDistance) {
            body.setTransform(target, body.getAngle());
        } else {
            direction.nor();
            // move a bit in the target direction 
            body.setTransform(current.add(direction.mul(travelDistance)), body.getAngle());
        }
    }
}

这篇关于恒定的Box2D体移动到在某一时刻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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