Collision code

Monkey Forums/Monkey Code/Collision code

Yahfree(Posted 2011) [#1]
Here's some useful collision code ported from bmax:




therevills(Posted 2011) [#2]
Nice!

Any examples of use?


dopeyrulz(Posted 2011) [#3]
Great thanks! Had started converting myself for some vector stuff but hadn't finished :)


Yahfree(Posted 2011) [#4]
therevils, I don't have any examples, but I'm using a circle->polygon collision in my current project if that's any help.

No problem dopeyrulz.

I just converted this code, and I haven't extensively tested it(circle->poly works for sure), so if you find any problems, let me know!


dopeyrulz(Posted 2011) [#5]
Yahfree,

Have a long weekend at parents so will let you know tomorrow (Monday) how it looks.


dopeyrulz(Posted 2011) [#6]
I've done some work around PointInTFormPoly:



I read somewhere else the code is not fully processed unless you include some reference to it - ie. only compiles what it needs. I needed to 'box' up the Var references (as outlined here: http://www.monkeycoder.co.nz/Community/posts.php?topic=172 )


pinete(Posted 2011) [#7]
Some examples might be absolutely helpful!


dopeyrulz(Posted 2011) [#8]
pinete,

This code originally comes from Digesteroids - example game from BlitzMax.


jtadeo(Posted 2011) [#9]
Thanks for the post. Same as pinete - if you can include an example that would be good. It'll help fast track the process.


GC-Martijn(Posted 2011) [#10]
Yea a simple example code how to use this please, I don't have BlitzMax so I can't see how to use this.


Shinkiro1(Posted 2011) [#11]
Class TCollision

	Field x1:Float
	Field y1:Float
	Field x2:Float
	Field y2:Float
	Field x3:Float
	Field y3:Float
	Field x4:Float
	Field y4:Float
	
	Field entity:TEntity
	
	Method Init:Void( entityRef:TEntity )
		entity = entityRef
		UpdateBounds()
	End Method
	
	Method UpdateBounds:Void()
		x1 = -entity.image.handle_x * Pos( entity.scale.sx )
		y1 = -entity.image.handle_y * Pos( entity.scale.sy )
		x2 =  (entity.image.width - entity.image.handle_x) * Pos( entity.scale.sx )
		y2 = -entity.image.handle_y * Pos( entity.scale.sy )
		x3 =  (entity.image.width - entity.image.handle_x) * Pos( entity.scale.sx ) 
		y3 =  (entity.image.height - entity.image.handle_y) * Pos( entity.scale.sy )  
		x4 =  -entity.image.handle_x * Pos( entity.scale.sx )
		y4 =  (entity.image.height - entity.image.handle_y) * Pos( entity.scale.sy )
		'Print x1 ; Print y1
		'Print x2 ; Print y2
		'Print x3 ; Print y3
		'Print x4 ; Print y4
		'Print "-------"
	End Method
	
	Method CollidesWithPoint:Bool( px:Float, py:Float )
		px = px - entity.position.x
		py = py - entity.position.y
		Local tx:Float = px*Cos(-entity.rotation) - py*Sin(-entity.rotation) 
		Local ty:Float = py*Cos(-entity.rotation) + px*Sin(-entity.rotation) 
		If tx > x1 And ty >y1 And tx < x3 And ty < y3
			Return True
		EndIf
		Return False
	End Method
	
End


As requested from GC-Martijn:
The above checks for a point inside a rotated rectangle.

I have converted it from BlitzMax and the entity thing should be obvious.
Pos(value:Float) just ensures the value is positive.
You can leave out entity.scale.sx/sy if your entities are never scaled.

Example:
Class TEntity
  Field image:Image
  Field collision:TCollision

  Method New()
    collision = New TCollision
    image = LoadImage("someimage.png")
  End

End

Local entity:TEntity = New TEntity

If entity.collision.CollideWithPoint(MouseX(),MouseY())
  Print "Collision"
End



GC-Martijn(Posted 2011) [#12]
Thanks for sharing ! :) Maybe I can use it inside a collision code I use that can't check Rects at the moment !


Dan(Posted 2012) [#13]
Thanks for this post. Came in very handy!