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
making character walk by itself
Moderator: Coders of Rage
making character walk by itself
TorteXXX
-
- Chaos Rift Newbie
- Posts: 15
- Joined: Sat Jun 20, 2009 8:05 pm
- Current Project: Swift
- Favorite Gaming Platforms: PC, SNES, DS
- Programming Language of Choice: C
- Contact:
Re: making character walk by itself
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:
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).
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.
Re: making character walk by itself
after a little thinking ive made this function
works pretty good, looks a little robo´ish but thats fine for now
any comments on my code?
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; }
}
}
any comments on my code?
TorteXXX
- Bakkon
- Chaos Rift Junior
- Posts: 384
- Joined: Wed May 20, 2009 2:38 pm
- Programming Language of Choice: C++
- Location: Indiana
Re: making character walk by itself
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.Vortex wrote: works pretty good, looks a little robo´ish but thats fine for now
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: making character walk by itself
I don't see how it would need trig; only a bit of division and pythag maybe.Bakkon wrote: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.Vortex wrote: works pretty good, looks a little robo´ish but thats fine for now
- Bakkon
- Chaos Rift Junior
- Posts: 384
- Joined: Wed May 20, 2009 2:38 pm
- Programming Language of Choice: C++
- Location: Indiana
Re: making character walk by itself
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
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
its acutally pretty funny to play around with and make theese little guys follow you
TorteXXX
- LuciDreamTheater
- Chaos Rift Newbie
- Posts: 39
- Joined: Tue Jan 20, 2009 2:18 am
- Location: Southern CA
- Contact:
Re: making character walk by itself
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.
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.