PDA

View Full Version : Vector Velocity Movement



C-Dawg
10-20-2006, 09:30 PM
I'd like an algorithm that will send an FFC shooting out in a given direction at a pre-set speed. It's the math part that's giving me trouble.

INPUT: ( slope of a line, direction, speed )

The slope is just the slope of the line the FFC is to shoot along. The direction is the way it will move along that slope. Specifying East or West should be enough, since it's a single linear line, except in the case of purely vertical movement. Then the speed should be how fast it moves along that line.

BEHAVIOR

The FFC should move along the slope of a line defined by the slope relative to it's starting position. This is fairly easy; x + 1 and y + slope will do it. (Rise over run, as I recall.) But in addition to moving on this line, it should move AT A CONSTANT SPEED, no matter in what direction it's headed. This is the tricky bit I'm having trouble with.

Speed is going to be determined by distance over time, or

Speed = Distance / Time

We'll just talk about one tic, so time = 1. And we use the distance formula to get the distance from our known current FFC positon (x1, y1) to our new position (x2, y2).

Speed = (x1-x2)^2 + (y1-y2)^2

Where the knowns are speed, x1, and y1, and the unknowns are x2 and y2. I can't solve this equation for those variables, so I'm sort of stuck.

I'm probably going about this the wrong way. Saffith, your thoughts?

Saffith
10-20-2006, 10:37 PM
I think I know how to go about this, if I'm understanding you correctly...

Movement at a given angle is easy:
vx = speed * cos(angle)
vy = speed * sin(angle)

The difficulty in this case is in finding the angle, but you should be able to skip that step altogether.
First, construct a triangle:

/|
/ |
slope / | sqrt(slope^2-1)
/ |
/ |
1
Then, of of course, you can find the sine and cosine directly by dividing the sides appropriately. Thus:
vx = speed * 1 / slope
vy = speed * sqrt(slope^2-1)
And you just make those both negative to move the opposite direction. Moving vertically is a special case, obviously, but it's a pretty easy one.