Different effects from Differen sides of an object

Blitz3D Forums/Blitz3D Programming/Different effects from Differen sides of an object

En929(Posted 2009) [#1]
I have a question for Blitzplus. Do you remember how in Super Mario Bros if Mario were to collide with an enemy from the side, it makes Mario die, but if one were to collide with that same enemy from the top, the enemy dies? I was wondering what is the easiest way to get that effect with a moving character in my game with a character called ENEMY?

Below is a screenshot and code (with notes) on what I was trying to accomplish.

Thanks

http://authorhenryemphrey.tripod.com/gallery/





Graphics 1640, 1000
SetBuffer BackBuffer ()


;----------------------------------------------------------------------------

;Load Items

HENRY = LoadAnimImage ("Move2.png",400,370,0,13)
ENEMY = LoadImage ("MintMonster.png")
DEFEATSOUND = LoadSound ("Punchsound.wav")
BACKGROUND = LoadImage ("DockII.jpg")
PLATFORM = LoadImage ("Big Platform2.png")

Type HENRY

	Field x,y
	
	Field frame 

End Type 


Type ENEMY

	Field x,y
	
End Type 

Type BACKGROUND

	Field x,y
	
	Field image

End Type 


Type PLATFORM

	Field x,y
	
End Type


h.HENRY = New HENRY
h\x =  600
h\y = 450
h\frame = 0




;there are two enemies

For j = 1 To 2
e.ENEMY = New ENEMY
e\x = 700 + (400*j)
e\y = 360
Next 


b.BACKGROUND = New BACKGROUND
b\x = 0
b\y = 700
b\image = BACKGROUND 

p.PLATFORM = New PLATFORM
p\x = 200
p\y = 370






MOVEMENT = 1




While Not KeyDown(1)

		Cls 
		
		
TileImage (b\image,b\x,b\y)			
		



		
;here are the enemies that I'm trying to make the two collision effects with. With theses, I'm trying to make it so that
;if I collide with the enemies from the side HENRY dies, but if HENRY collides with the same ENEMY
;from the top, then the ENEMY dies, like Mario and the Goombas on Super Mario Bros.	
		


For e.ENEMY = Each ENEMY
  e\x = e\x + MOVEMENT 


If e\x > 600 Then MOVEMENT = -MOVEMENT 
If e\x < 800 Then MOVEMENT = 1  


	
DrawImage (ENEMY,(b\x + e\x),e\y)






;Here's even more specifically what I'm trying to fix. Currently, this part doesn't have what I'm trying to accomplish.


If ImagesCollide (ENEMY,(b\x + e\x),e\y,0, HENRY,h\x,h\y,0) Then

					
					Delete e
				
					PlaySound (DEFEATSOUND)
				
		EndIf 


Next 


DrawImage (HENRY,h\x,h\y,h\frame)





;draws the platform onto the screen

DrawImage (PLATFORM,(b\x + p\x),p\y)






;keeps the player on the platform 

If ImagesCollide(HENRY,h\x,h\y,0,PLATFORM,(b\x + p\x),p\y,0) Then 

		
	    h\y = 400
	
Else 

		h\y = h\y + 10


		
EndIf  





;moves the player right


If KeyDown(205)
		

		
		
		b\x = b\x - 5
		h\frame = h\frame + 1
		
		If h\frame > 2 Then 
		h\frame = 0
		
		EndIf 

EndIf 
  




;moves the player left

		If KeyDown(203)
		
		
		b\x = b\x + 5
		h\frame = h\frame + 1
		

		If h\frame > 12 Then
		h\frame = 6


       EndIf

EndIf   





;makes the player jump

If KeyDown (57)
 
h\y = h\y -150
			
		
			
EndIf 





;brings the player back down to the ground after jumping

If Not KeyHit (57)

 h\y = h\y + 10

EndIf 


Flip

Wend 
 




Leon Drake(Posted 2009) [#2]
not hard really just need to have 2 collision boxes. one would be a collision box around mario except for his feet the other one is 1 pixel high and not quite as wide as his feet. so if mario is not falling or mid jump the bigger collision box collision would cause mario to die. however if the smaller 1 pixel high box collides first and mario is jumping or falling then the enemy dies.

at least thats how i would do it.


En929(Posted 2009) [#3]
I was wondering how do I make the collision boxes?


Leon Drake(Posted 2009) [#4]
depends on how you do it. you could just simply specify a rect area as a type and then make a function to check wether the enemies rect type collides with mario's.

Or you could just make an rect image that gets drawn before the background of your game so its invisible, then just do an images collide function. e.g.


Type HENRY

	Field x,y
	field falling
	Field frame 
        
        Field HenryCollide.CollisionBox = new CollisionBox
        Field HenryFoot.CollisionBox = new CollisionBox

End Type 

Type CollisionBox

        Field x,y,w,h
        field box ;is an image
End Type

;***************************
;use the foot collision box for platforms otherwise henry can get stuck on ;the edge
If ImagesCollide(h\HenryFoot\box,h\HenryFoot\x,h\HenryFoot\y,0,PLATFORM,(b\x + p\x),p\y,0) Then 

		
	    h\y = 400
	
Else 

		h\y = h\y + 10


		
EndIf  


;for enemies

If ImagesCollide (ENEMY,(b\x + e\x),e\y,0, h\HenryFoot\box,h\HenryFoot\x,h\HenryFoot\y,0) AND h\falling = true Then

					
                    ;enemy dies here
				
		EndIf 


If ImagesCollide (ENEMY,(b\x + e\x),e\y,0, h\HenryCollide\box,h\HenryCollide\x,h\HenryCollide\y,0) Then

					
					Delete e
				
					PlaySound (DEFEATSOUND)
				
		EndIf 



;*************************





anyways something like that basically. And for platforms on a character you typically don't want to use the entire character image as the collision. this can cause the charater to get stuck on the sides of a platform when falling off or trying to jump on. your bestter off using just a 1 pixel high square box for that. this way jumping through the platform can be handled better.