3D vector precise location calculations

85 Views Asked by At

I've asked this question before on stackoverflow but people told me that "It requires some pretty serious trigonometry". I'm just a silly programmer, not that smart that I even know anything about that trigonometry but I'm still willing to learn it if there is no other way. But, learning that would delay my project so much that I'm better of asking it. I know that the site doesn't actually allow people asking for things but at least a description or explanation on how to calculate it would help a lot!

To get to the main question, I've got 4 3D vector locations and the user is allowed to click anywhere between these 4 points. I've managed to get the absolute position by force the user to limit the action to a rotation of 0, 0, 0. The problem is, I don't want to force the user to make their rotation 0, 0, 0. I'll attach a picture to show you a few things, the information that comes with the image is as following:

So lets say we got these coordinates ( might be useful? ):

  • Top left: 2753.8962, -2399.3669
  • Top right: 2757.0393, -2399.3669
  • bottom left: 2753.8962, -2409.8771
  • bottom right: 2757.0393, -2409.8771
  • center: 2755.4746, -2403.3447

The offsets are as following ( offsets from the center to ... ):

  • Top left: -1.578533411026, 3.9779634475708
  • Top right: 1.5644228458405, 3.9779634475708
  • Bottom left: -1.578533411026, -6.5323362350464
  • Bottom right: 1.5644228458405, -6.5323362350464
  • Center: 0, 0 ( of course. )

There is a total rotation possible of 360 degrees. Any help would be very helpful! :)

enter image description here


update: Okay, so I took the answer from BaronVT and changed the calculation to: bx - math.cos ( rx ), by + math.cos ( ry )

It should return a value which hold the offsets from the center ( which is 0, 0 ) but it's calculating a bit wrong I guess. To make the question readable for people;

  • bx = the most outer part of the bounding box ( most left part of the vehicle )
  • by = the most upper part of the bounding box ( top left, together with bx )
  • rx = rotation on the X-axis
  • ry = rotation on the Y-axis

I'm able to get the current position of the vehicle, also where the player clicked ( they are clicking on an element, which returns an X, Y, Z too ).

1

There are 1 best solutions below

12
On BEST ANSWER

I'm not sure if this is what you're asking, but if the top left corner of the unrotated truck is $(-1.58, 3.98)$, then after a counter-clockwise rotation of $\theta$ degrees, the same corner will be at

$$ (-1.58\cos\theta - 3.98\sin\theta, -1.58\sin\theta + 3.98\cos\theta) $$

(i.e. $(x,y)$ winds up at $(x\cos\theta - y\sin\theta, x\sin\theta + y\cos\theta)$, see rotation matrices for more information)

EDIT: after further conversation with OP, we determined that what he wanted to do was, after rotating the truck, click a location on the screen, and then find what location that corresponds to in the unrotated truck. Of course, this is just a rotation by $-\theta$ and after figuring that out, the rest was straightforward.