[SOLVED] Some of math help


#1

Hello.

  1. lets say i got plane in 3d in any rotation and any location.
  2. it also have some width and height.(because its a plane or can be named 2Dbox, like in 2d)
  3. i raycast it receiving contact point.

First question: How can i receive 2d point(or percent values of width and height) of this plane from this contact point?

what myself i invent(not a good solution ;p) was:

    Vector3f minTranslation = element.getWorldTranslation(); // start point of plane
    Vector3f xMaxTranslation = element.getWorldXTranslation(); // end x point of plane
    Vector3f yMaxTranslation = element.getWorldYTranslation(); // end y point of plane
    float minDistance = minTranslation.distance(lastContactPoint); // contact point distance to start plane
    float percentX = minDistance / (minDistance + xMaxTranslation.distance(lastContactPoint));
    float percentY = minDistance / (minDistance + yMaxTranslation.distance(lastContactPoint));

it almost work, but it dont because ofc when point will be at width 100% and height 100% it will calculate like 50% and 50%. when point will be at width 100% height 0% it will be correct, same correct for point in 0% and 0%.

ofc i need closest point to width or height vector line to make distance work correctly.

So Second question: how can i receive closest point to other vector line. (so it might fix my solution)


#2

Two of the sides of your box can be represented by vectors. They share a corner. Call them side1 and side2… common point p in world space.

Given: the world space contactPoint.

Vector3f relative = contactPoint.subtract§;
float x = side1.normalize().dot(relative);
float y = side2.normalize().dot(relative);

If you want x and y as 0-1.0 based on side length then divide by the sides:
x = x / side1.length(); // if side length not zero
y = y / side2.length();

…x, y is the 0-1.0 coordinate on the 2D quad.


#3

thanks. so to fix my solution i just need normalize and use dot product from this side vectors.

i thought there will be much easier way check this 2d point anyway, but im glad it will work even this way.

thanks again :slight_smile: will do it.

edit: works like miracle, thanks again :slight_smile: