移动对象沿着直线 [英] Move Object along a straight line

查看:169
本文介绍了移动对象沿着直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图沿直线路径移动雪碧。我想它每次我经过的方法,直到我到达终点移动5个像素的斜坡上,或斜边。

我有坡度,线的y轴截距,我也有过的getX()和对的getY精灵当前X和Y值()。最后的X和Y点,停在是变量finalX和finalY。

我已经尝试了这么多的方程,但我似乎无法得到其中的任何工作。我缺少什么!?

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

 浮动X =(的getY()+ 5  -  interceptY)/斜率;
浮Y =斜率*(的getX()+ 5)+ interceptY;
的setPosition(X,Y);


解决方案

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

 浮法x方向= FloatMath.sin((浮点)Math.toRadians(getRotation()))
            * 现在的速度;
浮yDirection = FloatMath.cos((浮点)Math.toRadians(getRotation()))
            * -现在的速度;浮下一页末=的getX()+ x方向;
浮newy指定的getY =()+ yDirection;

您只需要获得中,你需要你的精灵移动的角度,这会为你做。希望这有助于。

I am trying to move a Sprite along a straight line path. I want to move it 5 pixels on the slope, or the hypotenuse each time I go through the method until I reach the end point.

I have the slope and y-intercept of the line, I also have the current X and Y values of the sprite through getX() and getY(). The final X and Y points to stop at are variables finalX and finalY.

I have tried so many equations but I can't seem to get any of them to work. What am I missing!!?

My latest equation was trying to use y=mx+b.

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

解决方案

Can help you with a few equations from my recent game, the code moves an object given its rotation:

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;

You just need to derive the angle in which you need your sprite to move and this will do for you. Hope this helps.

这篇关于移动对象沿着直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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