Be a Supporter!

Finding distance between 2 objects

  • 541 Views
  • 16 Replies
New Topic Respond to this Topic
RottenMilk
RottenMilk
  • Member since: Jul. 3, 2009
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Finding distance between 2 objects 2009-12-18 00:18:56 Reply

UGGGHHHHHHH

I knew this before.

But now i forgot.

how would you find the diffrence between the player's _x coordinate and the enemy's _x coordinate?

i'm using as2.

I know it has something to do with atan....????

help pls.


You wouldn't drink dirty water, So why would you drink rotten milk? Oh, you wouldn't? in that case...

Montycarlo
Montycarlo
  • Member since: Mar. 14, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 00:19:59 Reply

Math.abs(obj1.x - obj2.x)


Although practicality beats purity.
Errors should never pass silently.
In the face of ambiguity, refuse the temptation to guess.

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to Finding distance between 2 objects 2009-12-18 00:26:03 Reply

Math.tan(Math.atan(enemyx-playerx))

Montycarlo
Montycarlo
  • Member since: Mar. 14, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 00:30:23 Reply

At 12/18/09 12:26 AM, Glaiel-Gamer wrote: Math.tan(Math.atan(enemyx-playerx))

hahahahahahahahahahaha, thumbs up.


Although practicality beats purity.
Errors should never pass silently.
In the face of ambiguity, refuse the temptation to guess.

RottenMilk
RottenMilk
  • Member since: Jul. 3, 2009
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 00:34:16 Reply

I trust glaeil. thanks.


You wouldn't drink dirty water, So why would you drink rotten milk? Oh, you wouldn't? in that case...

Nisas
Nisas
  • Member since: Apr. 4, 2009
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 04:15:14 Reply

they say the hypotenuse is equal to the square root of the sum of both sides squared.

A^2 + B^2 = C^2

IE:

var xdif:Number = obj1.x - obj2.x;
var ydif:Number = obj1.y - obj2.y;
dist = Math.sqrt(xdif*xdif + ydif*ydif)

Signatures are for people who aren't lazy.

HonterGames
HonterGames
  • Member since: Jun. 18, 2009
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 10:16:49 Reply

At 12/18/09 12:34 AM, RottenMilk wrote: I trust glaeil. thanks.

Actually all of the replies u got would be acceptable depending on what you really wanted (eg the distance between objects, the difference between x pos, etc)


BBS Signature
Cojones893
Cojones893
  • Member since: Mar. 9, 2003
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 14:08:25 Reply

At 12/18/09 12:34 AM, RottenMilk wrote: I trust glaeil. thanks.

well you shouldn't...

Do you know what atan and tan do?

Cojones893
Cojones893
  • Member since: Mar. 9, 2003
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 14:16:24 Reply

At 12/18/09 12:26 AM, Glaiel-Gamer wrote: Math.tan(Math.atan(enemyx-playerx))

You missed a part

Math.PI*(Math.pow(Math.pow(Math.tan(Math.atan(obj1.x-obj2.x)),2),.5))/Math.PI

More accurate.

Yambanshee
Yambanshee
  • Member since: Oct. 5, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 14:34:00 Reply

just so you dont get totaly confused, everyone except for Nisas is screwing with you

Basicly, its like a right angled triangle. think of the y as the upright line, x as the horozontal line and the line connecting the two is a, or the distance. now to work out a, the formula is this:

a^2 = X^2+Y^2
so
a = square root(x+y)
so (in AS)

xdist = mc2.x - mc1.x
ydist = mx2.y - mc1.y
distance = Math.sqrt((xdist*xdist)+(ydist*ydist))

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature
4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to Finding distance between 2 objects 2009-12-18 14:36:04 Reply

At 12/18/09 02:16 PM, Cojones893 wrote:
At 12/18/09 12:26 AM, Glaiel-Gamer wrote: Math.tan(Math.atan(enemyx-playerx))
You missed a part

Math.PI*(Math.pow(Math.pow(Math.tan(Math .atan(obj1.x-obj2.x)),2),.5))/Math.PI

More accurate.

Can someone explain all that?

Yambanshee
Yambanshee
  • Member since: Oct. 5, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 14:58:36 Reply

Math.PI*(Math.pow(Math.pow(Math.tan(Math .atan(obj1.x-obj2.x)),2),.5))/Math.PI
Can someone explain all that?

if you read it carefully, you'll see its just a long complex formula which cancels out, giving you a end result of obj1.x-obj2.x i believe


AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature
4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to Finding distance between 2 objects 2009-12-18 15:01:59 Reply

At 12/18/09 02:58 PM, Yambanshee wrote:
Math.PI*(Math.pow(Math.pow(Math.tan(Math .atan(obj1.x-obj2.x)),2),.5))/Math.PI
Can someone explain all that?
if you read it carefully, you'll see its just a long complex formula which cancels out, giving you a end result of obj1.x-obj2.x i believe

Funny. So they're just messing with the dude. I knew you couldn't possibly get the distance without taking the y distance into account.

Montycarlo
Montycarlo
  • Member since: Mar. 14, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to Finding distance between 2 objects 2009-12-18 15:10:00 Reply

At 12/18/09 03:01 PM, 4urentertainment wrote: Funny. So they're just messing with the dude. I knew you couldn't possibly get the distance without taking the y distance into account.

He never asked for the vector difference, he merely asked for the x axis distance, not displacement.


Although practicality beats purity.
Errors should never pass silently.
In the face of ambiguity, refuse the temptation to guess.

4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to Finding distance between 2 objects 2009-12-18 15:11:58 Reply

At 12/18/09 03:10 PM, Montycarlo wrote:
At 12/18/09 03:01 PM, 4urentertainment wrote: Funny. So they're just messing with the dude. I knew you couldn't possibly get the distance without taking the y distance into account.
He never asked for the vector difference, he merely asked for the x axis distance, not displacement.

Really...Now that I read the post again, that does seem as what he wanted. I just figured he couldn't have wanted something that simple.

RottenMilk
RottenMilk
  • Member since: Jul. 3, 2009
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Finding distance between 2 objects 2009-12-19 12:22:17 Reply

I just want to know the difference between 2_x's. Very simple.

Not the difference between two objects. thanks.


You wouldn't drink dirty water, So why would you drink rotten milk? Oh, you wouldn't? in that case...

zrb
zrb
  • Member since: Aug. 8, 2006
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Finding distance between 2 objects 2009-12-19 12:32:12 Reply

At 12/19/09 12:22 PM, RottenMilk wrote: I just want to know the difference between 2_x's. Very simple.
Not the difference between two objects. thanks.

Well its been stated above (unless you don't need help anymore cause I can't tell):

xDifference = object2._x-object1._x;

Replace the objects by enemy or player or whatever you want.


School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature