Author

Thread





05202003 06:21
Posted by:
Jason Anderson
Location:
Doylestown, PA

Two questions to go with my last one which was answered very well, thank you, my heat seaking routines work FLAWLESSLY now. (Took a lot of work to figure it out, but when it clicked, I almost cried from happiness.)
First question:
How can I find out the distance apat two pixels are? Like I have point A at 100, 200 and point B at 460, 300. They're not on the same X or Y coordinate. So it's not as easy as subtracting A from B. What would be the correct mathematical routines to do this?
Second Question may actually be answered by the first. How would I find out whether point B is within a certain radius of point A. (I could figure this out myself if I had the answer to the first question.)
Anyone know? Thanks in advance. It's the final boundary at this point to getting a working game.

05202003 07:56
Posted by:
puffin

a way to find the distance between two points is with the distance formula which is, if i remember right:
sqrt ((x2  x1)^2 + (y2  y1)^2)
where the first point is (x1, y1) and the second is (x2, y2). it's basically the Pythagorean theorem.
i think by checking to see if point 1 is a certain distance from point 2 that you would automatically be checking if point 1 falls inside a certain radius.

05202003 08:34
Posted by:
Jason Anderson
Location:
Doylestown, PA

AWESOME! Thanks so much!
Math was never my strong point. I barely passed Algebra. I'm sure learning something now.
Awesome. I've only been working on this Asteroids style game for about 20 hours all together, and I've already done so much.

05202003 14:35
Posted by:
MapMaker
Location:
Edmonton, Alberta (Canada)

You mean you took algebra but you never leared the Pythagorian Theorum? ;)

05202003 15:52
Posted by:
puffin

learning more math will always come in handy. there are some free educational resources on the internet like http://www.math.com/, and http://www.ed.gov/free/smath.html, the latter one being an index of free educational resources provided by the government. it isn't limited just to math either.

05202003 17:00
Posted by:
DanLurie
Location:
Earth>USA>New Jersey>Clifton>My Chair

Don't you hate theorems that are hard to spell?
FWIW, you can do a lot of cool programming with just Algebra. Trig is good to understand also. If I might reccomend a good, short book. Precalculus Mathematics in a Nutshell by Barnes & Noble is very good. Cheap, too.

05202003 18:52
Posted by:
Jason Anderson
Location:
Doylestown, PA

I know, the problem is I can do basic Math easily. But as soon as I got into Algebra, I couldn't hold onto any of the knowledge. I failed the class miserably. The next year I wen't back to General Math II.
It's the same with programming languages. The only language I seem to have picked up is BASIC. Because it's just so basic. Easy to understand. I'm barely geting by with Javascript in Konfabulator and I can't even BEGIN to understand C++.
I didn't even really understand the whole Sin() Cos() functions until yesterday. Now I can do rotations and even 2d wireframes again.

05202003 20:38
Posted by:
Mark Tully
Location:
TNT HQ, England

Optimisation
Quick tip: as puffin said the distance between two points is given by the formula 'sqrt ((x2  x1)^2 + (y2  y1)^2)' and would normally be used in some code like this:
float dist = sqrt ((x2  x1)^2 + (y2  y1)^2)
if dist < 20 then explode()
However, the sqrt function is a slow mathematical operation, when comparing distance like this it's much quicker to square the constant side of the expression than to square root the variable side and it's exactly the same.
float dist = (x2x1)^2 + (y2y1)^2
if dist < 400 then explode()
If you were to do these two test 1000 times and compare how long they took you'd see testing the nonsqrt way is a lot faster meaning you can get a lot more tests done in the same amount of time.
Cheers,
Mark

05202003 21:28
Posted by:
DanLurie
Location:
Earth>USA>New Jersey>Clifton>My Chair

I didn't really follow Alg or Trig, either, until I wanted to do rotating sprites and 3d effects. Once you care, you make an effort to understand it.

05202003 23:54
Posted by:
Jason Anderson
Location:
Doylestown, PA

By Constant what do you mean?
I hope it doesn't mean that one of the points has to always be the same. I'll be using it to see if two almost constantly moving points are close togethjer. I.e. the Ship and the Enemy. Both will be moving.
I'll try that test right now. I hope it's not too slow. Is this a limitation of TNT or just the Basic language itself? Can it be made faster? I tested it on an object that is still and the ship. It wasn't slow. But since I'll be moving both points I hope it won't bog me down too much.
Even with line graphics, at 640x480 the game can go almost 45FPS. And I'm limiting it to 30 so maybe I have a nice buffer.

