Page 1 of 1
making character walk by itself
Posted: Sun Jun 21, 2009 10:17 am
by Vortex
Hello, im back to coding after taking a long break but i already have something i need help with
Lets say i want to make a character that the player cant controll walk an certain point on the screen,
lets say the coordinates are: x:300 y: 200 how whould i implement this?
i need to create a function that takes 2 parameters and then sets the character to walk to that point like: walk(300,200);
do i need to look into pathfinding? i really have no idea how to implement this please help
Dont spoonfeed me, tell me how to do things in theory and ill post if im stilling having trouble
Re: making character walk by itself
Posted: Sun Jun 21, 2009 10:27 am
by zapakitul
You can position 2 "objects"! Lets say you have the NPC (N), Object A and object B, and you want him to go from A to B! You do something like this:
Code: Select all
Point object N to Object B coordinates. Move object N.
If object N is close to object B, point object N towards object A.
If N is close to A, point him towards B.
Edit: Also, you could make him go in circles! Like move your NPC forward, and keep on rotating him(like rotate object's angle by his angle ammount +1).
Re: making character walk by itself
Posted: Sun Jun 21, 2009 10:37 am
by Vortex
after a little thinking ive made this function
Code: Select all
void citizen::walk(int xCor,int yCor)
{
if(x<xCor)
{ xVel=1; }
if(x>xCor)
{ xVel= -1; }
if(x==xCor)
{ xVel=0;
if(y>yCor)
{ yVel= -1; }
if(y<yCor)
{ yVel=1;}
if(y==yCor)
{ yVel=0; }
}
}
works pretty good, looks a little robo´ish but thats fine for now
any comments on my code?
Re: making character walk by itself
Posted: Sun Jun 21, 2009 1:02 pm
by Bakkon
Vortex wrote:
works pretty good, looks a little robo´ish but thats fine for now
Yeah, the way you have it now, it'll walk diagonally until it's horizontally or vertically aligned with its target, then walk straight. If you want one perfect line from point A to point B, you need to do a little trig to find the X and Y speed he'll need to travel.
Re: making character walk by itself
Posted: Sun Jun 21, 2009 1:15 pm
by RyanPridgeon
Bakkon wrote:Vortex wrote:
works pretty good, looks a little robo´ish but thats fine for now
Yeah, the way you have it now, it'll walk diagonally until it's horizontally or vertically aligned with its target, then walk straight. If you want one perfect line from point A to point B, you need to do a little trig to find the X and Y speed he'll need to travel.
I don't see how it would need trig; only a bit of division and pythag maybe.
Re: making character walk by itself
Posted: Sun Jun 21, 2009 1:24 pm
by Bakkon
Ehh, yeah. I've been working with rotation all afternoon so I have sin/cos on the brain. I envisioned a triangle and jumped straight on the angles. Yeah, it can all be done with just similar triangles.
Re: making character walk by itself
Posted: Sun Jun 21, 2009 1:53 pm
by Vortex
ill maybe try to make it walk straightly to the target point later but for now this will do,
its acutally pretty funny to play around with and make theese little guys follow you
Re: making character walk by itself
Posted: Sun Jun 21, 2009 2:16 pm
by LuciDreamTheater
I, personally, would look up some pathfinding algorithms. There's a collection here:
http://www-cs-students.stanford.edu/~am ... html#paths .
Edit: The motivation for using such algorithms is for situations with obstacles between point A and point B. The way you have it set up, your characters might get stuck on a tree stump or something.