2D Motion and Vectors

Started by
11 comments, last by timbobsteve 17 years, 8 months ago
This may be a bit more simplistic than it should be but a presentation program I've been working on requires a straight line movement from point A to point B. I allow for the intermediate x and y co-ordinates to be determined independently. What I currently pass to the function that determines its rate of movement is how many cycles it takes to get from point A to point B.

From memory and sort of pidgin coding C:

void Object::SetUpMove (rect A, rect B, unsigned int cycles){   xSlope =  (float)(B.x - A.x)/(float) cycles;   ySlope =  (float)(B.y - A.y)/(float) cycles;   origx = A.x; origy = A.y;   NumCycles = 0;  // start counter at zero   MaxCyles = cycles; }


xSlope and ySlope are member floats of the class as MaxCycles and NumCycles are unsigned int members. I probably set a flag in there specifying that there an active animatiion/move going on.

On each pass to update, I'm assuming that some sort of timing control is goin on I call a function I called Click() against all objects that tells them to check for active animation/moves/whatevers and to do what the object is supposed to do.

bool Object::Click(){   if (!action) return false;   // checks for other actions   // if it shows a linear move......   xpos = origx + (int)(xSlope * NumCycle);   ypos = origy + (int)(ySlope * NumCycle);   NumCycle++;}


This approach relieved me of trying to base the y position on the x position for each cycle and eliminated the trig functions.

The directive to draw the object on the screen comes after all objects have been updated.

As I say, a bit simplistic but it works for my purposes. When it comes to actually doing more precise timing on it I'd likely work out a means of determining 'cycle' to be based on the tic count since starting divided by the total number of tics it should take to get to point B.
Ŀ
Advertisement

So the topics covered here are basically what allow for the creation of Street Fighter, FInal Fantasy 6, and Chrono Trigger/Xenogears?
OK. I got it all to work. It turns out the eratic response was due to the angle(direction) being increased too quickly (e.g. direction + 2 intead of +1) also the speed was a bit too high also.

Thanks for everyone that took the time to reply and help out. For those who want to know what worked for me

// Declare PI to whatever value you use for piconst int PI = 3.1415// You need to write the degrees2radians functionvoid degrees2radians(float angle){    return number/(180/PI);}// Do this in the update for your objectnew_xPosition += cos( degrees2radians( direction ) ) * speed;new_yPosition += sin( degrees2radians( direction ) ) * speed;


Thanks again everyone. No doubt my quest will raise other questions so I may see you all soon :P

EDIT: My objects were moving quite strangely e.g. object velocity = (1,0) at an angle of 45 degrees and speed of 1 unit when it should be (1,1).

This was my own fault. I was holding the positions of each object in a struct like the following:
struct Vector{    int x,y;}


The problem was that the integer values didn't hold onto the decimal values (obviously) and my movement speed would have to be greater than 1 to get proper velocity values.

Thankyou again to everyone that helped. I now am well on my way to understanding basic movement and trig. :D Hooray!

[Edited by - timbobsteve on August 13, 2006 11:54:05 PM]

This topic is closed to new replies.

Advertisement