05212003 00:07
Posted by:
matteo
Location:
Venice, ITALY!

yeah, you guessed it, only TNT has this problem of slow square roots, due to bad programming by Tully...
it can be made faster if you write
#do{FastSquareRoots}
at the first line of your code
and, yes, the distance formula can be used only for constant points, that is, you can' t move them; or, at least, they have to be still in the last 7 frames, otherwise there are problems because of general relativity.

05212003 00:35
Posted by:
Jason Anderson
Location:
Doylestown, PA

Well, I made a test program to do 10000 loops testing both static points and one static/one moving point.
I was getting weird results depending on extra code or stuff taken out. Weird. Here's my final results for 10000 calculations:
10000 tests
SqRt: 342.000000
Non SqRt: 242.000000
Moving SqRt: 234.000000
Moving Non SqRt: 97.000000
The final two tests were the same wiether one or two points were moving. Of course the point was only moving one direction.
And oddly if I removed certain extraneous code it would tale .1 second longer. But not bad for 10000 calculations really. I don't plan on making a game with 1000 enemies at once anyway. Well... maybe if I decide to make it a scrolling playfield. Other than that it's fine.
<< CODE >>
Float t1a, t2a, t1b, t2b, t1c, t2c, t1d, t2d, t1e, t2e, t1f, t2f
Int x1, x2, y1, y2, i, n = 10000
Print ""
Print As String(n) + " tests"
x1 = 0 : y1 = 0 : x2 = 100 : y2 = 100
t1a = Timer
For i = 1 to n
float dist = sqrt ((x2  x1)^2 + (y2  y1)^2)
Next
t2a = Timer  t1a
Print "SqRt: " + As String(t2a)
x1 = 0 : y1 = 0 : x2 = 100 : y2 = 100
t1b = Timer
For i = 1 to n
float dist = (x2x1)^2 + (y2y1)^2
Next
t2b = Timer  t1b
Print "Non SqRt: " + As String(t2b)
x1 = 0 : y1 = 0 : x2 = 100 : y2 = 100
t1c = Timer
For i = 1 to n
x1 = x1 + 1
float dist = sqrt ((x2  x1)^2 + (y2  y1)^2)
Next
t2c = Timer  t1c
Print "Moving SqRt: " + As String(t2c)
x1 = 0 : y1 = 0 : x2 = 100 : y2 = 100
t1d = Timer
For i = 1 to n
x1 = x1 + 1
float dist = (x2x1)^2 + (y2y1)^2
Next
t2d = Timer  t1d
Print "Moving Non SqRt: " + As String(t2d)
<< CODE >>
As for my current test in the game right now, the result seemed pretty accurate no matter what. But I got what you're saying, Matteo. BTW, were you kdding about the #do{FastSquareRoots} line? I tried it and it gives me an error.

05212003 01:14
Posted by:
puffin

i'd like matteo to elaborate on how the distance formula can't be used for moving points due to general relativity. i think it is a very interesting topic.

05212003 04:25
Posted by:
Jason Anderson
Location:
Doylestown, PA

I'm curious to that as well. It seems to be working fine for me. Even with both points moving.

05212003 04:54
Posted by:
puffin

matteo is like a master of bullshit i'd say. the #do{fastsquareroots} was good enough, but then to bring Einstein into it, pure gold. reminds me of that time i wrote a report on how the camel is one of the most bloodthirsty super predators on the face of the planet.

05212003 05:29
Posted by:
Jason Anderson
Location:
Doylestown, PA

Ok.. Now I'm curious and interested in what puffin is saying.
I wish to subscribe to both your newsletters.
Good thing I got my answer before this thread went all wacky. ;)

05212003 12:40
Posted by:
matteo
Location:
Venice, ITALY!

