Page 1 of 1
Math problem
Posted: Fri Dec 07, 2012 2:39 am
by overras
Upps,

Im not so good at math and ha...
Well, let's say I want to calculate the distance between 2 coordinates in seconds ?
2:3 to 4:3
Can somebody write me the math formula for this please ? Thank you guys
AND , let's don't make another topic, I want opinions,
criticize and suggestions for this code:
http://s14.postimage.org/eu9e5mntt/loopindex.jpg
I need spceial from you to criticize .
Re: Math problem
Posted: Fri Dec 07, 2012 3:07 am
by Zykal
Well assuming you don't move diagonally and 1 unit is 1 second
your example of 2:3 and 4:3 would be 2 seconds, If thats so
I would make variables for each part of the coord for both sets of coords
so
$X1, $Y1 and $X2, $Y2
then its just subtracting $x2 from $x1 in this case would be -2 then use ABS on it so it'll always be posative
then do the same for the Y1 and Y2 which in this case would be 0 then add them both together 2 = 0 = 2
Re: Math problem
Posted: Fri Dec 07, 2012 8:55 am
by overras
Thank you very much for your help but I use a different move system, moving in all directions with 1 click, example from 1:1 to 99:99 and I need the math formula for PHP.

Re: Math problem
Posted: Fri Dec 07, 2012 3:21 pm
by Ark
Use the pythagorean formula then. Goes like this:
Code: Select all
$x1 = 1;
$y1 = 1;
$x2 = 99;
$y2 = 99;
$distance = sqrt ( pow ( $x2 - $x1, 2 ) + pow ( $y2 - $y1, 2 ) );
Re: Math problem
Posted: Fri Dec 07, 2012 8:16 pm
by overras
Ark wrote:Use the pythagorean formula then. Goes like this:
Code: Select all
$x1 = 1;
$y1 = 1;
$x2 = 99;
$y2 = 99;
$distance = sqrt ( pow ( $x2 - $x1, 2 ) + pow ( $y2 - $y1, 2 ) );
Damn, thanks!! :!:You must be a teacher or.. ? Thanks you man, thank you very much!
Re: Math problem
Posted: Fri Dec 07, 2012 9:01 pm
by vitinho444
overras, i dont want to mean "u shouldn't come here" but if u google distance between 2 points in google, that equation comes right off

I used the same (googling it) for my 2d engine

Re: Math problem
Posted: Tue Dec 11, 2012 12:34 am
by Jackolantern
Another really, really important thing to know for 2D game development is basic trig. For example, to turn an object (maybe a gun, for example) in the direction your mouse is pointing, get the X and Y values of the mouse cursor, and then use this (language neutral pseudocode):
Code: Select all
var angle = Math.atan2(mouseY - this.positionY, mouseX - this.positionX);
Or the non-object oriented way (for example, turning a gun):
Code: Select all
var angle = Math.atan2(mouseY - gun.Y, mouseX - gun.X);
That returns the rotation in radians, which is what most programming languages use (not degrees). And if you do need to rotate an object and only have degrees, you can turn them to radians this way:
Code: Select all
var rads = degrees * (Math.PI / 180);
And to move something at an angle (say animating a bullet being fired from a gun in the first example), you need a bit of trig for that, too. First, you need the angle in radians (shown here as a variable called "rads") you want the object to move along as well as the desired velocity you want the object to move (basically how fast you want it to move; this typically requires testing to find the right velocity), and then you can get the amount to add to the X and Y values for your object like this:
Code: Select all
var moveX = Math.sin(rads) * velocityX;
var moveY = Math.cos(rads) * velocityY;
//now you would actually animate the object
bullet.X = bullet.X + moveX;
bullet.Y = bullet.Y + moveY;
These 4 little procedures (including the
distance formula discussed above) here are among some of the most useful for a 2D game dev to know
