Collision detection OOP

Monkey Forums/Monkey Beginners/Collision detection OOP

vic(Posted 2015) [#1]
Hi,

I have made 3 seperate classes Player (p), Enemy (e) and Bullet (b) and ran into below described problem with some simple collison detection function.

How could/should I use the following collision detection code,

If rectsoverlap(b.x,b.y,5,5,e.x,e.y,32,32) = True
...<do something with Enemy/Bullet>
End if

I either end up with a NullObjectAccess error when placed in OnUpdate() method) or
when placed inside the Bullet class it seems it cannot access the Enemy x, y position and vice versa.

The rectsoverlap function:
Function rectsoverlap:Int(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
If x1 > (x2 + w2) Or (x1 + w1) < x2 Then Return False
If y1 > (y2 + h2) Or (y1 + h1) < y2 Then Return False
Return True
End


Salmakis(Posted 2015) [#2]
if you get a "NullObjectAccess" then this might be refer to the object e, instead of its field x, otherwise x would just something like 0 or whatever you defined for its Default values
so maybe in this Moment the object e seems to be null.
Did you called something like
e = new Enemy(...)
or if you got an own constructor like enemy.Create(...) then maybe you not returned your new object?


Pakz(Posted 2015) [#3]
The rectsoverlap function is not correct btw. I found the same function on this forum but found that it collides one pixel distance from the true collision point. I fixed it by putting = characters in the function.

See below.

Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
    If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False
    If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False
    Return True
End


This one makes sure that collision rectangles can touch eachother, Your function collides when it is next to a rectangle.

For you original post. Were you using global or local? I am stil new at monkey so I might be completely wrong with the error message.

I have lots of examples on my blog(monkey tutorial section) that might help you learn how to do the collision things. I use rectsoverlap a lot. I am going to make a beginners example with a player class and enemy class and bullet class after reading your post. I think it will be ready soon.

I already messed up some of my examples by using the older rectsoverlap I found a few days ago. This I still need to repair now.

Edit :

Here is the example I made. I hope it was close to what you ment to do.




vic(Posted 2015) [#4]
Thanks for your reply Salmakis & Pakz.
@Pakz: I tried your code and it runs fine, I just want to understand what I am doing wrong (or what is missing) in my code.

In the Bullet class Update method I want to check if the bullet overlaps the enemy x,y but somehow I don't have 'access' to either the bullet x,y or enemy x,y from there.




Gerry Quinn(Posted 2015) [#5]
You have a separate single global e:Enemy which is never set so it is null when accessed. The enemies are in the global enemies:List< Enemy >.

There seems no reason for p, e or b to exist (you seem to have multiple players too).

Your Bullet.Update() should go through the list of enemies, testing against each.


vic(Posted 2015) [#6]
Woohoo, I got it working... thanks guys :)