Computer Science Canada

following a path? help please :D!

Author:  munt4life [ Sun May 01, 2011 9:56 am ]
Post subject:  following a path? help please :D!

Hey i was wondering what i would have to do if im controlling a circle, and there is another circle, how would i get the circle to follow me, i am making a game where theres many obstacles and i was thinking in one part there would be a couple "monsters" chasing me, so depending on where i move they would go towards me as well. Any feedback would be helpful thanks!

Author:  A.J [ Sun May 01, 2011 12:39 pm ]
Post subject:  RE:following a path? help please :D!

There are many ways one could implement enemy's chasing you. One naive way to do so is to have the enemies move towards your position at every iteration; to do this, you measure the angle at which the enemy needs to move at, and increase its x and y position (assuming this is a 2D game) accordingly. The downside of this method is that the enemies don't traverse around obstacles (instead they just try going through them).

To account for obstacles, however, you need to utilize a path finding algorithm. The one I recommend is the A* ('A star') algorithm (I'll leave it to you to read up on it). If you have any questions regarding it, feel free to ask us here.

Author:  munt4life [ Sun May 01, 2011 6:05 pm ]
Post subject:  Re: following a path? help please :D!

im assuming i need trig?

Author:  Insectoid [ Sun May 01, 2011 6:21 pm ]
Post subject:  RE:following a path? help please :D!

Yes. It's fairly simple though; since you're only making right triangles, you can get away with sine law.

Author:  DemonWasp [ Sun May 01, 2011 6:42 pm ]
Post subject:  RE:following a path? help please :D!

Actually, that particular algorithm doesn't even need sine law (and is faster without it). You just need to remember a few things about similar triangles and Pythagorean theorem.

Author:  Raknarg [ Sun May 01, 2011 7:36 pm ]
Post subject:  RE:following a path? help please :D!

or you could use
x + cosd (theta) and
y + sind (theta)

Author:  munt4life [ Sun May 01, 2011 8:37 pm ]
Post subject:  Re: following a path? help please :D!

ok so i tried doing something like
theta := arctan((y1-compy)/(x1-compx))
compx+=speedx(cos(theta))
compy+=speedy(sin(theta))

im getting an error on subscripts im pretty sure my math is right what should i fix?

Author:  A.J [ Sun May 01, 2011 9:02 pm ]
Post subject:  RE:following a path? help please :D!

I thought you wanted to enemies that are chasing you to avoid obstacles. In which case, the method you are currently attempting to code (i.e. the first approach I described in my previous post) doesn't cut it.

Author:  munt4life [ Sun May 01, 2011 9:05 pm ]
Post subject:  Re: following a path? help please :D!

im trying to get the actual moving to work for now then after i will create restrictions i defined all those variables why does turing say speedx and speedy cannot have subscripts.

Author:  A.J [ Sun May 01, 2011 9:24 pm ]
Post subject:  RE:following a path? help please :D!

Even if you have the enemies move towards you at every iteration in this fashion, you can't modify it to avoid obstacles as well; the enemies will just keep trying to come towards you even if there's an obstacle in the way.

Author:  munt4life [ Sun May 01, 2011 9:26 pm ]
Post subject:  Re: following a path? help please :D!

Could you please tell me why my code isnt working please so i could then move on to such a problem Razz

Author:  A.J [ Sun May 01, 2011 9:44 pm ]
Post subject:  RE:following a path? help please :D!

I don't see any code here to help you with/.

Author:  munt4life [ Mon May 02, 2011 5:11 pm ]
Post subject:  Re: following a path? help please :D!

anyone?

Author:  munt4life [ Mon May 02, 2011 5:46 pm ]
Post subject:  Re: following a path? help please :D!

k so now i have this
i tried a similar triangles method.
var bCoordx := x - compx
var bCoordy := y - compy
var d : real := Math.Distance (x, y, compx, compy) % computer finds out input position to determine where to move
var sx := 10 * bCoordx / d
var sy := 10 * bCoordy / d

then i have a bunch of stuff in a procedure blah blah so i have
drawfillbox (compx, compy, compx + 10, compy + 10, red)
how would i make the compx and compy change according to this? im confused please help Very Happy

Author:  Raknarg [ Mon May 02, 2011 5:53 pm ]
Post subject:  RE:following a path? help please :D!

Depends on whether or not you want it to avoid obstacles. Is this the case?

Author:  munt4life [ Mon May 02, 2011 6:20 pm ]
Post subject:  Re: following a path? help please :D!

it seems im just trying to jget the atleast attraction to work for now :p, perhaps later ill try to add such a feature, for now just the chasing is good without obstacles Razz

Author:  Raknarg [ Mon May 02, 2011 6:59 pm ]
Post subject:  RE:following a path? help please :D!

ok, well here's a simple explanation:

Step 1: Find angle
Use arctand ((enemyy - y) / (enemyx - x)). Remember that it changes depending on whether or not the character is above or below the enemy. I would just use something like:
if x > enemyx then
angle := angle + 180

Step 2: Find vx and vy (the amount x and y change)
Easy enough. The formula is:
vx := speed * cosd (angle)
vy := speed * sind (angle)
Then you add those to x and y.

Again, that's a simple explanation. Try to use it, and ask if you have more questions about it. Or someone can try and give a better explanation:P

Author:  munt4life [ Mon May 02, 2011 7:17 pm ]
Post subject:  Re: following a path? help please :D!

what do you mean add to x and y?

Author:  munt4life [ Mon May 02, 2011 7:19 pm ]
Post subject:  Re: following a path? help please :D!

also, yeah i got that much it was just the actual movement of my rectangle( which in this case is the enemy ) that i wanted to move
so i got to the part you got too except im not sure what to put into the drawfillbox(????)

Author:  Raknarg [ Mon May 02, 2011 7:28 pm ]
Post subject:  RE:following a path? help please :D!

oh... ok
You set x and y like so:

x := x + round (vx)
y:= y + round (vy)
Draw.FillBox (x, y, ?, ?, ?)

You can set those last three parameters to whatever you need.
Also, the vx and vy are positive or negative depending on where they are compared to x and y. Such as:

if x < enemyx then
vx := -speed * cosd (angle)
elsif x > enemyx then
vx := speed * cosd (angle)
end if

Author:  munt4life [ Mon May 02, 2011 7:57 pm ]
Post subject:  Re: following a path? help please :D!

+10 bits thanks alot Very Happy

Author:  munt4life [ Mon May 02, 2011 8:03 pm ]
Post subject:  Re: following a path? help please :D!

haha i asctually just got away with the really simple method of

drawfillbox (compx, compy, compx + 10, compy + 10, red)
if (compx < x) then
compx += 5
end if
if (compx > x) then
compx -= 5
end if
if (compy > y) then
compy -= 5
end if
if (compy < y) then
compy += 5
end if

Author:  Raknarg [ Mon May 02, 2011 8:05 pm ]
Post subject:  RE:following a path? help please :D!

XD Yeah, that works too. It's only if you want realistic movement. That works fine.


: