Code archives/Graphics/Automatic 2D collision response

This code has been declared by its author to be Public Domain code.

Download source code

Automatic 2D collision response by BlitzSupport2002
I thought I'd posted this already, but don't see it anywhere...

It only does a sort of circular response to the collision, but should be useful for something! It's also pretty fixed as-is, so just use it as a starting point for your own needs. Replace "big-moo.bmp" with an image of your own...
; -----------------------------------------------------------------------------
; Automatic collision response in 2D...
; -----------------------------------------------------------------------------
; support@blitzbasic.com
; -----------------------------------------------------------------------------

Graphics 640, 480
AppTitle "Automatic 2D collision response"
SetBuffer BackBuffer ()
ClsColor 64, 128, 180

; -----------------------------------------------------------------------------
; Load images...
; -----------------------------------------------------------------------------

sheep1 = LoadImage ("big-moo.bmp")
MaskImage sheep1, 255, 0, 255
sheep2 = CopyImage (sheep1)

px = 10: py = 10 ; Player x and y positions

; -----------------------------------------------------------------------------
; The variables that get the resulting movement values must be floats!
; -----------------------------------------------------------------------------

cx# = 90: cy# = 90 ; Computer x and y positions

; -----------------------------------------------------------------------------
; The 'mover' variable specifies how much to move in a collision. You could
; make this dynamic within your main loop, of course...
; -----------------------------------------------------------------------------

mover# = 1

Repeat

	Cls

	If KeyDown (203) px = px - 1
	If KeyDown (205) px = px + 1
	If KeyDown (200) py = py - 1
	If KeyDown (208) py = py + 1
	
	If ImagesCollide (sheep1, px, py, 0, sheep2, cx, cy, 0)
		angle# = ATan2 (cy - py, cx - px)
		cx = cx + (mover * (Cos (angle)))
		cy = cy + (mover * (Sin (angle)))
	EndIf
	
	DrawImage sheep1, px, py
	DrawImage sheep2, cx, cy
		
	Flip
	
Until KeyHit (1)

End

Comments

None.

Code Archives Forum