Libgdx, how can I create a rectangle from coordinates?

I am currently trying to make a selector box for an RTS game. To do this, I need to drag the mouse to create a selection box, however this can lead to a negative length / width.

Is there a way in Libgdx to make a rectangle using only two sets of coordinates?

Thanks.

0
source share
1 answer

This is a simple idea if I understand what you want to do:

to create a rectangle that you can use, Rectangle(float x, float y, float width, float height) for more information you can read it here http://libgdx.badlogicgames.com/nightlies/docs/api/com/ badlogic / gdx / math / Rectangle.html

this psuedo code is more or less:

create a listener that captures keystrokes, mouse or them, if necessary,

on landing, catches x, y and assigns a:

 yourVariableTouchDown.x = x; yourVariableTouchDown.y = y; 

then, when touchup captures the execution of x, and the point at which it dials the contact and assigns:

 yourVariableTouchUp.x = x; yourVariableTouchUp.y = y; 

after creating the retractor:

 private Rectangle yourRectangle = new Rectangle(); yourRectangle(yourVariableTouchDown.x, yourVariableTouchDown.y, (yourVariableTouchDown.x - yourVariableTouchUp.x), (yourVariableTouchDown.y - yourVariableTouchUp.y)); 

if you want to see it, you can use ShapeRenderer: see http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html

add for the test to the variable class

 private ShapeRenderer sRDebugRectangel = new ShapeRenderer(); 

add update or drawing for testing

 sRDebugRectangel.begin(ShapeType.Filled); sRDebugRectangel.identity(); sRDebugRectangel.rect(yourRectangle.getX(), yourRectangle.getY(), yourRectangle.getWidth(), yourRectangle.getHeight()); sRDebugRectangel.end(); 

you can look at using this listener: https://www.google.es/#q=listener+libgdx

PS: what you say negatively will be checked when touchup is less than a touchdown change, when a rectangle is created that was exactly what I happened to be with you, tested it and adjusted the variables to create the rectangle now, because you cannot be created desirable when negative, now I have time to get with it, in fact, and did not check it, why I said it was a pseudo-code, I hope that you serve, the idea

PS: You can also watch https://stackoverflow.com/tour

0
source

Source: https://habr.com/ru/post/988256/


All Articles