Background
I am creating a game in which you can control multiple people in a community, so obviously, the people need to be able to move on their own, with their own tasks to accomplish. I used the method described
Question
How do I make them all move at the same speed? And if you have a better solution on how to move a mesh from point to point then please tell me how.
PS: In the post I linked, I used the "fancy movement" part of the answer.
CodePudding user response:
(In the event the fiddle goes away, the previous answer used a slowly incremented variable with path.getPointAt(position)
to move the objects along the path.)
The Curve.getPointAt
method takes a value that is a percentage along the curve. Because of this calculation, the distance between points (for example 0
and 0.1
) will be different based on the length of the curve.
So if curve1
is 10 units
long, and curve2
is 100 units
long, then:
curve1.getPointAt(0.1) // vector 1 unit from the start of curve1
curve2.getPointAt(0.1) // vector 10 units from the start of curve2
If you're basing your motion on this, then basically the object moving along curve2
will move 10x
as fast as the object moving along curve1
.
What you want to do instead is determine how many steps the object should take along a path, given a certain "steps/second" or "steps/frame". This is more aligned to the concept of a real physical velocity, rather than one defined by the length of the path being taken.
Let's say you want to travel at 2 units per frame
along curve1
and curve2
. Now that you're traveling at a fixed rate, you just need to divide the rate by the total length to get the number of "steps" (or in this case, frames) it will take to reach the end of the path.
2 / 10 = 0.2 // this is the point polling percentage for curve 1
2 / 100 = 0.02 // this is the point polling percentage for curve 2
Now, when you move an object along curve1
, you will need to use path.getPointAt(0.2)
, and for curve2
you will use path.getPointAt(0.02)
.
Looking ahead a little, you might ask what happens when the math doesn't line up outside a perfect example, and you have extra "partial steps" to make it to the end of the curve. Well, that's up to you to decide how to handle it. You could spend an extra frame to finish the motion, or you could look a step ahead during the movement calculation and decide to just send it to the end.