0
votes

I am new to libGDX.I just want a Rectangle or a small curve drawn between the object and the clicked position .I know libGDX has RECTANGLE class but I need to rotate it but the problem is, it gets rotated in center origin and i want to rotate it from its starting position.

I just want to draw a rectangle or a curved line to be drawn between the object and the clicked position like this >>>

enter image description here

Code to get user click position :

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

Code to get the width(distance) between the object and the clicked position :

float abwidth = x1 - position.x;

Code to compute the rotation :

 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 

The above computed degree when put in the following code -- > shapeRenderer.rect(x,y,width,height, 0, 0, abdegree ); is not giving me the perfect angle So what would be a perfect way to rotate the straight horizontal rectangle to the click position.
Or is there any way of achieving this in some other way instead of using rectangle like using curve or something else ?

1
How are you doing your rotation? Where is your MCVE?Kevin Workman
I have not yet rotated the rectangle .We have just calculated the degree of angle we have to rotate it because if we directly write the code like this --> Rectangle rect = new Rectangle(); rect.set(position.x, position.y, width, height); This code will draw a straight rectangle. I just wanted to know is it possible to rotate a rectangle and if yes ,is it possible to rotate it considering the starting position as the origin of rotation because normally it will take its center as its origin for rotation right ?Vishal Nair

1 Answers

2
votes

You can use this class for rendering shapes

and it has rect(float x, float y, float width, float height, float originX, float originY, float rotation) method for drawing rectangles

set originX,originY to 0,0 or other numbers to change rotation origin point