Straight line between two points at a constant velocity?

Software: Other

OEGInc, Newbie
Posted: 05 April 2014 12:47 AM   Total Posts: 27

I’m sure this has been asked before, but I was unable to find any results searching these forums.  I did find some Google results, but they didn’t make much sense to me. wink

I have two points in 3D space, a starting point and an ending point.  I want to move from starting point to ending point at a constant velocity.  Below “velocity” is ACTUALLY “velocity * timeDelta” in my update frame just for the record.

startPoint = Vector3D(0, 0, 0);
endPoint = Vector3D(123, 456, 789);

Using the formula I found: L = {P + t(Q - P)}

We update our startPoint as follows:
    startPoint.x += (velocity * (endPoint.x - startPoint.x))
    startPoint.y += (velocity * (endPoint.y - startPoint.y))
    startPoint.z += (velocity * (endPoint.z - startPoint.z))

Which works great, EXCEPT - as expected, the closer you get to the endPoint the slower you move… Which is a really cool effect (easing into the endPoint), but not what I am looking for.

I know I could do it if I kept track of the original startPoint and then added a time variable, incrementing the time by the velocity each step - but I was kind of hoping that I could do it in a simpler way without adding any additional variables?

I was thinking of extending the endPoint out in virtual space, but then that adds complexity of calculating when you actually reached your destination among other things…  And the fact remains, the longer the line is, the faster the movement is at the startPoint and slower at the endPoint.

Any ideas?
—Rob

   

Avatar
theMightyAtom, Sr. Member
Posted: 05 April 2014 12:14 PM   Total Posts: 669   [ # 1 ]

Can it be simpler than?

var startPoint:Vector3D = new Vector3D(000);
var 
endPoint:Vector3D = new Vector3D(123456789); 
var 
steps:int 100;
var 
pos:Vector3D;
   
var 
length:Vector3D endPoint.subtract(startPoint);
length.scaleBy(steps);
   
//then loop
   
pos pos.add(length); 

That will also continue indefinitely if you want it to…

Good Luck!

   

OEGInc, Newbie
Posted: 06 April 2014 08:10 AM   Total Posts: 27   [ # 2 ]

Thanks for your reply.  I haven’t tried your code yet, but just from looking at it I don’t see how it is really any different than what I already have.

If you are breaking it up into to steps (100 in your example), then wouldn’t moving 10 units away be significantly slower than moving 1000 units away?

Thanks!
—Rob

   

Avatar
theMightyAtom, Sr. Member
Posted: 06 April 2014 08:13 AM   Total Posts: 669   [ # 3 ]

Try it smile

COnstant Velocity = it moves the same distance with each tick/time interval.

Your code changes the distance every cycle.

   

OEGInc, Newbie
Posted: 06 April 2014 11:24 AM   Total Posts: 27   [ # 4 ]

I see what you are saying, originally I had missed the // loop comment and had thought you were recalculating the distance remaining every cycle.

As I mentioned in my original post, I already know I can do it if I add extra variables (Ie: keeping track of the startPosition in my example, or keeping track of the increment amount in your example). 

I was just wondering if there was a way of doing it without adding extra variables, it seems like there should be…

I plan to have hundreds of objects moving from point A to point B simultaneously and I was trying to keep the code as lean as possible.

   

Mr Margaret Scratcher, Sr. Member
Posted: 07 April 2014 05:56 PM   Total Posts: 344   [ # 5 ]

can you do lookat and moveforward?

   

OEGInc, Newbie
Posted: 17 April 2014 02:17 PM   Total Posts: 27   [ # 6 ]
theMightyAtom - 06 April 2014 08:13 AM

Try it smile

COnstant Velocity = it moves the same distance with each tick/time interval.

Your code changes the distance every cycle.

It’s working now, assuming the same “length”, but short distances the object travels slower, and larger distances the object travels faster.

I’d like the speed to be constant whether the object is moving from 0, 0, 0 to 10, 10, 10 or 0, 0, 0, to 100, 200, 300…

I’m clearly having a brain fart, I *know* it’s not this complicated…

—Rob

 

   

OEGInc, Newbie
Posted: 18 April 2014 09:26 AM   Total Posts: 27   [ # 7 ]

Ok, I figured it out…  Here are the details for anyone who is interested.

// Setup our starting and ending vectors
var startingVector:Vector3D = new Vector3D(000);
var 
endingVector:Vector3D = new Vector3D(100200300);
var 
elapsedTime:Number 0.00;
var 
velocity:Number 25.00;
var 
distanceAdjust:Number;

// Calculate the distance between our two vectors
var distance:Number Math.sqrt(
  ((
endingVector.startingVector.x) * (endingVector.startingVector.x)) +
  ((
endingVector.startingVector.y) * (endingVector.startingVector.y)) +
  ((
endingVector.startingVector.z) * (endingVector.startingVector.z))
);

// Get the inverse of our distance to use for a scaling multiplier
distanceAdjust = (distance);


////////////////////////////////////////
//
// Then in our game loop
//

// Calculate our movement using Cartesian/Euclidean space formula
var xInc:Number = (elapsedTime * (endingVector.startingVector.x));
var 
yInc:Number = (elapsedTime * (endingVector.startingVector.y));
var 
zInc:Number = (elapsedTime * (endingVector.startingVector.z));

// Adjust our game object's position
gameObject.startingVector.xInc;
gameObject.startingVector.yInc;
gameObject.startingVector.zInc;

// Calculate our "facing" rotational angle
gameObject.rotationY = (Math.atan2(xInczInc) * 180.0 Math.PI);

// Increment our current time position in the formula
elapsedTime Math.min(1.00elapsedTime + (velocity timeDelta) * distanceAdjust);

if (
elapsedTime == 1.00)
/* We've reached our destination! */ 

Hope this helps someone!

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X