Libgdx, how can I create a rectangle from coordinates?

◇◆丶佛笑我妖孽 提交于 2019-12-12 02:06:38

问题


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

In Libgdx is there a way to make rectangle from just using 2 sets of coordinates?

Thanks.


回答1:


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

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

this a psuedo code more or less:

create a listener that captures keystrokes, mouse or them as appropriate,

in touchdown catches x, y, and assign a:

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

then when the touchup captures the x is executed, and the point where it makes up touch and assign a:

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

after create the rectagle:

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: look this http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html

add for test in variable class

private ShapeRenderer sRDebugRectangel = new ShapeRenderer();

add for test in update or draw

sRDebugRectangel.begin(ShapeType.Filled);
sRDebugRectangel.identity();

sRDebugRectangel.rect(yourRectangle.getX(), 
                      yourRectangle.getY(),
                      yourRectangle.getWidth(),
                      yourRectangle.getHeight());

sRDebugRectangel.end();

you can look on that listener use: https://www.google.es/#q=listener+libgdx

P.S: what you say negative, will be a matter of check when touchup is less than touchdown change where the rectangle is created that was just what I happened to you have test it and adjust the variables to create the rectangle now because you can not be created desirably when negative, now I have time to get with it, in fact eh not tested this why I said it was pseudo code, hope you serve, idea

P.S: You can also look at this https://stackoverflow.com/tour



来源:https://stackoverflow.com/questions/28125125/libgdx-how-can-i-create-a-rectangle-from-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!