3

I'm looking for the math that give me the direction in which i'm crossing a body. I'm out of ideas, I know it must be very simplistic application of trigonometry but I tried various things without success.. Not sure what is the best approach.

Here the details:

enter image description here

So I have a body that is rotated at a given angle and I need to find out if i'm entering the body from it's back or from it's front. The trajectory in this use-case is defined by the mouse position (on move), therefore I can draw a line from the previous Point and the current Point.

Hope someone can help me with this:) & Thanks very much in advance.

Yaniv De Ridder
  • 773
  • 1
  • 7
  • 13

2 Answers2

2

This can be solved using a few vector operations. First of all, construct a vector representing the movement.

movement = currentPoint - previousPoint

Then to see whether it aligns with the orientation of the body (specified by 'normal', the normal vector on the front of the body), just use the dot product: if movement dot normal > 0, movement is in the direction of the normal on the front, so it hits the body from the back. Otherwise it hits the body from the front.

Note that we do not normalize any vectors, that is not needed when you just want to know whether it hits the object from back or front. A nice bonus is that when you normalize movement, (I assume that normal is already normalized), the dot product between movement and normal represents the incident angle: angle = arccos(movement dot normal)

Edit

This approach handles a relative movement so you still need to test for intersection with the object to know whether you hit it at all. To achieve this, test for line-object intersection. (Depends on your object so I can't really say a lot more about this.)

TaZ
  • 743
  • 7
  • 15
0

I think this answer and the link provided should help. The answer itself deals with determining whether 2 segments intersect, and the link explains how to determine whether a point is on the left or the right of a segment.

Unless I am mistaken, the first part would tell you whether your current trajectory crosses the body, and the second part would tell you whether your current point is on the left or right of the body "vector", which given your drawing should tell you whether you collided from the front or the end.

Community
  • 1
  • 1
Mathias
  • 15,191
  • 9
  • 60
  • 92