Thought I might just as well put this up, simple calculations on the above picture:
Firstly, what is the distance between the character and his enemy? Anyone who has read about Pythagoras knows that A� +B� equal C�, so that�s the answer. The hypotenuse is obviously the distance between the two.
C = sqrt(sqr(A)+sqr(B));
Then how large are the values A and B? Let�s call the two objects visible at the image object_character and object_enemy. You must choose either of them from which you will run you calculations. I choose object_character. For A I will have to run the following calculation; object_enemy.y � object_character.y, which would work fine. Only that if the enemy gets on the other side of the character and thereby gets a lower y value things might screw up (you will get a negative number). Therefore, to prove how you could come up with a solution that works in either case I will show you this calculation:
A = sqrt(sqr(object_character.y � object_enemy.y));
See how this is foolproof? Even if it would be a negative number it would become positive as you multiply it with itself (a number multiplied with itself will always be positive). The sqrt, square root, function will turn the number back to normal ratio again. Now we do the same thing with the B value (just that we use x instead of y):
B = sqrt(sqr(object_character.x � object_enemy.x));
Now if you first calculate A, then B and the equation of C will work.
Note that just like someone replied sqrt(sqr(x)) is the same thing as abs(x), in math written |x|. I chose to use sqrt(sqr(x)), that'll be easier for those who haven't read a lot of math already to understand.
Further on we can calculate the angles within that same (fictive) triangle that we have used to determine the distances. The angle between the character and his enemy will be very useful later on; when for an example you want the character to shoot something at his enemy (we will be showing how to do this).
The mathematical tool we�ll be using here is known as trigonometry. A lot of you have certainly already been working with this, and the famous cosines, sinus, tangent. Sinus waves are by the way also commonly used in game design, although it�d be out of the subject of this article to explain how. Anyway, I won�t do any brief description of trigonometry, like some of you might need. If you do not know trigonometry it might therefore be hard to understand the following calculations. You will need to check the definitions of cos, sin, and tan using (just a suggestion) the Internet.
I�ll just use A and B as though they were defined values, but in your game you�ll have to calculate them like I did above. Now study the following calculations:
tan v = a/b
Leading to:
v = arctan(a/b)
Then (just because I want to exemplify this with something concrete, such that the use of this becomes clearer) to fire a bullet in that direction you�d do something like:
a = abs(object_character.y � object_enemy.y);
b = abs(object_character.x � object_enemy.x);
i = instance_create(x,y,obj_bullet);
i.direction = arctan(a/b);
i.speed = 5;
I�ll try to update this article at some later point, and maybe I can include something more. Ask if there is anything specific that you�d like to know.
Comments
Loading comments...