3

I am trying to make a new tool for the tabletop simulator community based on my "pack up bag". The Packup Bag is a tool that remembers world position and rotation of objects you place inside it, so you can then place them back in the same positions and rotations they came from when "unpacking the bag".

I have been trying to modify this so it spits things out in a relative position and rotation to the bag, instead of using hardcoded world coordinates. The idea here is that players can sit at any location at the table, pick the faction bag they wish to play.. drop it on a known spot marked for them and press the place and it will populate contents of the bag relative to its location.

Now I have gotten some of this worked out... I am able to get the bag to place relative in some ways .. but I am finding it beyond my maths skills to work out the modifications of the transforms.

Basically I have this part working..

  • The mod understands relative position to the bag
  • The mod understands relative rotation to the bag

BUT.. the mod dose not understand relative position AND rotation at the same time.... I need someway to modify the position data relative to the rotational data... but can not work out how.

See this video.... https://screencast-o-matic.com/watch/cFiOeYFsyi

As you can see as I move the bag around the object is placed relative to it.... but if I rotate the bag, the object has the correct rotation but I need math to work out the correct position IF it is rotated. You can see it is just getting placed in the same position it was as if there was no rotation... as I haven't worked out how to code it to do this.

Now I have heard of something called "matrix math" but I couldn't understand it. I'm a self taught programmer of only a few months after I started modding TTS.

You can kinda understand what I mean I hope.. In the video example, when I rotate the bag, the object should be placed with the correct rotation but the world position needs to be changed.

See this Example to see relative rotation .... https://screencast-o-matic.com/watch/cFiOeZFsyq

My code dose this by remembering the self.getPostion() of the bag and the obj.Position() of the object getting packed up.. it then dose a self - obj and stores that value for the X and Y position. It also remembers if it is negative or position and then when placing it uses the self.postion() and adds or subtracts the adjustment value. Same for rotation.

Still I do not know what ot go from here.. I have been kinda hurting my head on this and thought maybe some of you math guys might have a better idea on how to do this.

: TL;DR :

So I have

  • bag.getPosition() and obj.getRotation()
  • bag.getRotation(0 and obj.getRotation()

These return (x,y,z}

What math can I use to find the relative position and rotation of the objects to the bag so if I rotate the bag. The objects come out of it in a relative way...

Preferably in LUA.. thank you!

ikegami
  • 367,544
  • 15
  • 269
  • 518
aJynks
  • 677
  • 2
  • 14
  • 27

1 Answers1

1

I'd hope you've found the answer by now, but for anyone else finding this page:

The problem is much simpler than what you're suggesting - it's basic right triangle trigonometry.

Refer to this diagram. You have a right triangle with points A, B, and C, where C is the right angle. (For brevity, I'll use abbreviations opp, adj, and hyp.) The bag is at point A, you want the object at point B. You have the angle and distance (angle A and the length of the hyp, respectively), but you need the x,y coordinates of point B relative to point A.

The x coord is the length of adj, and y coord is the length of opp. As shown, the formulas to calculate these are:

cos(angle A) = adj/hyp
sin(angle A) = opp/hyp

solving for the unknowns:

adj = hyp * cos(angle A)
opp = hyp * sin(angle A)

For your specific use, and taking into account the shift in coordinate system x,y,z => x,z,y:

obj_x_offset = distance * math.cos(bag.getRotation().y)
obj_z_offset = distance * math.sin(bag.getRotation().y)

obj_x_position = bag.getPosition().x + obj_x_offset
obj_z_position = bag.getPosition().z + obj_z_offset

Diagram source: https://www.khanacademy.org/math/geometry/hs-geo-trig/hs-geo-modeling-with-right-triangles/a/right-triangle-trigonometry-review

theCheat
  • 11
  • 1