Strategy selection box?

I am wondering if it is possible to create a way to select multiple object using a selection box? For example in strategy games you can hold and drag your mouse to draw a selection box. And everything inside the box is selected. Would it be possible to draw a box on the mouse and then for each point in the box perform a ray cast? Or is that too processing intensive?

If there is any examples of this could you please post a link?

Thank you very Much for your Time :slight_smile:

I would get the center of each object, call cam.getScreenCoordinates() on them, and check if they are within the rectangle. Although there’s probably some other ways

1 Like

Thank you thats a better solution. SO how would i go about drawing the square? Any tutorials?

I would just use a Quad mesh with a material that makes it look like a selection box and put it on the guiNode.

the x/y position of the quad would be where the mouse started to drag, the width and the height of the quad would be the current mouse coordinates.

1 Like

I have created the quad like so:

public void createSelector() {
Mesh mesh = new Mesh();
Vector3f[] vertices = new Vector3f[4];
vertices[0] = new Vector3f(0, 0, 0);
vertices[1] = new Vector3f(3, 0, 0);
vertices[2] = new Vector3f(0, 3, 0);
vertices[3] = new Vector3f(3, 3, 0);

        Vector2f[] texCoord = new Vector2f[4];
        texCoord[0] = new Vector2f(0, 0);
        texCoord[1] = new Vector2f(1, 0);
        texCoord[2] = new Vector2f(0, 1);
        texCoord[3] = new Vector2f(1, 1);

        int[] indexes = {2, 0, 1, 1, 3, 2};

So how do i make it initialize that the mouse pos and how do i add it to the gui node?
Im really new to all this im sorry :confused:

use inputManager.getCursorPosition() to get the current x/y coordinates of the mouser (do this inside of on ActionListener that listens for the mouse left button to be pushed)

btw you dont need to make your own quad. jmonkey already has its own quad you can just use:

the width/height of the quad would be the current values from inputManager.getCursorPostion() (that you fetch inside of your update loop while the mouse left is held down)

and the local translation of the geometry would be the cursor when you first clicked the mouse left button.

1 Like

Awesome thanks. Only problem left is that My input manager is in a different class. So how can i access the gui Node in my main class from my input manager class?

before someone less nice than me answers :slight_smile: it seems you need a bit more understanding of References in java. You can pass it as an argument to a method of your other class, or use a static getter, or what ever :slight_smile:

1 Like