Help with collisions

Blitz3D Forums/Blitz3D Beginners Area/Help with collisions

Javier(Posted 2008) [#1]
Hi. I hope help me with a trouble of collisions. Here my code:


Graphics3D 640,480,16,1

SetBuffer BackBuffer()

Const FPS=60

global rayo,platillo,enem,metal,secuencia

x_flot1=0
y_flot1=-0.25

camara=CreateCamera()
CameraRange camara,0.05,10

luz=CreateLight()

platillo=LoadAnimMesh("platillo.b3d")
ScaleEntity platillo,0.04,0.08,0.08
RotateEntity platillo,0,90,0
PositionEntity platillo,0,-0.2,0.5
ExtractAnimSeq(platillo,0,40);Secuencia 1
ExtractAnimSeq(platillo,40,60);Secuencia 2
secuencia=1
animate platillo,1,1,secuencia,0

rayo=loadsprite("rayo.bmp")
hideentity rayo

enem=loadmesh("enemigo.b3d")
hideentity enem

metal=loadtexture("metal.bmp")

If KeyHit(32) Then   
   crea_disparo()
   SoundVolume sonido_laser,0.5
   PlaySound sonido_laser
endif 

Type disparo
   Field tipo
   Field vel#
End Type

Function crea_disparo()
   ray.disparo=New disparo
   ray\tipo=CopyEntity(rayo)
   ScaleSprite ray\tipo,0.02,0.02
   EntityType ray\tipo,1
   EntityRadius ray\tipo,0.02

   If secuencia=1 Then
      PositionEntity ray\tipo,EntityX(platillo)+0.05,EntityY(platillo)+0.015,EntityZ(platillo)
      ray\vel=0.05
   ElseIf secuencia=2 Then
     PositionEntity ray\tipo,EntityX(platillo)-0.05,EntityY(platillo)+0.015,EntityZ(platillo)
      ray\vel=-0.05
   EndIf
End Function 

Function mueve_destruye_disparo()
   For ray.disparo=Each disparo
       MoveEntity ray\tipo,ray\vel,0,0
       If EntityX(ray\tipo)>EntityX(platillo)+1 Or EntityX(ray\tipo)<EntityX(platillo)-1 Then
          FreeEntity ray\tipo
          Delete ray
       EndIf
   Next
End Function

Function destruye_disparo()
For ray.disparo=Each disparo
    If EntityCollided(ray\tipo,3) Then
       FreeEntity ray\tipo
       Delete ray
    EndIf
Next
end function 

Type flotilla1
   Field tipo
   Field vel#
End Type

Function crea_flotilla1()
 For i=1 To 5
       flot1.flotilla1=New flotilla1
       x_flot1=Rnd(1,2)*p_flot1
       flot1\tipo=CopyEntity(enem)
       ScaleEntity flot1\tipo,0.08,0.08,0.08
       PositionEntity flot1\tipo,x_flot1,y_flot1,0.5
       EntityTexture flot1\tipo,metal,0,1
       NameEntity flot1\tipo,i
       EntityType flot1\tipo,3
       EntityBox flot1\tipo,-0.04,-0.04,-0.04,0.08,0.08,0.08
       y_flot1=y_flot1+0.1
       flot1\vel=0.01*v_flot1
       v_flot1=v_flot1*-1
       p_flot1=p_flot1*-1
   Next
End Function 

crea_flotilla1()

Function repos_flotilla1()
   For flot1.flotilla1=Each flotilla1       
       If EntityX(flot1\tipo)>0.6 Or EntityCollided(flot1\tipo,1) Then
          If EntityName(flot1\tipo)=1 Then
             PositionEntity flot1\tipo,Rnd(-1.3,-0.6),-0.25,0.5
          EndIf
          If EntityName(flot1\tipo)=3 Then
             PositionEntity flot1\tipo,Rnd(-1.3,-0.6),0,0.5
          EndIf
          If EntityName(flot1\tipo)=5 Then
             PositionEntity flot1\tipo,Rnd(-1.3,-0.6),0.25,0.5
          EndIf
        EndIf 

       If EntityX(flot1\tipo)<-0.6 Or EntityCollided(flot1\tipo,1) Then
          If EntityName(flot1\tipo)=2 Then 
             PositionEntity flot1\tipo,Rnd(1.3,0.6),-0.125,0.5
          EndIf
          If EntityName(flot1\tipo)=4 Then
             PositionEntity flot1\tipo,Rnd(1.3,0.6),0.125,0.5
          EndIf
        EndIf
Next
End Function 

Function mueve_flotilla1()
   For flot1.flotilla1=Each flotilla1
       MoveEntity flot1\tipo,flot1\vel,0,0
   Next
End Function

period=1000/FPS
time=MilliSecs()-period

Collisions 1,3,3,1

While Not KeyHit(1)

       Repeat
	elapsed=MilliSecs()-time
       Until elapsed
       ticks=elapsed/period	
       tween#=Float(elapsed Mod period)/Float(period)

	For k=1 To ticks
	    repos_flotilla1()
            If k=ticks Then CaptureWorld
            time=time+period
            mueve_flotilla1()
            mueve_destruye_disparo()
            destruye_disparo()
            updateworld
        next

   RenderWorld tween

   Flip

wend
End


the first time (before return to start position) , all work fine:

http://img129.imageshack.us/img129/9248/img1kb5.th.jpg

after return to start position, laser collides with enemy, but enemy is in another side :-(

http://img353.imageshack.us/img353/4147/img2jf1.th.jpg

What´s wrong. :-(

Thank you very much to help me. :-)


b32(Posted 2008) [#2]
Without the media, I find it difficult to help out. The problem you're describing however, could be collision related. When you 'warp' an entity, that is, re-position it with PositionEntity, it still responds to collisions. To avoid this, use ResetEntity after repositioning.


Javier(Posted 2008) [#3]
I tried it but does not work... :-( Please help meeee!


Stevie G(Posted 2008) [#4]
Try the resetentity or ( hideentity / positionentity / showentity ) and comment out the rendertweening code to see if that works.

I've heard about strange things happening with re-positioning and render-tweening although there have been a few good solutions posted if you do a search.

Other than that post the code without the need for external media - just use cubes and the like.


Javier(Posted 2008) [#5]
I've heard about strange things happening with re-positioning and render-tweening although there have been a few good solutions posted if you do a search.



Nop, not is the command Render tween. Here is the code an can try you it.

;Javier Ramírez...

Graphics3D 640,480,16,1

SetBuffer BackBuffer()

Global platillo,txt_plat,x#,y#,v,p,disp

y=-0.3
v=1
p=-1

camara=CreateCamera()
CameraRange camara,0.01,10

luz=CreateLight()

platillo=CreateSphere()
ScaleEntity platillo,0.03,0.03,0.015
PositionEntity platillo,0,-0.18,0.5
;PositionEntity platillo,0,0.12,0.5

Type balas
  Field tipo
End Type 

Function crea_balas()
  bal.balas=New balas
  bal\tipo=CreateSphere()
  ScaleEntity bal\tipo,0.01,0.01,0.01
  PositionEntity bal\tipo,EntityX(platillo),EntityY(platillo),EntityZ(platillo)
  EntityType bal\tipo,1
  EntityRadius bal\tipo,0.01
End Function 

Function mueve_balas()
  For bal.balas=Each balas
      MoveEntity bal\tipo,-0.02,0,0
      If EntityX(bal\tipo)<-0.5 Or EntityCollided(bal\tipo,3) Then
         FreeEntity bal\tipo
         Delete bal
      EndIf 
  Next
End Function 

Function movimientos_disparo()

 ;If EntityY(platillo)<0.3 Then
 ;   If KeyDown(200) Then MoveEntity platillo,0,0.01,0
 ;EndIf
 ;If EntityY(platillo)>-0.3 Then
 ;   If KeyDown(208) Then MoveEntity platillo,0,-0.01,0
 ;EndIf
 ;If EntityX(platillo)>-0.4 Then
 ;   If KeyDown(203) Then MoveEntity platillo,-0.01,0,0
 ;EndIf
 ;If EntityX(platillo)<0.4 Then
 ;   If KeyDown(205) Then MoveEntity platillo,0.01,0,0
 ;EndIf

 If KeyHit(32) Then
    crea_balas()
 EndIf
    
End Function 

Type enemigos
   Field tipo[5]
   Field vel#[5]
   Field ex[5]
   Field tv[5]
End Type

Function crea_enemigos()
 enem.enemigos=New enemigos
   For i=1 To 5
       x=Rnd(1,2)*p
       enem\tipo[i]=CreateCube()
       ScaleEntity enem\tipo[i],0.04,0.04,0.04
       PositionEntity enem\tipo[i],x,y,0.5
       EntityType enem\tipo[i],3
       EntityBox enem\tipo[i],-0.02,-0.02,-0.02,0.04,0.04,0.04
       y=y+0.15
       enem\vel[i]=0.01*v
       v=v*-1
       p=p*-1
   Next
End Function 

crea_enemigos()

Function mueve_enemigos()
   For enem.enemigos=Each enemigos
       For i=1 To 5
           MoveEntity enem\tipo[i],enem\vel[i],0,0
       Next

          If EntityX(enem\tipo[1])>0.6 Or EntityCollided(enem\tipo[1],1) Then
             PositionEntity enem\tipo[1],Rnd(-1.3,-0.6),-0.3,0.5
             ResetEntity enem\tipo[1]
          EndIf

          If EntityX(enem\tipo[3])>0.6 Or EntityCollided(enem\tipo[3],1) Then
             PositionEntity enem\tipo[3],Rnd(-1.3,-0.6),0,0.5
             ResetEntity enem\tipo[3]
          EndIf

          If EntityX(enem\tipo[5])>0.6 Or EntityCollided(enem\tipo[5],1) Then
             PositionEntity enem\tipo[5],Rnd(-1.3,-0.6),0.3,0.5
             ResetEntity enem\tipo[5]
          EndIf

          If EntityX(enem\tipo[2])<-0.6 Or EntityCollided(enem\tipo[2],1) Then
             ;HideEntity enem\tipo[2]
             PositionEntity enem\tipo[2],Rnd(0.6,1.3),-0.15,0.5
             ResetEntity enem\tipo[2]
             ;ShowEntity enem\tipo[2]
          EndIf

          If EntityX(enem\tipo[4])<-0.6 Or EntityCollided(enem\tipo[4],1) Then
             ;HideEntity enem\tipo[4]
             PositionEntity enem\tipo[4],Rnd(0.6,1.3),0.15,0.5
             ResetEntity enem\tipo[4]
             ;ShowEntity enem\tipo[4]
          EndIf

   Next
End Function 

Collisions 1,3,3,1
;With this don´t work good :-(. it´s a bug?

;Collisions 1,3,2,1
;With this, work fine! :-) but... why?

While Not KeyHit(1)

movimientos_disparo()
mueve_balas()
mueve_enemigos()

UpdateWorld	
RenderWorld

Text 10,20,"Posición Y: " + EntityY(platillo) 

Flip

Wend
End


the solution is "Collisions 1,3,2,1" but....Why?. I think that then is a bug with entitybox and collisions.

Gracias. :-)


Stevie G(Posted 2008) [#6]
Nae bother Yoda!