在Cubic Bezier曲线移动修改器中旋转精灵

时间:2014-04-28 11:20:02

标签: android andengine

我可以使用路径修改器旋转我的精灵,并在移动时使用正确的角度。为实现此目的,我在IPathModifierListener的 onPathWaypointStarted()方法中使用 setRotation ()。

PathModifier aModifier = new PathModifier(finishTime, aPath,
            new IPathModifierListener() {
                @Override
                public void onPathWaypointStarted(
                        PathModifier pPathModifier, IEntity pEntity,
                        int pWaypointIndex) {

                    switch (pWaypointIndex) {
                    case 0:

                        setRotation(270 + getAngle(xCur1, xCur2, x1, x2));

                        break;
                    case 1:
                        setRotation(270 + getAngle(x1, x2, x3, x4));

                        break;
                    case 2:
                        setRotation(270 + getAngle(x3, x4, x5, x6));

                        break;
                    case 3:

                        setRotation(270 + getAngle(x5, x6, x7, x8));
                        break;
                    case 4:
                        setRotation(270 + getAngle(x7, x8, x9, x10));

                        break;

                    case 5:
                        setRotation(270 + getAngle(x9, x10, x11, x12));
                        break;

                    case 6:
                        setRotation(270 + getAngle(x11, x12, x13, x14));
                        break;

                    default:
                        break;
                    }

                }

                @Override
                public void onPathWaypointFinished(
                        PathModifier pPathModifier, IEntity pEntity,
                        int pWaypointIndex) {
                }

                @Override
                public void onPathStarted(PathModifier pPathModifier,
                        IEntity pEntity) {
                }

                @Override
                public void onPathFinished(
                        final PathModifier pPathModifier,
                        final IEntity pEntity) {

                    ResourcesManager.getActivity().runOnUpdateThread(
                            new Runnable() {
                                @Override
                                public void run() {

                                    unregisterEntityModifier(pPathModifier);

                                }
                            });

                }
            });

我的getAngle在

之下
    private synchronized float getAngle(float startx, float starty,
        float targetx, float targety) {

    double angle1 = Math.atan2(targety - starty, targetx - startx);
    float angle = (float) Math.toDegrees(angle1);
    // Log.e("getAngle", "getAngle" + angle);
    return angle;
}

但是当我想使用CubicBezierCurveMoveModifier顺利移动wuite时,我找不到像IPathModifierListener这样的监听器。所以我不能用正确的角度旋转精灵。

任何想法。

我的对象:

enter image description here

1 个答案:

答案 0 :(得分:0)

我知道我已经迟到了这个并且可能已经找到了一个解决方案,但我刚刚遇到了这个问题并且掀起了这个问题。

我认为没有任何理由不能用于其他移动修饰符,但这是我为Andengine GLES2中的CubicBezierCurveMoveModifier所做的。

它真正做的就是在更新之前捕获旧的x,y坐标,然后根据新的x,y计算角度。

我添加了一个rotationOffset参数,因为我正在使用的精灵图像偏离了90度。可能已经认为0度是精灵的顶部,但要么我的数学错误或0度是图像的右边

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.CubicBezierCurveMoveModifier;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.ease.IEaseFunction;

public class CubicBezierCurveWithRotationModifier extends CubicBezierCurveMoveModifier {

    float rotationOffset = 0f;

    public CubicBezierCurveWithRotationModifier(float rotationOffset, float pDuration, float pX1, float pY1, float pX2, float pY2, float pX3, float pY3, float pX4, float pY4) {
        super(pDuration, pX1, pY1, pX2, pY2, pX3, pY3, pX4, pY4);
        this.rotationOffset = rotationOffset;
    }

    public CubicBezierCurveWithRotationModifier(float rotationOffset, float pDuration, float pX1, float pY1, float pX2, float pY2, float pX3, float pY3, float pX4, float pY4, IEaseFunction pEaseFunction) {
        super(pDuration, pX1, pY1, pX2, pY2, pX3, pY3, pX4, pY4, pEaseFunction);
        this.rotationOffset = rotationOffset;
    }

    public CubicBezierCurveWithRotationModifier(float rotationOffset, float pDuration, float pX1, float pY1, float pX2, float pY2, float pX3, float pY3, float pX4, float pY4, IEntityModifierListener pEntityModifierListener) {
        super(pDuration, pX1, pY1, pX2, pY2, pX3, pY3, pX4, pY4, pEntityModifierListener);
        this.rotationOffset = rotationOffset;
    }

    public CubicBezierCurveWithRotationModifier(float rotationOffset, float pDuration, float pX1, float pY1, float pX2, float pY2, float pX3, float pY3, float pX4, float pY4, IEntityModifierListener pEntityModifierListener, IEaseFunction pEaseFunction) {
        super(pDuration, pX1, pY1, pX2, pY2, pX3, pY3, pX4, pY4, pEntityModifierListener, pEaseFunction);
        this.rotationOffset = rotationOffset;
    }

    @Override
    protected void onManagedUpdate(float pSecondsElapsed, IEntity pEntity) {
        float oldX = pEntity.getX();
        float oldY = pEntity.getY();

        super.onManagedUpdate(pSecondsElapsed, pEntity);

        float deltaX = pEntity.getX() - oldX;
        float deltaY = pEntity.getY() - oldY;

        float degree = (float) Math.atan2(deltaY, deltaX);

        pEntity.setRotation(rotationOffset + MathUtils.radToDeg(degree));
    }
}