Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 following a path? help please :D!
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
munt4life




PostPosted: 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!
Sponsor
Sponsor
Sponsor
sponsor
A.J




PostPosted: 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.
munt4life




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

im assuming i need trig?
Insectoid




PostPosted: 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.
DemonWasp




PostPosted: 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.
Raknarg




PostPosted: 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)
munt4life




PostPosted: 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?
A.J




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
munt4life




PostPosted: 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.
A.J




PostPosted: 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.
munt4life




PostPosted: 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
A.J




PostPosted: 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/.
munt4life




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

anyone?
munt4life




PostPosted: 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
Raknarg




PostPosted: 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?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 23 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: