How to determine that is a node inside the quad?


#1

how to determine that is a node inside the quad?

I have a point (node.get World Translation) and four positions of a quad (vertices[4]).
I want to determine if that point is inside the quad or not. and without using shape collision.

in multi-select by mouse like C&C game.


#2

I assume by “quad” you mean com.jme3.scene.shape.Quad. Correct?


#3

Is your quad axis-aligned?

Even still, this is quite trivial. Translate the world point into quad-local space. See if it’s with the bounds of the quad.


#4

yes.

I don’t know how to do it.

I found this:

but I don’t know how can I calculate the area of a triangle with three given points (x,y).


#5

“I wonder how to translate something from world to local space… let me check the javadoc just in case this is one of those 1 in 10 times that there is something…”

world-to-local…world-to-local… hmmm…

https://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html#worldToLocal-com.jme3.math.Vector3f-com.jme3.math.Vector3f-


#6

ok, how can I calculate the area of a triangle with three given points (x,y)?


#7

Why do you need to do that? Don’t you already know how big your quad is?

In local space,
if x < 0 and x > width… it’s not in the quad.
if y < 0 and y > height… it’s not in the quad.


#8

When someone helps you with an issue, you should communicate your thanks. This is a special case of “Treat the list like people”. For more tips, see the “How to get answers” link in the left sidebar of this Forum:
https://www.mikeash.com/getting_answers.html#guide_begin

    /**
     * Calculate the area of the specified triangle.
     *
     * @param triangle (not null, unaffected)
     * @return the area (&ge;0)
     */
    public static double area(Triangle triangle) {
        Vector3f a = triangle.get1();
        Vector3f b = triangle.get2();
        Vector3f c = triangle.get3();

        Vector3f ab = b.subtract(a);
        Vector3f ac = c.subtract(a);

        Vector3f cross = ab.cross(ac);
        double areaSquared = lengthSquared(cross) / 4.0;
        double area = Math.sqrt(areaSquared);

        return area;
    }

    /**
     * Calculate the squared length of a vector. Unlike
     * {@link com.jme3.math.Vector3f#lengthSquared()}, this method returns a
     * double-precision value for precise comparison of lengths.
     *
     * @param vector input (not null, unaffected)
     * @return the squared length (&ge;0)
     */
    public static double lengthSquared(Vector3f vector) {
        double xx = vector.x;
        double yy = vector.y;
        double zz = vector.z;
        double result = xx * xx + yy * yy + zz * zz;

        return result;
    }

#9

@yn97

Look into this method:

FastMath.pointInsideTriangle

#10

I don’t get why you do (x^2)/4 then take the square root of that. You just square then sqrt. The same as just taking abs( cross / 2), which is a property of the cross product. Abs(cross) is equal to the area of the parallelogram formed by the two vectors.


#11

The cross product is a vector. To compute the abs() of a vector, one must extract a square root.


#12

Sorry I must’ve been out of my mind last night :sweat_smile:


#13

No worries. I appreciate when people study my code and offer constructive feedback.