Find point in right triangle with given one vector and one point

54 Views Asked by At

I am developing a game where the user move a car with his finger. The car is represented as vector (one point and angle of rotation in the screen). When the user start to dragging the car he generate a new point P. I am trying to smooth the moving of the car so it won't change its angle if the point is somewhere +/- 10px which is the margin of the car (The car is represented as rectangular). Here is an image for better explaining: enter image description here

How to find the point M(x,y)?

(Please note, the angle α is the rotation of the car, not the angle of point O and P)

1

There are 1 best solutions below

0
On

Let $\hat{\alpha}$ be the unit vector from $O$ along the $\alpha$ direction: $$\hat{\alpha} = ( \cos \alpha, \sin \alpha ) \;.$$ Then your point $M$ is given by $$ ((P-O) \cdot \hat{\alpha}) \; \hat{\alpha} \; + O .$$ That is, project $P$ onto $\hat{\alpha}$ (via the dot product) to get the length, and then just scale $\hat{\alpha}$ by that length. Finally, shift by $O$ (unnecessary if $O=(0,0)$).