2012-07-20 111 views
1

我想沿着直线路径移动一个Sprite。我想在斜率上移动5个像素,或者每次使用该方法时都要使用斜边,直到达到终点。沿着直线移动对象

我有斜线和y截距的线,我也有通过getX()和getY()的精灵当前的X和Y值。终止的X和Y点是变量finalX和finalY。

我已经尝试了这么多的方程,但我似乎无法让他们中的任何人工作。我错过了什么!!?

Hopefully this image makes sense for what I am trying to do!

我最近的方程尝试使用表达式y = mx + B。

float X = (getY() + 5 - interceptY)/slope; 
float Y = slope*(getX() + 5) + interceptY; 
setPosition(X, Y); 
+1

你可以发布你的最新尝试? – Keppil 2012-07-20 06:59:33

回答

4

能帮你从我最近的比赛的几个公式,代码移动的物体赋予其旋转:

float xDirection = FloatMath.sin((float) Math.toRadians(getRotation())) 
      * currentSpeed; 
float yDirection = FloatMath.cos((float) Math.toRadians(getRotation())) 
      * -currentSpeed; 

float newX = getX() + xDirection; 
float newY = getY() + yDirection; 

你只需要获得中,你需要你的精灵移动的角度这会为你做。希望这可以帮助。

+0

非常感谢你! =)一旦我找到正确的旋转,就可以轻松地工作!用它来设置我的精灵的旋转,它直接插入你的代码! 'setRotation((float)Math.toDegrees(Math.atan2(finalY - getY(),finalX - getX()))+ 90);'偶然使用AndEngine? =) – Gatekeeper 2012-07-20 07:36:21

+0

@Gatekeeper,不客气,很高兴我能帮忙!是的,我正在使用AndEngine。 – Egor 2012-07-20 08:10:29