making character walk by itself

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

making character walk by itself

Post by Vortex »

Hello, im back to coding after taking a long break but i already have something i need help with :shock:

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 :bow:

Dont spoonfeed me, tell me how to do things in theory and ill post if im stilling having trouble
TorteXXX
zapakitul
Chaos Rift Newbie
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

Post 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).
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: making character walk by itself

Post 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?
TorteXXX
User avatar
Bakkon
Chaos Rift Junior
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

Post 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.
User avatar
RyanPridgeon
Chaos Rift Maniac
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

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Bakkon
Chaos Rift Junior
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

Post 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.
User avatar
Vortex
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Sat Nov 15, 2008 1:00 pm

Re: making character walk by itself

Post 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 :)
TorteXXX
User avatar
LuciDreamTheater
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Jan 20, 2009 2:18 am
Location: Southern CA
Contact:

Re: making character walk by itself

Post 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.
Post Reply