sprite collision question

Just starting out? Need help? Post your questions and find answers here.
wolfwood2x
User
User
Posts: 76
Joined: Sun Oct 02, 2005 5:08 am

sprite collision question

Post 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.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

This is a little difficult to answer without knowing the dimensions of sprites #0, #101, or #103.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post 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.
wolfwood2x
User
User
Posts: 76
Joined: Sun Oct 02, 2005 5:08 am

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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
BERESHEIT
Post Reply