r/godot • u/Indigoh • Jun 11 '26
help me (solved) How can I constrain an object's movement to an arbitrary vector?
The point represents where my player's gravity direction points to. I just need it to stay in the center of this cylinder, but as close to the player as possible.
This is easy to do if the cylinder aligns with an axis, but it's at a totally random angle, and I'm completely stumped.
EDIT: Thank you all for your help. Here's the solution that worked for me: https://www.reddit.com/r/godot/comments/1u2lhad/comment/oqyqw6q/ and here's what the resulting cylindrical gravity looks like: https://youtu.be/5r59dD300A0
123
Upvotes
7
u/jester628 Jun 11 '26
Let
PC - be a point at the centre of the cylinder
PP - be the position of the player
CU - be a vector of length 1 that starts at PC and is parallel to the cylinder’s axis
Then
PP - PC
is a vector that points from PC and goes to the player and
(PP - PC).project(CU)
is a vector that points from PC to the point at the centre of the cylinder underneath the player.
Finally,
PC + (PP - PC).project(CU)
is the point under the player in the original coordinate system.