Jump to content



Photo
- - - - -

Measuring Distances Between Objects?


  • Please log in to reply
4 replies to this topic

#1 Espyo

Espyo

    Newbie

  • Members
  • Pip
  • 19 posts
  • Gender:Male
  • Location:Portugal
  • Interests:Games... And such.<br />About Casio... Programming! Woo yeah!

  • Calculators:
    Casio FX 1.0 Plus

Posted 09 April 2008 - 05:49 PM

I was thinking of creating a game that looks like Chaos Emerald Hunting from Sonic Adventure DX and Sonic Adventure 2.
For those who don't know what it is, well, it's basically a "cold... very cold... getting warmer... warm... hot!... warm... hot! Bingo!"
Well, the problem is: I can do a random to put the 3 pieces on random spots, and the closer you are to them, the on-screen radar will change. Except that: Is there any way to calculate the distance difference between the main character's X and Y and one of the pieces X and Y?

#2 kucalc

kucalc

    Casio Maniac

  • [Legends]
  • PipPipPipPipPipPipPipPip
  • 1422 posts
  • Gender:Male
  • Location:USA
  • Interests:Programming: C/C++, Fortran, LISP, COBOL 85 Standard, PHP, x86 and SH3 Assembly

    Computer graphics

  • Calculators:
    fx-9860G / fx-7400G Plus / Algebra FX 2.0+ / fx-9770G / CFX-9850G / CFX-9850GB+ / TI-89 / TI-nSpire

Posted 09 April 2008 - 06:23 PM

Hey Espyo!

To calculate the distance between two points you can use the distance forumula. You need two coordinates:

x1, y1 (Could be the main player)
x2, y2 (Could be an enemy)

Here's the forumula:
distance = sqrt( (x2-x1)^2 + (y2-y1)^2 )

#3 TomL_12953

TomL_12953

    Casio Addict

  • Members
  • PipPipPip
  • 57 posts
  • Gender:Male
  • Location:Malone, NY USA
  • Interests:Programmable calculators, BASIC computer language, Computers in general, Old movies and TV shows

  • Calculators:
    TI-59, TI-85, TI-86, TI-89, TI-89 Titanium, Radio Shack EC-4026, Algebra FX 2.0 Plus, HP-35s, Compucorp 326 Scientist, Durabrand 828, fx-9860G II SD

Posted 09 April 2008 - 07:49 PM

Y'all might recognize that as the Pythagorean Theorem that gives the length of the hypotenuse of a right triangle.

Tom Lake

#4 verena

verena

    Casio Fan

  • Members
  • PipPip
  • 41 posts
  • Gender:Female
  • Location:Tashkent

  • Calculators:
    Casio ƒx-10 ~ ƒx-991ES

Posted 09 April 2008 - 10:05 PM

I would not use the "power" function "^" though, to calculate the square, it takes much more time to calculate than the direct square function (uppercase "2").
distance = sqrt( (x2-x1)2+ (y2-y1)2 )
Or use a direct calculation: distance = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) )

If you need an even faster calculation, leave out the "sqrt", then you will have the square of the distance, which is just as useful to judge proximity of two points.
For instance a result of "100" means the distance is 10, "25" means the distance is 5, etc.

#5 Espyo

Espyo

    Newbie

  • Members
  • Pip
  • 19 posts
  • Gender:Male
  • Location:Portugal
  • Interests:Games... And such.<br />About Casio... Programming! Woo yeah!

  • Calculators:
    Casio FX 1.0 Plus

Posted 12 April 2008 - 02:46 PM

Oh, so that's how!
Thanks a bunch everyone ;D


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users