MouseJointDef libgdx - 绘制像愤怒的小鸟一样的轨迹线 [英] MouseJointDef libgdx - draw a trajectory line like Angry Birds

查看:138
本文介绍了MouseJointDef libgdx - 绘制像愤怒的小鸟一样的轨迹线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在libgdx游戏中

我想触摸然后拖动某处然后在释放(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,但它立即施加一个力(即目标立即移动 - 我希望它保持静止然后一旦触摸事件发生然后施加力量)

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 中,检查上面的标志是否为真,计算触摸位置相对于鸟类拍摄中心的距离和拍摄角度。

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 中,您可以通过调用

    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).

这篇关于MouseJointDef libgdx - 绘制像愤怒的小鸟一样的轨迹线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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