PDA

View Full Version : How to get an object's direction of travel?


Scott Ayers
05-02-2010, 03:40 PM
Me again,
If I'm asking too many questions guys. Let me know and I'll take a break.

I'm trying to get a better grasp on how to use an object's direction of travel in scripts.
As I understand it. The direction of travel is calculated by using the vector of the object's starting position subtracted by it's last known position.
I've seen people write this as using the object's travel history to get it's direction.

So if I wanted to make a cube bounce between Y= -100 & Y= 100.
I would need to create a starting vector to use as the start position. Then create a for() loop to make the cube's position increment. And then use PI to reverse the cube's direction of travel?

And then there's the issue of speed.
I've read that if the speed is too slow. The object will get stuck at the bounce point.
So that means I have to also use speed somehow in this right?

If anyone has any examples of using and manipulating directions like this. I'd love to see them to learn from.

-ScottA

Scott Ayers
05-03-2010, 10:29 PM
I learned a bit more about using vectors to move and rotate objects.
But I'm still stuck on getting, and using, a directional vector to change it's direction of travel.

Here's what I have so far.
I can move the cube fine using vectors. But I'm stuck on reversing it's direction of travel: main(doc,op)
{
var obj = doc->FindObject("Cube");
var pos = obj->GetMg()->GetV0();// get it's Global position
var V = vector(0,0,0); // Create a vector to use for measuring distance traveled
var speed = 15;
var direction = Degree(360);// the initial direction of travel that will be reversed later on
var ReverseDirection;

V.y = pos.y + speed * cos(direction);

if (V.y > 100) // If the cube has reached this limit
{
V.y = 100; // force the object to not exceed 100
ReverseDirection = (3*PI) - direction; // Set the new direction
}

else if (V.y < -100) // If the cube has reached this limit
{
V.y = -100; // force the object to not exceed 100
ReverseDirection = (3*PI) - direction; // Revese the cube's direction
}

obj ->SetPosition(V); // Apply the positions
}

Anyone have any ideas?

-ScottA

tcastudios
05-03-2010, 10:56 PM
For calculating direction I usually store the previous position
or animate the object/document forward (to get next position)

Cheers
Lennart

Scott Ayers
05-03-2010, 11:48 PM
Do I need to make use of the time function to get the second to last frame. Then record the object's position somehow? Or is it done some other way?

I've been using Google to research how to do this. And I've seen that method of storing the object's previous position mentioned many times. But they never show how they actually go about recording it.
They always seem to skim over that part. So I thought I'd try and use trig(Cos) instead.
I'm finding tons of theory out there. But little in the way of practical examples.

-ScottA

CGTalk Moderation
05-03-2010, 11:48 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.