Collision detection with drawrect and types

BlitzMax Forums/BlitzMax Beginners Area/Collision detection with drawrect and types

Vasquo(Posted 2010) [#1]
Hi There,

Im currently working on my very own pong clone, I’m using the tutorial from Assarti as a code base, so most of the code has been copy pasted from his tutorials and adapted to my own needs.
I don’t want to use any images, but now I’m running into problems with my collision detection.

I’ve added this code to the type TBall

Function ballCollision:Int(x0:Int, y0:Int, w0:Int, h0:Int, x2:Int, y2:Int, w2:Int, h2:Int)
  	Local ret:Int
	If x0 > (x2 + w2) Or (x0 + w0) < x2
		ret = 0
		Return ret
	EndIf
	If y0 > (y2 + h2) Or (y0 + h0) < y2
		ret = 0
		Return ret
	End If	
	ret = 1
    Return ret
End Function

The function needs the player cords as well. That’s where I get in to trouble, I can’t figure out how to get the players x and y values.

Ive added them as globals and updated the player state, it did work but it’s a dirty solution.

So is there a way to get data from another type without the use of those globals?
or am I trying to solve something that should not exist in the first place due to a logic error in the code?
Here is the full code:


Kind regards,
Vasquo


AltanilConard(Posted 2010) [#2]
You could add the player as a global variable:
Global player:TPlayer = TPlayer.Create(20, 220, 4, 100, 10)
TBall.Create(WindowWidth / 2, 400, 4, 10, 10)

Now you can access his position wiht player.x, player.y. This means you would have to add an other global variable for every other player. If you want more flexible code, that supports any amount of players, you should loop trough the GameObject list again looking for players:
For Local plr:TPlayer = EachIn gameObjectList
	If ballCollision(x, y, 10, 10, plr.x, plr.y, 10, 140)
		xSpeed = -xSpeed
		ySpeed = -ySpeed
	End If
Next

Something like that should work.


Vasquo(Posted 2010) [#3]
That seems to work correctly! Thank you Conard

One more question though,

ive added an enemy paddle, if i add a loop for TEnemy the ball also detects the collision so it does work correctly. But is there a way to loop true more Types then just one?

i want to add walls and the ball should also detect these entitys.

Currently i would have to add a eachin loop for every element to check against.

is there any way i can create a general collision functions for the ball? or should the collision check take place at the players,enemy, walls?


Vasquo(Posted 2010) [#4]
I think i already found a solution.

I create a new list named collisionList
Ive added the player and the enemy to this list when its created.

after that the code works out the collision
For Local o:TGameObject = EachIn collisionList
	If ballCollision(x, y, 10, 10, o.x, o.y, o.width, o.height)
		xSpeed = -xSpeed
		ySpeed = -ySpeed
	End If
Next


btw is there a alternative way to handle this?

Kind regards,
Vastuo


AltanilConard(Posted 2010) [#5]
I think your collisionList is a fine way to handle this, ofcourse there are alternatives, but they won't be much better.