none
Collision system ways RRS feed

  • Question

  • Well im wondering if there is any other way than using a loop to go from start position to get the bounding box, if you check for the left side of the bounding box, you check from start Y till the Y + image height... and im asking, is there any other way to achieve this, maybe faster ?

     


    I love game design and game programing.
    Friday, August 26, 2011 9:38 PM

Answers

  • Hi,

    Thanks for the code - I don't have the sprite images, but get the idea of what you are doing.

    We  should break down the problem - we have the ideas for shape generation and movement, but need to work on the grid and collisions for the packman and should work on this bit separately for a bit.

    There are several ways to detect collision:

    1] Using the colour of the background to determine if we are are on a boundary or not - this is very general, but could be slow.

    2] Use the proximity of the sprite to a feature - in which case we need to know the position and shape of the obstacle.

    3] Have a pre-defined grid defined with all of the possible positions and movements defined in arrays.

    Possibly I would go for method 3 for a packman game, but this requires the most work setting the grid up, so lets try method 1 first - it is simpler especially if we limit the directions and positions the sprite can move.

    We can use GraphicsWindow.GetPixel to determine the colour of the background, so we need to have a basic background colour, lets say pink and put some features that are different colour - these are not shapes added, but stuff drawn on the window.

    We start consider a 8 by 8 grid with some randomly placed walls.

    STEP 1 - Set up a backround and test we can get a pixel colour

    'Create the Window
    gw = 400
    gh = 400
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    GraphicsWindow.Show()
    'Draw the background (just bigger than the window) - we have to do this to detect the colour with Shapes.GetPixel
    bgColor = "#FFAAFF" 'Pink
    GraphicsWindow.BrushColor = bgColor
    GraphicsWindow.FillRectangle(-10,-10,gw+20,gh+20)

    TextWindow.WriteLine(GraphicsWindow.GetPixel(100,100))

    STEP 2 - Put some walls on the background

    'Draw Walls
    GraphicsWindow.PenWidth = 6
    GraphicsWindow.PenColor = "Black"
    'First a Full Mesh
    For i = 0 To 8
      GraphicsWindow.DrawLine(i*50,0,i*50,gh)
      GraphicsWindow.DrawLine(0,i*50,gw,i*50)
    EndFor
    'Now make some spaces - this is just a quick test - you will need a better way to create a grid
    GraphicsWindow.PenColor = bgColor
    For x = 1 To 50
      i = Math.GetRandomNumber(8)
      j = Math.GetRandomNumber(6)
      GraphicsWindow.DrawLine((i-1)*50+3,j*50,i*50-3,j*50)
      i = Math.GetRandomNumber(6)
      j = Math.GetRandomNumber(8)
      GraphicsWindow.DrawLine(i*50,(j-1)*50+3,i*50,j*50-3)
    EndFor

    STEP 3 - Create a sprite - work with generic shapes rather than images while prototyping - this also means you can publish test code that others can run.

    'Add a sprite
    GraphicsWindow.BrushColor = "Blue"
    sprite = Shapes.AddEllipse(40,40)
    spriteX = 4 'Column
    spriteY = 6 'Row
    Shapes.Move(sprite,spriteX*50-25-20,spriteY*50-25-20) 'Note the 'spriteX*50-25' is box centre and 'spriteX*50-25-20' is the sprite left coordinate

    STEP 4 - Add the keyboard movement no auto repeat, just use mouse down

    'Initialise Key events
    GraphicsWindow.KeyDown = OnKeyDown
    keyPressed = 0

    'MAIN LOOP

    While ("True")
      If (keyPressed = 1) Then 'Process the key and reset the press
        processKey()
        keyPressed = 0
      EndIf
      Shapes.Animate(sprite,spriteX*50-25-20,spriteY*50-25-20,100) ' A short animate looks smoother
      Program.Delay(100)
    EndWhile

    'Key press event subroutines
    'We need a new key press for each movement
    Sub OnKeyDown
      lastKey = GraphicsWindow.LastKey
      keyPressed = 1
    EndSub

    Sub processKey
      'Move object - NO diagonal movement
      If (lastKey = "Left") Then
        spriteX = spriteX-1
      ElseIf (lastKey = "Right") Then
        spriteX = spriteX+1
      ElseIf (lastKey = "Up") Then
        spriteY = spriteY-1
      ElseIf (lastKey = "Down") Then
        spriteY = spriteY+1
      EndIf
    EndSub

    STEP 5 - Check for collisions using the GetPixel method in the processKey subroutine

      'Check for walls
      cenX = spriteX*50-25 'Box centre
      cenY = spriteY*50-25
      leftPixel = GraphicsWindow.GetPixel(cenX-25,cenY)
      rightPixel = GraphicsWindow.GetPixel(cenX+25,cenY)
      upPixel = GraphicsWindow.GetPixel(cenX,cenY-25)
      downPixel = GraphicsWindow.GetPixel(cenX,cenY+25)

    Keep the structure and comments up to date.

    When you have a problem, break it down to the simplest example you can that shows the problem.

    While developing, resist the temptation to add lots of visual clutter until the engine of the game is working properly.

    The whole thing, import SVN475 - there is plenty more work to be done, but the basic ideas here should help.

     



    • Marked as answer by Primoz128 Saturday, August 27, 2011 11:10 AM
    Saturday, August 27, 2011 9:39 AM
    Moderator