Please keep this a serious topic
No kiddin, I was serious.
1) The #do{FastSquareRoots} is available only if you have the "RadRebPrefs" extension file (Acronym for "RADicals REsolving Boosted PREFerable Speed") installed in your system. You can get it at
http://www.danlabgames.com/radicalrebound/RadicalRebound.sit
2) Of course if the points are moving fast enough there relative space and time are shrinked, so distance in the 4D space is changed (not considering the other 7Ds). Some might say that this is true only for speeds near 300.000 kilometers per second, but this is actually not true: we must be precise in our calculations, otherwise we risk that our game cause the electrons in the monitors cathodic tube to change their spin abruptly. This was actually what caused the Cernobyl disaster: the bad programming of a space invaders clone. Of course if you have an LCD display you are safe (but only if your screen resoulution indexes are NOT prime numbers or perfect squares).
3)And stop it with that camels story: it' s pure nonsense: almost all camels are not predators: be precise please, don' t tell stupid stories: only the devil red camel of the south pole is actually extremely dangerous: only 3 of the 729438 people have seen one and remained alive: Tony Culkins, Joa Pessoa and Jimmy the Raynor. Well, actually Jimmy is not very alive... Just saw him yesterday and I don' t think he'll do it for long... poor chap.
The devil red camel of the south pole is, of course, the cause for the inhabitantness (i.e. that is not habited)of the south pole. Watch out for his fireballs if you go there.

05222003 02:53
Posted by:
Jason Anderson
Location:
Doylestown, PA

Well, basically that link gave me a game and a manual. Not the library. ???

05222003 03:57
Posted by:
matteo
Location:
Venice, ITALY!

well, at least you got a good game ;) (it' s mine!)

05222003 04:46
Posted by:
Jason Anderson
Location:
Doylestown, PA

Seriously. Is there a library somewhere?

05222003 04:52
Posted by:
Jason Anderson
Location:
Doylestown, PA

Pretty cool. Better than the first.
I really like that you have it going fast and such. I'm having trouble with Hardware mode. I really need to speed it up so I can switch from lines to graphics.

05222003 13:31
Posted by:
matteo
Location:
Venice, ITALY!

Thanks! Here are some rules to get good performance and nice effects:
Not too many big sprites
Use scaling and rotations : nice effects
Scrolling slows down very little: use it!
Use transparencies
Don' t draw

05242003 02:08
Posted by:
Jason Anderson
Location:
Doylestown, PA

I still don't have a library to use.

05272003 23:04
Posted by:
Mark Tully
Location:
TNT HQ, England

Jason,
I'm sure matteo will release his library as soon as TNT Basic actually supports plugins...
The formula for telling how far apart two points is works whether the points are moving or not. When I said constant I was referring to the value that you are testing against, eg the distance that the objects had to be apart before they would hit. In your case it was 20.
You had:
sqrt((x2x1)^2 + (y2y1)^2) < 20
A rule of algebra is that you can change either side of this expression as long as you make the same changes to both sides. (When I say side I mean side of the < sign. This applies to all comparison operators). So if I square the expression on the left of the < and square the expression on the right of the < the overall expression means exactly the same thing. It now becomes:
((x2x1)^2 + (y2y1)^2) < 20^2
Of course 20^2 is 400 so I just replaced it in my previous example so TNT Basic didn't need to work it out. With the sqrt gone it will run faster. This is because working out the sqrt of a number is a slow process  no matter what matteo claims! ;)
Good luck,
Mark

05292003 09:09
Posted by:
Jason Anderson
Location:
Doylestown, PA

Wait. If TNT doesn't currently support plugins. How does Matteo do his cool stuff? I'm all ferjiggered here.

05292003 20:58
Posted by:
Mark Tully
Location:
TNT HQ, England

Practice, practice, practice...
We're all using the same commands here ;)

05292003 21:22
Posted by:
matteo
Location:
Venice, ITALY!

hehe...
How much would you pay for my "RADicals REsolving Boosted PREFerable Speed" (RadReBPrefS) library? with it you can :
#do{FastSquareRoots}
#do{FastAndFuriousHardwareMode}
and finally, the new added jem
#do{MakeOSoCoolGame}
it' s yours for only 25$!!!

05292003 23:37
Posted by:
someone
Location:
Quebec ( Canada )

How can you think we will believe you it exists when mark just posted:
> TNT doesn't currently support plugins.

05302003 00:05
Posted by:
matteo
Location:
Venice, ITALY!

infact it' s not a plug in, it' s a library.
PS: how can you think that i think that you believe me? ;p

All times are GMT




