沿LibGDX中的路径向量偏移精灵

时间:2017-11-29 21:39:58

标签: java vector libgdx sprite

我目前有一个跟踪路径的精灵......从转向角度来看一切都很好,但是我试图让精灵的中心跟踪路径,而不是角落(0,0)跟踪路径。基本上,我希望鱼的中心跟随线。

下面是我实施的内容以及我希望实现的目标。

enter image description here

这个机制的核心实现在于我的update()方法;如下;

private void update(float deltaTime) {
        float angle = (float) Math.atan2(path.get(waypoint).y - getY(),  path.get(waypoint).x - getX());
        velocity.set((float) Math.cos(angle) * speed, (float) Math.sin(angle) * speed);

        velocity_normal = new Vector2(-velocity.y, velocity.x).nor();

        setPosition(
                getX() + (velocity.x * deltaTime),
                getY() + (velocity.y * deltaTime)
        );
        setRotation(angle * MathUtils.radiansToDegrees);

        if(isWayPointReached()){
            setPosition(path.get(waypoint).x, path.get(waypoint).y);
            if(waypoint + 1 >= path.size){
                waypoint = 0;
            } else {
                waypoint++;
            }
        }
    }

特别是setPosition来电。我最初的想法是计算速度矢量的法向量,归一化,并分别乘以鱼的高度乘以x和y分量......在我看来,这会使鱼的高度(150px)偏移。尝试的代码如下;

velocity_normal = new Vector2(-velocity.y, velocity.x).nor();

setPosition(
                getX() + (velocity.x * deltaTime) + velocity_normal.x * getHeight() * deltaTime,
                getY() + (velocity.y * deltaTime) + velocity_normal.y * getHeight() * deltaTime
        );

结果出现了一些奇怪的行为,鱼从线上逐渐进一步,似乎矢量正在复合并添加每一帧。

我也尝试在达到每个路点后更新法线向量,但这也不起作用。

我认为上述逻辑是正确的,但是我在矢量数学中犯了一个基本错误吗?

非常感谢您的协助。

编辑:

添加到构造函数:

setPosition(
                path.get(waypoint).x  - 0.5f * getWidth() ,
                path.get(waypoint).y - 0.5f * getHeight()
        );

修订后的update()方法;

private void update(float deltaTime) {
        float angle = (float) Math.atan2(path.get(waypoint).y - getY(),  path.get(waypoint).x - getX());
        velocity.set((float) Math.cos(angle) * speed, (float) Math.sin(angle) * speed);


        Vector2 velocity_normal = new Vector2();
        velocity_normal.set(velocity).nor().scl( speed * deltaTime ); // multiply the speed to scale the unit vector up

        translate( velocity_normal.x, velocity_normal.y );




        setRotation(angle * MathUtils.radiansToDegrees);

        if(isWayPointReached()){
            setPosition(path.get(waypoint).x, path.get(waypoint).y);
            if(waypoint + 1 >= path.size){
                waypoint = 0;
            } else {
                waypoint++;
            }
        }
    }

请注意省略setPosition调用,并替换为;

Vector2 velocity_normal = new Vector2();
velocity_normal.set(velocity).nor().scl( speed * deltaTime );        
translate( velocity_normal.x, velocity_normal.y );

如何影响如下所述的pointA / pointB?

感谢。

2 个答案:

答案 0 :(得分:1)

Sprite位置来自左下角,而原点(sprite旋转的位置)已经设置为精灵的中心。所以只有本地偏移是错误的。你必须从位置减去一半大小,然后精灵可以从那个偏移量相对移动。

设置精灵的位置:

setPosition(
        path.get(waypoint).x - 0.5f * getWidth(),
        path.get(waypoint).y - 0.5f * getHeight()
);

在更新方法中。因为您每帧都要添加速度,所以可以翻译精灵。

void update(float deltaTime){

    // directional global vector
    Vector2 velocity = tmp.set(path.get(waypoint)).sub(path.get(waypoint - 1)).nor().scl(speed * deltaTime);
    // reference angle is relative to the right vector(1, 0)
    float angle = velocity.angle();

    setRotation(angle);
    translate(velocity.x, velocity.y);

    if (isWayPointReached()){
        setPosition(
                path.get(waypoint).x - 0.5f * getWidth(),
                path.get(waypoint).y - 0.5f * getHeight()
        );
        if(waypoint + 1 >= path.size){
            waypoint = 1;
        } else {
            waypoint++;
        }
    }
}

答案 1 :(得分:0)

Sprite类中有setCenter()方法:

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setCenter-float-float-

尝试设置这样的中心,而不是另外计算它。