Computer Science Canada [Tutorial] "Perfect" Following |
Author: | The_Bean [ Tue Mar 25, 2008 9:12 pm ] | ||||||||||||
Post subject: | [Tutorial] "Perfect" Following | ||||||||||||
After watching classmates make countless game where you are being chased by something and have to shoot at it, and my recent Geometry Wars game, I have decided to show you how to make those enemies follow perfectly. Warning: Math Ahead It is advised to have Grade 11 academic math before attempting to understand this, but if you think you can do it, go ahead. Part: 1 - What to get away from! We all know how to make a ball bounce off the wall. Now people often apply the same idea in trying to get an enemy to follow you or your mouse around the screen. This is really primitive, but in tile based games works just fine. But we're not here to talk about those. I find alot of people using the simple if my x position is greater then my enemies then my enemies += their speed same thing with the y and in the opposite direction. But doing this makes your enemy only able to travel: N NE E SE S SW W NW. This is a simple program of that:
Now that you know what you use to be doing and what were getting away from lets get to "Perfect" Following Part: 2 - The Math In grade 11 math you go deeper into trig and how to use it. That is exactly what we are going to do here. (I'm going to * as the degree symbol to make it easier and shorter for me to write) Firstly if you noticed before the N NE E... are at 0*, 45*, 90* ... so when trying to get something to follow you perfectly it needs to travel in more degrees than just those. We need to find the degree from your enemy to you Since were working on the x,y plane we know that tan Angle =y/x gives you the degree, but the problem is that the origin is in the bottom left and we need the origin to be over the enemy so he will (0,0) and your guy or mouse will be (x,y) so to do this we subtract the enemies x and y from your x and y so your coordinates compared to your enemy are (mouseX-enemyX,mouseY-enemyY). so now we can use tan=y/x now to put this in turing we need the angle on one side an everything on the other. Angle := tan inverse y/x and in turing Angle := arctand (y/x) now putting this in a function
But we can't use this just yet! First there are a couple other things that we need to take care of. When x=0 meaning that you and the enemy are on the same x coordinate you will have y/0 and we know that you can't divide by 0 so when need to take catch that in the function.
But we still aren't done! For some reason when y=0 turing can't decide what to do and sometimes he travels away form you and sometimes hes goes towards you. So I put the case where y=0 in also.
And once again still not done. Doesn't this seam alot easier in class. The power of the Brain WOW! We also have to consider that tan only gives us something between 90 and -90 so we need to see whta quadrent it is in and then make the necessary change to accomadate.
YAY Your function for getting the angle to which you are from your enemy is now done. Now to use that angle. If you remember form class. You derived from that nice little triangle that to get your (x,y) from a given angle and radius you used: x= r * cos Angle y= r * sin Angle Now we will use r (radius, the hypotinues of the triangle) as the speed of the enemy (how fast he travels around the screen in pixels) and the Angle is what we did before. the x and y you get are the amount that the enemy travels in that given direction. So you add that to their current (x,y) and get their new (x,y) r distance away at the Angle. Now to put those in turing your x and y need to be integers remember that. enemyX+= round(cosd(Angle)*enemySpeed) enemyY+= round(sind(Angle)*enemySpeed) Now put that into a nice little program add a cool tail a simple game aspect to it and you get something like this:
Now fix up those old basic games you use to make, and know that your enmys no longer look stupid. |
Author: | Mackie [ Tue Mar 25, 2008 10:09 pm ] |
Post subject: | RE:[Tutorial] "Perfect" Following |
This is an excellent tutorial! It's very infomative, and you communicate your ideas very well. +20 bits. |
Author: | zylum [ Wed Mar 26, 2008 1:03 am ] |
Post subject: | Re: [Tutorial] "Perfect" Following |
The information presented is very useful, good job! I would however change the title of the tutorial. It describes the thought process of how to derive a function that gives the polar angle between two points, which has many applications, not only enemy following. FYI, there is a simpler method for enemy following using similar triangles. Maybe you can make a second part describing it Also, just to clean things up a bit and make it a 'proper' tutorial, read the tutorial about how to make good tutorials ie. using headings and color and such to make it more visually appealing. + Karma |
Author: | swami [ Wed Mar 26, 2008 4:51 pm ] |
Post subject: | Re: [Tutorial] "Perfect" Following |
:O omg.... Amazing!!!! Very well put, it made me think i could fly. :S if thats a good thing |
Author: | LaZ3R [ Wed Mar 26, 2008 5:28 pm ] |
Post subject: | RE:[Tutorial] "Perfect" Following |
Very nice and very well coded. The procedure for youLose is kind of pointless in THIS case since it consists of nothing but a single line of output, but if more stuff was added it would make sense |
Author: | Nick [ Wed Mar 26, 2008 6:33 pm ] |
Post subject: | RE:[Tutorial] "Perfect" Following |
youLose would be better as a function too |
Author: | swami [ Mon Mar 31, 2008 5:09 pm ] |
Post subject: | Re: [Tutorial] "Perfect" Following |
lmao 'youLose' ... awesome Beaner xD http://www.losethegame.com/ <---- inside joke... |
Author: | riveryu [ Sat May 17, 2008 6:14 pm ] |
Post subject: | Re: [Tutorial] "Perfect" Following |
Just for people who want to learn/review some Trigs... <a href ="http://www.clarku.edu/~djoyce/trig/">Dave' Short Trig Course </a> - good stuff I learned from when i was gr 10 (right now) <a href="http://www.physics.uoguelph.ca/tutorials/trig/trigonom.html"> University of Guelph Trig Review </a> - good jam packed summary/review...contains more than you need for this though "If you google everything, then you'll get everything." |
Author: | richcash [ Sat May 17, 2008 6:31 pm ] |
Post subject: | Re: [Tutorial] "Perfect" Following |
Trig Without Tears. That's a good approach to trigonometry too. It does not encourage "memorizing symbols in order". |
Author: | Saad [ Sat May 17, 2008 7:32 pm ] | ||
Post subject: | Re: [Tutorial] "Perfect" Following | ||
The tutorial is good, and great job on it. Finding the angle is a valid way to do it but why do more work then needed. I shall be going over a method that doesn't directly involve finding the angles but works on similar triangles (its still based on trig but you have no need to know it). The Theory From the following picture We can see that both triangles are similar. Knowing that we can derive the following equations For those that don't understand the following More simply put, is the same as the projected length of the direction and is the distance from the player and object Now knowing this we can re-arrange and find that And The Code And so lets use these equations in some code
|
Author: | r691175002 [ Sat May 17, 2008 8:47 pm ] |
Post subject: | Re: [Tutorial] "Perfect" Following |
More specifically, what Saad has described is normalizing the displacement vector between you and the target then scalar multiplying it by the speed. |
Author: | richcash [ Sat May 17, 2008 10:55 pm ] |
Post subject: | Re: [Tutorial] "Perfect" Following |
Good job, Saad. And good job The_Bean, you're tutorial is still useful as it shows how to find the polar angle. If you look at the The_Bean's method, you'll notice he uses cos(angle) and sin(angle), which we all know is x/radius and y/radius respectively, which leads us to the exact same equations as in Saad's method, so finding the polar angle is extraneous for the purpose of "following". This problem can be thought of in terms of trigonometry, but if you reduce properly you'll lead to doing the same thing as in similar triangles method (which can also be thought of in terms of vectors, as r691175002 pointed out). |
Author: | chipanpriest [ Tue Dec 06, 2011 7:45 pm ] |
Post subject: | Re: [Tutorial] "Perfect" Following |
Hey could somebody post a code where there's no tail? :p the tail's messing me up |
Author: | Zren [ Tue Dec 06, 2011 9:06 pm ] | ||
Post subject: | RE:[Tutorial] "Perfect" Following | ||
There was probably a better (or more recent) tutorial I should of linked you to. Oh well. The function setAngle is properly known as atan2 in other languages. Here's an example using radians.
|