All replies

  • Some code please - and a more detailed question - there are loads of ways to do collision detection.

    Collision is usually about detecting shape proximity or overlap.

    Here are some examples (full programs can be complex so we need to focus on specific small prototyping code and questions to provide realistic help).

    Import XJS516, PQZ001, CCZ718, RQS337, JJF680

    Friday, August 26, 2011 9:59 PM
    Moderator
  • Im trying to make a bounding box using a while loop.  

    In that loop i will be using my wall y start position and height of the wall. CODE while(obj_wall_x < obj_wall_height) then... as the obj_wall_x i will use extra variables.

    All this will be used in a subroutine. 

    And im interested if there is any other way than a loop.

    And here is also the project: RKD642


    I love game design and game programing.
    Friday, August 26, 2011 10:43 PM
  • Hi,

    Thanks for the code - I don't have the sprite images, but get the idea of what you are doing.

    We  should break down the problem - we have the ideas for shape generation and movement, but need to work on the grid and collisions for the packman and should work on this bit separately for a bit.

    There are several ways to detect collision:

    1] Using the colour of the background to determine if we are are on a boundary or not - this is very general, but could be slow.

    2] Use the proximity of the sprite to a feature - in which case we need to know the position and shape of the obstacle.

    3] Have a pre-defined grid defined with all of the possible positions and movements defined in arrays.

    Possibly I would go for method 3 for a packman game, but this requires the most work setting the grid up, so lets try method 1 first - it is simpler especially if we limit the directions and positions the sprite can move.

    We can use GraphicsWindow.GetPixel to determine the colour of the background, so we need to have a basic background colour, lets say pink and put some features that are different colour - these are not shapes added, but stuff drawn on the window.

    We start consider a 8 by 8 grid with some randomly placed walls.

    STEP 1 - Set up a backround and test we can get a pixel colour

    'Create the Window
    gw = 400
    gh = 400
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    GraphicsWindow.Show()
    'Draw the background (just bigger than the window) - we have to do this to detect the colour with Shapes.GetPixel
    bgColor = "#FFAAFF" 'Pink
    GraphicsWindow.BrushColor = bgColor
    GraphicsWindow.FillRectangle(-10,-10,gw+20,gh+20)

    TextWindow.WriteLine(GraphicsWindow.GetPixel(100,100))

    STEP 2 - Put some walls on the background

    'Draw Walls
    GraphicsWindow.PenWidth = 6
    GraphicsWindow.PenColor = "Black"
    'First a Full Mesh
    For i = 0 To 8
      GraphicsWindow.DrawLine(i*50,0,i*50,gh)
      GraphicsWindow.DrawLine(0,i*50,gw,i*50)
    EndFor
    'Now make some spaces - this is just a quick test - you will need a better way to create a grid
    GraphicsWindow.PenColor = bgColor
    For x = 1 To 50
      i = Math.GetRandomNumber(8)
      j = Math.GetRandomNumber(6)
      GraphicsWindow.DrawLine((i-1)*50+3,j*50,i*50-3,j*50)
      i = Math.GetRandomNumber(6)
      j = Math.GetRandomNumber(8)
      GraphicsWindow.DrawLine(i*50,(j-1)*50+3,i*50,j*50-3)
    EndFor

    STEP 3 - Create a sprite - work with generic shapes rather than images while prototyping - this also means you can publish test code that others can run.

    'Add a sprite
    GraphicsWindow.BrushColor = "Blue"
    sprite = Shapes.AddEllipse(40,40)
    spriteX = 4 'Column
    spriteY = 6 'Row
    Shapes.Move(sprite,spriteX*50-25-20,spriteY*50-25-20) 'Note the 'spriteX*50-25' is box centre and 'spriteX*50-25-20' is the sprite left coordinate

    STEP 4 - Add the keyboard movement no auto repeat, just use mouse down

    'Initialise Key events
    GraphicsWindow.KeyDown = OnKeyDown
    keyPressed = 0

    'MAIN LOOP

    While ("True")
      If (keyPressed = 1) Then 'Process the key and reset the press
        processKey()
        keyPressed = 0
      EndIf
      Shapes.Animate(sprite,spriteX*50-25-20,spriteY*50-25-20,100) ' A short animate looks smoother
      Program.Delay(100)
    EndWhile

    'Key press event subroutines
    'We need a new key press for each movement
    Sub OnKeyDown
      lastKey = GraphicsWindow.LastKey
      keyPressed = 1
    EndSub

    Sub processKey
      'Move object - NO diagonal movement
      If (lastKey = "Left") Then
        spriteX = spriteX-1
      ElseIf (lastKey = "Right") Then
        spriteX = spriteX+1
      ElseIf (lastKey = "Up") Then
        spriteY = spriteY-1
      ElseIf (lastKey = "Down") Then
        spriteY = spriteY+1
      EndIf
    EndSub

    STEP 5 - Check for collisions using the GetPixel method in the processKey subroutine

      'Check for walls
      cenX = spriteX*50-25 'Box centre
      cenY = spriteY*50-25
      leftPixel = GraphicsWindow.GetPixel(cenX-25,cenY)
      rightPixel = GraphicsWindow.GetPixel(cenX+25,cenY)
      upPixel = GraphicsWindow.GetPixel(cenX,cenY-25)
      downPixel = GraphicsWindow.GetPixel(cenX,cenY+25)

    Keep the structure and comments up to date.

    When you have a problem, break it down to the simplest example you can that shows the problem.

    While developing, resist the temptation to add lots of visual clutter until the engine of the game is working properly.

    The whole thing, import SVN475 - there is plenty more work to be done, but the basic ideas here should help.

     



    • Marked as answer by Primoz128 Saturday, August 27, 2011 11:10 AM
    Saturday, August 27, 2011 9:39 AM
    Moderator
  • Ok this looks completly different than what i was trying to do, but ill do both because bounding box is what most later 2D games use not what you shown, but thanks anyways.
    I love game design and game programing.
    Saturday, August 27, 2011 11:11 AM