Page 1 of 1

sprite collision question

Posted: Sat May 23, 2009 2:37 am
by wolfwood2x
I am setting up walls for a 2d sprite to be bound in. The idea is to prevent the sprite from moving through a wall.

Code: Select all

If KeyboardPushed(#PB_Key_A) Or KeyboardPushed(#PB_Key_Left)
            Imagex= imagex-3
             If SpriteCollision(0, imagex, imagey, 101, 103, 93)
                 imagex=imagex+3
             EndIf
             If SpriteCollision(0, imagex, imagey, 103, 196, 280)
                 imagex=imagex+3
                Debug "test"
             EndIf          
EndIf
If I comment out the final IF block the first part seems to function normally ( when I try to move left through the wall it prevents me from moving left).

However, with the block as is when I try to move left at all I am prevented from moving no matter where the sprite is. Even if I am nowhere near the 196, 280 coords.

what is wrong with this code?


In retrospect this should probably be in the game programming area. If one of the mods wants to move this topic feel free to do so.

Posted: Sat May 23, 2009 4:41 am
by Demivec
This is a little difficult to answer without knowing the dimensions of sprites #0, #101, or #103.

Posted: Sat May 23, 2009 4:54 am
by Kaeru Gaman
when you want a bounding wall, sprite collision is not the appropriate way to solve it.
you don't need the wall at all, you just stop by coordinates.

Code: Select all

If KeyboardPushed(#PB_Key_A) Or KeyboardPushed(#PB_Key_Left)
    If ImageX > 12   ; Left Border
        ImageX -3
    EndIf  
EndIf 

btw... Sprite(Pixel)Collision does not check for any existing pixel...
SpriteCollision is in fact nothing more than a coordinates check, resp. checking two bitmapmasks with SpritePixelCollision.

Posted: Sat May 23, 2009 11:38 am
by djes
More than that, if your steps are 3 pixels, you may pass beyond the wall ("jumping"). You have to find a trick to check this possibility.

Posted: Sat May 23, 2009 3:51 pm
by wolfwood2x
Kaeru Gaman wrote:when you want a bounding wall, sprite collision is not the appropriate way to solve it.
you don't need the wall at all, you just stop by coordinates.
This would be a great solution if I only had 4 walls. But since it is intended to be a maze that seems like a daunting task. I guess I could make a Struct with dimensions of the walls and do a for each element loop to compare against the main sprite.

Posted: Sat May 23, 2009 4:21 pm
by netmaestro
Have a play with this and see if it's any help:

Code: Select all

#Brown = 15480

OpenWindow(0,0,0,640,480,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
InitSprite():InitKeyboard()
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
CreateSprite(0,640,480)
StartDrawing(SpriteOutput(0))
  Box(0,0,640,480,#Brown)
  Box(20,20,600,440,#Black)
  Box(310,240,20,220,#brown)
  Box(140,20,20,130,#brown)
  Box(460,20,20,130,#brown)
StopDrawing()

CreateSprite(1,32,32)
StartDrawing(SpriteOutput(1))
  Circle(16,16,16,#Red)
StopDrawing()

x=40
y=260

quit=0
Repeat
  Repeat:ev=WindowEvent():If ev=#PB_Event_CloseWindow:quit=1:EndIf:Until ev=0
  ClearScreen(0)
  DisplayTransparentSprite(0,0,0)
  DisplayTransparentSprite(1,x,y)
  FlipBuffers()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Right)
    If Not SpritePixelCollision(0,0,0,1,x+3,y) : x+3 : EndIf
  EndIf
  If KeyboardPushed(#PB_Key_Left)
    If Not SpritePixelCollision(0,0,0,1,x-3,y) : x-3 : EndIf
  EndIf
  If KeyboardPushed(#PB_Key_Down)
    If Not SpritePixelCollision(0,0,0,1,x,y+3) : y+3 : EndIf
  EndIf
  If KeyboardPushed(#PB_Key_Up)
    If Not SpritePixelCollision(0,0,0,1,x,y-3) : y-3 : EndIf
  EndIf
     
Until quit