如何在libGDX中的两个坐标之间绘制矩形或曲线

时间:2014-03-25 12:54:53

标签: libgdx game-engine

我是libGDX的新手。我只想在对象和点击位置之间绘制一个矩形或一条小曲线。我知道libGDX有RECTANGLE类但我需要旋转它但问题是,它在中心原点旋转我想从它的起始位置旋转它。

我只想在对象和点击位置之间绘制一个矩形或曲线,如下所示>>>

enter image description here

获取用户点击位置的代码:

int x1 = Gdx.input.getX();
int y1 = Gdx.input.getY();

获取对象与点击位置之间的宽度(距离)的代码:

float abwidth = x1 - position.x;

计算轮换的代码:

 float f1 = Math.abs(y1 - position.y);
 float f2 = Math.abs(x1 - position.x);
 abwidth = Math.abs(abwidth);
 float abdegree = Math.toDegrees(Math.atan((f1)/(f2)));
 abdegree = abdegree * (-1);//done this because it was giving the opposite rotation i dont know if this is wrong but it made the angle upwards 

以下代码中的上述计算程度 - > shapeRenderer.rect(x,y,width,height, 0, 0, abdegree );没有给我一个完美的角度那么将直线水平矩形旋转到点击位置的完美方式。
或者有没有办法以其他方式实现这一点,而不是像使用曲线或其他东西一样使用矩形?

1 个答案:

答案 0 :(得分:2)

您可以使用this类来渲染形状

它有 rect(float x, float y, float width, float height, float originX, float originY, float rotation) 绘制矩形的方法

将originX,originY设置为0,0或其他数字以更改旋转原点

相关问题