Monkey Game Development Book Help

Monkey Forums/Monkey Beginners/Monkey Game Development Book Help

Nick_Ve1(Posted 2014) [#1]
Hello Everyone,
I am taking a Game Development Class, and we are using Monkey Game Development. In chapter 3 it wants to make a game called Rocket Commander it is much like Missile Command. The last part for the bombs in the CheckCollisions Method I am getting a error of cannot convert explosion to float here is the whole Method.

Method CheckCollisions:Int()

Local dist:Float

For Local explosion := Eachin explosions
dist = GetDistance(cx, cy, explosion.x, explosion.y)

If dist < explosion Then
bombs.Remove(Self)
game.score += expolsion.score
game.totalBombsDestroyed += 1
explosion.score += 100
Endif
Next

For Local city := Eachin cities
dist = GetDistance(cx, cy, city.x, city.y)

If dist < 30 Then
bombs.Remove(Self)
cities.Remove(city)
Endif
Next

For Local launcher := Eachin launchers
dist = GetDistance(cx, cy, launcher.x, launcher.y)

If dist < 15 Then
bombs.Remove(Self)
launchers.Remove(launcher)
Endif
Next

Return True
End

Has anyone worked with this book and can give me some advice.


Jesse(Posted 2014) [#2]
I haven't seen the book but from what I can see is that explosion is an object and dist is a float. maybe it should be dist < explosion.radius?
post the explosion class code. maybe it will help making correct conclusion.


nikoniko(Posted 2014) [#3]
Yes, it's a book typing error

In my code this block is
    For Local explosion := Eachin explosions
      dist = GetDistance(cx, cy, explosion.x, explosion.y)
      If dist < 20 Then
        bombs.Remove(Self)
        game.score += explosion.score
        game.totalBombsDestroyed += 1
        explosion.score += 100
      Endif
    Next    



MikeHart(Posted 2014) [#4]
Thanks nikoniko for helping out.


Goodlookinguy(Posted 2014) [#5]
GetDistance isn't getting the actual distance between 2 points is it? If so, that's horribly inefficient for comparing distance. The book hopefully mentions that square root calculations are very heavy, especially on mobile devices. Always use the squared distance where possible.

Edit: Let me mention that one of my games used the real distance for computations and it stopped the game from displaying more than sixty things on screen at a time without bogging down to a few frames a second. I then switched it over to use squared distances and it allowed me to put thousands of things on screen all at once without this bogging down.


nikoniko(Posted 2014) [#6]
Goodlookinguy wrote:
The book hopefully mentions that square root calculations are very heavy, especially on mobile devices. Always use the squared distance where possible.


I am thinking RC is very simple game example and using square root is right as example game engine geometry. Next step - optimization! Use square funct or precalculated table of func (sqrt, sin, cos, etc) for superfast position/distance how we did in pc/xt time without math coprocessor :)

PS What is smooth font ugly in Internet Explorer?!


Goodlookinguy(Posted 2014) [#7]
Lookup tables are kinda useless now since floating point values are used more often than not.

Something like this is tons of times faster than getting the real distance and is realistic for use in modern day games. I just hope the book mentions this somewhere along the way.
For Local explosion := Eachin explosions
	dist = GetSquaredDistance(cx, cy, explosion.x, explosion.y)
	If dist < 20*20
		bombs.Remove(Self)
		game.score += explosion.score
		game.totalBombsDestroyed += 1
		explosion.score += 100
	End
Next

Function GetSquaredDistance( x1:Float, y1:Float, x2:Float, y2:Float )
	Local distX:Float = x2 - x1
	Local distY:Float = y2 - y1
	Return distX * distX + distY * distY
End



nikoniko(Posted 2014) [#8]
Goodlookinguy wrote:
Something like this is tons of times faster than getting the real distance and is realistic


You're right. It's the good and useful optimization example.


dawlane(Posted 2014) [#9]
To me
dist = GetSquaredDistance(cx, cy, explosion.x, explosion.y)
If dist < 20*20

Looks like a circle to circle collision and could be code to call a function to return a bool
If Circle2Circle(cx, cy, explosion.x, explosion.y,10,10).....

Function Circle2Circle:Bool( x1:Float, y1:Float, x2:Float, y2:Float, r1:Float, r2:Float )
	Local dx:Float = x2 - x1
	Local dy:Float = y2 - y1
	Local ri:Float = r1 + r2
	Return ( (dx * dx) + (dy * dy) <= ri * ri )
End Function


An interesting article that's worth a read about the origin of the inverse square root function in Quake 3


Goodlookinguy(Posted 2014) [#10]
It's interesting that the code matches up with a circles colliding algorithm. Although I based that algorithm on 2 vectors. And, to be fair, it doesn't actually match up completely.

This part of the line
20*20

is precomputed to
400

I was just doing 20*20 for readability.

Edit: Also, I think just about everyone knows about the invsqrt function after the source code for those large games were released and it was riddled with fowl comments.


Gerry Quinn(Posted 2014) [#11]
It can also be useful to check the bounding box first:

' Calculate dx and dy
If dx >= - 20 And dx <= 20 And dy >= -20 And dy <= 20
If dx * dx + dy * dy <= 20 * 20
' Collision happens
End
End

Realistically, though, a missile command game is probably testing less than 100 collisions per update, and finicky optimisations are unnecessary on modern machines.


GuShh(Posted 2014) [#12]
The quake sources were riddled with expressive comments not fowl!


Goodlookinguy(Posted 2014) [#13]
Semantics. Still, they were working with C++ so I'm not surprised.