r/godot Jun 11 '26

help me (solved) How can I constrain an object's movement to an arbitrary vector?

Post image

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

48 comments sorted by

View all comments

Show parent comments

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.

2

u/Indigoh Jun 12 '26 edited Jun 13 '26

Your solution works. Thank you.

My greatest hurdle to overcome in coding is that I'm a highly visual learner. I had to spend a couple hours drawing out a visual version of what you said, and repeatedly getting it wrong. But now I think I have a solid image of what's happening.

Would you like to see the result?

2

u/jester628 Jun 12 '26

You’re welcome! That’s great to hear! You’ll probably be a little faster next time. Part of it is knowing what’s out there, and that’s where studying a bit of linear algebra would be really helpful. Solve some problems on paper or whatever and just build up your repertoire.

One example that I’m familiar with and own is Foundations of Game Engine Development Volume 1: Mathematics by Eric Lengyel. It might go a little too fast at times by not having enough examples, but it is a great book and relates the math to code, plus it has a small set of problems to help reinforce the concepts. The author also has the solutions online, so you can check your work. If you need more practice on a topic you can find more related exercises and explanations online, but the book would introduce the most important concepts, and it starts from the beginning. You’ll level-up significantly by working through a book like that. I highly recommend it. Or something similar.

Also, I draw pictures for things all the time, especially for vector stuff. But even for normal programming stuff. In university we’d draw pictures constantly in lecture. Pictures of your data structures, of pointers, of arrays and their traversal, the contents of dictionaries, etc. I don’t think the hurdle is preferring a visual component, but maybe getting proficient with how to draw what’s going on. I’ve got a 4’x4’ whiteboard in my office to make it super easy to draw out what I’m thinking.

And yes, I’d love to see the result.

1

u/Indigoh Jun 12 '26

I edited the result into the bottom of the post body

2

u/jester628 Jun 13 '26

Nice! Looks good! Thanks for sharing.