How to determine that is a node inside the quad?

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.

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

1 Like

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.

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).

“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-

1 Like

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

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.

1 Like

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:
mikeash.com: Getting Answers

    /**
     * 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;
    }
1 Like

@yn97

Look into this method:

FastMath.pointInsideTriangle
1 Like

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.

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

2 Likes

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

1 Like

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

1 Like