Page 1 of 1

Collision & Rotate 3Dsprites

Posted: Fri Feb 11, 2005 8:30 pm
by MadMax
Code updated for 5.20+

This post got lost, it's just a small trick to use collision with rotated sprites.

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Error", "Can't open DirectX 7 Or later", 0)
  End
EndIf

fps.f=85
gfps.f=85
rfps.f=1

infoon=1

x=0
y=0

ang3d=0

hit=0

If OpenScreen(800,600,32,"Dabidu")
  
  
  CreateSprite(0,16,16, #PB_Sprite_PixelCollision)
  CreateSprite(1,128,128)
  
  If StartDrawing(SpriteOutput(0))
    FrontColor(RGB(0,255,0))
    Box(0,4,2,8)
    Box(14,4,2,8)
    Box(2,7,2,2)
    Box(12,7,2,2)
    Box(6,6,4,4)
    Box(3,0,10,2)
    Box(3,14,10,2)
    Box(7,2,2,2)
    Box(7,12,2,2)
    StopDrawing()
  EndIf
  
  If StartDrawing(SpriteOutput(1))
    FrontColor(RGB(200,0,0))
    LineXY(62,0,64,0)
    LineXY(25,50,62,0)
    LineXY(102,50,64,0)
    LineXY(25,50,50,50)
    LineXY(50,50,50,127)
    LineXY(50,127,77,127)
    LineXY(77,127,77,50)
    LineXY(77,50,102,50)
    FillArea(63,63,RGB(200,0,0),RGB(125,125,125))
    StopDrawing()
  EndIf
  
  
  fpss=ElapsedMilliseconds()
  
  
  Repeat
    
    FlipBuffers()
    
    ;==================================================================
    ; HERE WE DRAW THE SPRITE TO GRAB IT ONTO SPRITE #5
    ;==================================================================   
    
    ClearScreen(RGB(0,0,0))
    
    DisplaySprite(1, 250, 200)
    RotateSprite(1,ang3d,0)
    
    
    GrabSprite(5,250,200,128,128, #PB_Sprite_PixelCollision)
    ;******************************************************
    
    ClearScreen(RGB(0,80,0))   
    ;========================================================================================
    ;                 FPS
    ;========================================================================================
    conta=conta+1
    
    If ElapsedMilliseconds()=>fpss+1000
      fpss=ElapsedMilliseconds()
      fps=conta
      rfps=gfps/fps
      conta=0
    EndIf
    ;=========================================================================================
    ;=========================================================================================
    
    DisplaySprite(1, 250, 200)
    RotateSprite(1,ang3d,0)
    
    ExamineMouse()
    mx=MouseX()
    my=MouseY()
    
    DisplayTransparentSprite(0,mx,my)
    
    ;========================================================================
    ;HERE WE DETECT COLLISION WITH SPRITE #5 THAT STRANGELY ENOUGH IS THE SAME AS THE ROTATED SPRITE
    ;===========================================================================================
    
    If SpritePixelCollision(0,mx,my,5,250,200)
      hit=1
    Else
      hit=0
    EndIf
    
    ;=========================================================================================
    ;                 PRINT TEXT
    ;=========================================================================================
    
    If infoon=1
      
      
      If StartDrawing(ScreenOutput())
        
        FrontColor(RGB(255,255,255))
        DrawingMode(1)
        DrawText(20,20, "FPS: "+StrF(fps))
        If hit
          FrontColor(RGB(255,0,0))
          DrawText(20,60, "HIT!!!")
        EndIf
        FrontColor(RGB(0,255,255))
        DrawText(100,400, "USE CURSOR KEYS TO ROTATE... MOUSE CURSOR TO COLLIDE.")
        
        StopDrawing()
      EndIf
      
    EndIf
    
    ;=========================================================================================
    ;=========================================================================================   
    
    
    ;=========================================================================================
    ;               KEYBOARD INPUT
    ;=========================================================================================     
    ExamineKeyboard()
    
    If KeyboardPushed(#PB_Key_Right)
      ang3d=(ang3d+1) % 360
    EndIf
    
    If KeyboardPushed(#PB_Key_Left)
      ang3d=(ang3d-1) % 360
    EndIf
    
    If KeyboardReleased(#PB_Key_F1)
      Select infoon
          
        Case 0
          
          infoon=1
          
        Case 1
          
          infoon=0
          
      EndSelect
    EndIf
    
    ;=========================================================================================
    ;=========================================================================================   
  Until KeyboardPushed(#PB_Key_Escape)
  
Else
  MessageRequester("Error", "Can't open a 640*480 - 16 bit screen !", 0)
EndIf

End
Hope it's useful for somebody.

Posted: Fri Feb 11, 2005 8:37 pm
by blueznl
sneaky and nice :-)

i just wonder if 3d sprites make much sense when collision is going to be done in memory... i guess not really, does it?

Re: Collision & Rotate 3Dsprites

Posted: Fri Feb 11, 2005 11:57 pm
by Psychophanta
MadMax wrote: ;==================================================================
; HERE WE DRAW THE 3D SPRITE TO GRAB IT ONTO SPRITE #5
;==================================================================
The problem is that that trick is not very nice. Seems ugly.
Can you do it with several rotating and/or zooming 3D sprites? :twisted:

Posted: Sat Feb 12, 2005 11:32 am
by MadMax
The problem is that that trick is not very nice. Seems ugly.
He he. Well may not be very beautiful. But you can do it with quite a few sprites rotating or zoomed, this of course will have a limit depending on the gfx card and CPU. But it seems pretty fast, just use flipbuffers(0) and you will see it's fast enough for most uses.

Depending on what you want to do, sometimes it's posible to pre-render the rotations and then just use normal 2D sprites.