Skip to main content
GameDev.net gamedev.net
🔒 Locked

Character movement.

Started by Muzlack Apr 13, 2002 at 11:23 PM 6 replies 300+ views
Original Post
Muzlack
Muzlack
Ok, I''ve asked this before, and I got pretty close to getting it right, but I''m still a little confused. I''m trying to make a character move to the exact point that you click on the screen. what I do is I divide the y/x like the whole rise over run formula, then, I add that to the y and add 1 to the x, this doesn''t work correctly. why not?
--Muzlack
Muzlack
Muzlack
Let me describe a little more clearly exactly what I want. I have a character in a bitmap room. I want the character to maintain the same pace moving in a perfect line to the point where you click on the screen.
--Muzlack
Nibbles
Nibbles
can''t explain the math details to well off the top of my head, but find the angle between the character, and destination (basic algebra), and use sin/cos crap to move in the direction of the angle until the distance between the two points is 0 or close to.


------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------
Oluseyi
Oluseyi
You''re adding 1 to the x. Either interpolate over the entire span (x and y) or increment by a fixed velocity along the vector of motion.

See 2D interpolation for a quick explanation of interpolation (which I think is the better method).

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
orion power
orion power
I need a 2d engine that is powerful and high quility!
Muzlack
Muzlack
Hmm.. I''m still a little confused. What do i get this float f from? (from the other thread).
--Muzlack
Muzlack
Muzlack
I got this code from a book, why doesn''t it work? :

  
void swapTermsInt(int *a, int *b) {
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void ApproxNextX(int dirVector) {
if(errorterm>=0)
{
xinc = stepx;
errorterm +=k2;
} else
errorterm +=k1;
yinc=stepy;
}
void ApproxNextY(int dirVector) {
if(errorterm >=0)
{
yinc = stepy;
errorterm +=k2;
} else
errorterm +=k1;
xinc=stepx;
}

bool GetSlope() {
int dx, dy;

int y1=ego.y;
int y2=move2.y;
int x2=move2.x;
int x1=ego.x;

dx=x2-x1;
dy=y2-y1;
if(dx >0) {
stepx=3;
} else
{
dx=-dx;
stepx=-3;
}
if(dy >0) {
stepy=3;
} else {
dy=-dy;
stepy=-3;
}
if(dx>dy) {

k1=dy<<2;
k2=k1-(int)(dx<<2);
errorterm=k1-(int)dx;
ApproxNextY(dy);
return true;
}
else {

k1=dx<<2;
k2=k1-(int)(dy <<2);
errorterm=k1-(int)dy;
ApproxNextX(dx);
return false;
}
}


and then each time I get a new slope, it checks to see if it returned false or true, and approxNextX or Y accordingly..
--Muzlack

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.