MouseJointDef libgdx - 像愤怒的小鸟一样画一条轨迹线
在 libgdx 游戏中
in libgdx game
我想 touchDown 然后拖动某处,然后在释放 (touchUp) 时根据与目标主体的距离和方向施加方向力.当您触地时,目标身体保持静止,然后在触地时沿所需轨迹施加力.
I want to touchDown and then drag somewhere and then on the release (touchUp) apply a directional force based on the distance and direction from the target body. When you touchdown the target body stays still and then on touchup the force is applied along the desired trajectory.
(非常类似于愤怒的小鸟——当你拿着弹弓时,你可以看到目标身体的虚线轨迹——我也想这样做)
(very similar to Angry birds - where you get to see the trajectory in dotted lines for the target body when you hold hack the slingshot - I want to do the same)
所以我想这可能不是最难做的事情,但考虑到一些选项,我倾向于使用 MouseJointDef,但它会立即施加力(即目标立即移动 - 我希望它保持静止,然后一旦touchup 事件发生然后施加力)
So I guess that this might not be the hardest thing to do but given a few options Im leaning towards using a MouseJointDef but its an immediate force applied (i.e. the target moves immediately - I want it to stay still and then once the touchup event happens then apply the force)
绘制轨迹最简单的方法是什么?我也在使用 Box2D.
Whats the easiest way to draw the trajectory also? Im using Box2D also.
推荐答案
创建一个继承 InputAdapter
类的类,然后创建它的一个实例并注册它来监听触摸输入.
Create a class that inherits InputAdapter
class, then create an instance of it and register it to listen the touch inputs.
Gdx.input.setInputProcessor(inputAdapter);
有 3 种方法可以处理您必须覆盖的触摸事件 touch_down
、touch_dragged
和 touch_up
.
There are 3 methods to handle the touch events touch_down
, touch_dragged
and touch_up
that you have to override.
在touch_down
中,检查触摸位置是否在鸟区.如果是,则将布尔标志设为 true.
In touch_down
, check the touching position to whether is in the birds area or not. If it is, make a boolean flag true.
在touch_dragged
中,检查上面的flag,如果为真,计算touch位置相对于小鸟射击中心的距离和射击角度.
In touch_dragged
, check the flag above and if it was true, calculate the distance of the touch position relative to the bird shooting center and the shooting angle.
在touch_up
中,您可以通过调用来订购计算的数量进行拍摄
In touch_up
, you can order to shoot with the calculated amounts by calling
body2shoot.applyLinearImpulse(impulse, body2shoot.getWorldCenter());
不需要MouseJointDef
来移动body2shoot
.只需将 body2shoot
的变换设置在触摸位置,以便在每个渲染周期中拖动即可.
There is no need to MouseJointDef
to move the body2shoot
. Just set the transform of body2shoot
in touching position to be dragged in each cycle of render.
为了计算轨迹,我写了一个这样的类:
For calculating the trajectory I wrote a class like this:
public class ProjectileEquation
{
public float gravity;
public Vector2 startVelocity = new Vector2();
public Vector2 startPoint = new Vector2();
public ProjectileEquation()
{ }
public float getX(float t)
{
return startVelocity.x*t + startPoint.x;
}
public float getY(float t)
{
return 0.5f*gravity*t*t + startVelocity.y*t + startPoint.y;
}
}
为了绘制它,我设置了 startPoint
和 startVelocity
然后在循环中我逐步给出一个 t
(时间)并调用getX(t)
和 getY(t)
.
and for drawing it just I set the startPoint
and startVelocity
and then in a loop I give a t
(time) incrementally and call getX(t)
and getY(t)
.
相关文章