none
random movement of shapes and collision detection of those shapes RRS feed

  • Question

  • I am new to SmallBasic and I am still learning. I have a question regarding how you would make ten 90x90 pixel squares move around the screen in random directions, and when they hit the edges of an 800x600 graphics window, they would bounce off in the other direction. Also, if a small circle controlled by the user(I don't need help with the mouse controls of the circle) hit a square it would display a message stating that. Any help would be appreciated, thanks for your time.
    Sunday, December 2, 2012 8:23 PM

Answers

  • Here. I've fixed your problem I think. You just used the keyword "Or" where you should have put "And" and misplaced a few of ">" and "<."

    I also changed the amount of blocks to 4 to make it possible to survive.

    gw = 800
    gh = 600
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    begin:
    objectw=35
    objecth=35
    objectx=(gw/2)-(objectw/2)
    objecty=(gh/2)-(objecth/2)
    object=shapes.AddRectangle(objectw,objecth)
    Shapes.Move(object,objectx,objecty)
    
    GraphicsWindow.MouseMove=OnMouseMove
    
    'Create shapes, the positions and velocities
    For i = 1 To 4
      scolour[i]=GraphicsWindow.GetRandomColor()
      GraphicsWindow.BrushColor=scolour[i]
      block[i] = Shapes.AddRectangle(90,90)
      blockPosX[i] = 50 + Math.GetRandomNumber(gw-100)
      blockPosY[i] = 50 + Math.GetRandomNumber(gh-100)
      blockVelX[i] = Math.GetRandomNumber(7)
      blockVelY[i] = Math.GetRandomNumber(7)
    EndFor
    
    'Infinite loop moving shapes
    While ("True")
      'Update positions
      For i = 1 To 4
        'Bounce blocks
        If (blockPosX[i] < 45 Or blockPosX[i] > gw-45) Then
          blockVelX[i] = -blockVelX[i]
        EndIf
        If (blockPosY[i] < 45 Or blockPosY[i] > gh-45) Then
          blockVelY[i] = -blockVelY[i]
        EndIf
        'Update position
        blockPosX[i] = blockPosX[i] + blockVelX[i]
        blockPosY[i] = blockPosY[i] + blockVelY[i]
        'Move block
        Shapes.Move(block[i],blockPosX[i]-45,blockPosY[i]-45)
        'Declare sides of square
        left=objectx
        top=objecty
        right=objectx+objectw
        bottom=objecty+objecth
        'Check if square hits blocks
        If blockPosX[i]-45<right And blockPosX[i]+45>left And blockPosY[i]-45< bottom And blockPosY[i]+45> top Then
          GraphicsWindow.ShowMessage("You Have Lost","Game Over")
          GraphicsWindow.Clear()
          Goto begin
          EndIf
      EndFor
      Program.Delay(20)
    EndWhile
    
    Sub OnMouseMove
      objectx=GraphicsWindow.MouseX-objectw/2
      objecty=GraphicsWindow.MouseY-objecth/2
      Shapes.Move(object,objectx,objecty)
    EndSub


    I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'


    • Edited by Math Man Monday, December 3, 2012 3:27 PM
    • Proposed as answer by Math Man Monday, December 3, 2012 10:47 PM
    • Marked as answer by litdevModerator Monday, December 10, 2012 7:28 PM
    Monday, December 3, 2012 3:26 PM

All replies

  • You would need some arrays to store the 10 squares and their position and also probably their velocities.  It depends by what kind of random kind of movement you want how to control them.  To bounce them, the easiest way is just to reverse the sign of the velocity of the square when it it hits a wall.  To detect a collision the easiest is to detect that there is an overlap of the two shapes.  More complicated for a circle and square - there are extensions that can help, but I would start by using overlapping areas.

    Here is some ideas on moving the blocks randomly.

    gw = 800
    gh = 600
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    
    'Create shapes, the positions and velocities
    For i = 1 To 10
      block[i] = Shapes.AddRectangle(90,90)
      blockPosX[i] = 50 + Math.GetRandomNumber(gw-100)
      blockPosY[i] = 50 + Math.GetRandomNumber(gh-100)
      blockVelX[i] = Math.GetRandomNumber(10)
      blockVelY[i] = Math.GetRandomNumber(10)
    EndFor
    
    'Infine loop moving shapes
    While ("True")
      'Update positions
      For i = 1 To 10
        'Bounce blocks
        If (blockPosX[i] < 45 Or blockPosX[i] > gw-45) Then
          blockVelX[i] = -blockVelX[i]
        EndIf
        If (blockPosY[i] < 45 Or blockPosY[i] > gh-45) Then
          blockVelY[i] = -blockVelY[i]
        EndIf
        'Update position
        blockPosX[i] = blockPosX[i] + blockVelX[i]
        blockPosY[i] = blockPosY[i] + blockVelY[i]
        'Move block
        Shapes.Move(block[i],blockPosX[i]-45,blockPosY[i]-45)
      EndFor
      Program.Delay(20)
    EndWhile

    Sunday, December 2, 2012 9:01 PM
    Moderator
  • Thanks, that was a huge help but I'm still having some problems. Why isn't this detecting when the square hits the blocks?

    gw = 800
    gh = 600
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh

    objectw=35
    objecth=35
    objectx=(gw/2)-(objectw/2)
    objecty=(gh/2)-(objecth/2)
    object=shapes.AddRectangle(objectw,objecth)
    Shapes.Move(object,objectx,objecty)

    GraphicsWindow.MouseMove=OnMouseMove

    'Create shapes, the positions and velocities
    For i = 1 To 10
      scolour[i]=GraphicsWindow.GetRandomColor()
      GraphicsWindow.BrushColor=scolour[i]
      block[i] = Shapes.AddRectangle(90,90)
      blockPosX[i] = 50 + Math.GetRandomNumber(gw-100)
      blockPosY[i] = 50 + Math.GetRandomNumber(gh-100)
      blockVelX[i] = Math.GetRandomNumber(7)
      blockVelY[i] = Math.GetRandomNumber(7)
    EndFor

    'Infinite loop moving shapes
    While ("True")
      'Update positions
      For i = 1 To 10
        'Bounce blocks
        If (blockPosX[i] < 45 Or blockPosX[i] > gw-45) Then
          blockVelX[i] = -blockVelX[i]
        EndIf
        If (blockPosY[i] < 45 Or blockPosY[i] > gh-45) Then
          blockVelY[i] = -blockVelY[i]
        EndIf
        'Update position
        blockPosX[i] = blockPosX[i] + blockVelX[i]
        blockPosY[i] = blockPosY[i] + blockVelY[i]
        'Move block
        Shapes.Move(block[i],blockPosX[i]-45,blockPosY[i]-45)
        'Declare sides of square
        left=GraphicsWindow.MouseX-objectw/2
        top=GraphicsWindow.MouseY-objecth/2
        right=GraphicsWindow.MouseX+objectw/2
        bottom=GraphicsWindow.MouseY+objecth/2
        'Check if square hits blocks
        If blockPosX[i]=right Or blockPosX[i]+90=left Or blockPosY[i]= bottom Or blockPosY[i]-90= top Then
          GraphicsWindow.ShowMessage("You Have Lost","Game Over")
          Program.End()
          EndIf
      EndFor
      Program.Delay(20)
    EndWhile

    Sub OnMouseMove
      objectx=GraphicsWindow.MouseX-objectw/2
      objecty=GraphicsWindow.MouseY-objecth/2
      Shapes.Move(object,objectx,objecty)
    EndSub

    Sunday, December 2, 2012 11:40 PM
  • When you move the square with the mouse, objectx and objecty are updated.

    In the loop you don't check if this new position is running into a block.


    Jan [ WhTurner ] The Netherlands



    Monday, December 3, 2012 3:05 PM
    Answerer
  • Here. I've fixed your problem I think. You just used the keyword "Or" where you should have put "And" and misplaced a few of ">" and "<."

    I also changed the amount of blocks to 4 to make it possible to survive.

    gw = 800
    gh = 600
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    begin:
    objectw=35
    objecth=35
    objectx=(gw/2)-(objectw/2)
    objecty=(gh/2)-(objecth/2)
    object=shapes.AddRectangle(objectw,objecth)
    Shapes.Move(object,objectx,objecty)
    
    GraphicsWindow.MouseMove=OnMouseMove
    
    'Create shapes, the positions and velocities
    For i = 1 To 4
      scolour[i]=GraphicsWindow.GetRandomColor()
      GraphicsWindow.BrushColor=scolour[i]
      block[i] = Shapes.AddRectangle(90,90)
      blockPosX[i] = 50 + Math.GetRandomNumber(gw-100)
      blockPosY[i] = 50 + Math.GetRandomNumber(gh-100)
      blockVelX[i] = Math.GetRandomNumber(7)
      blockVelY[i] = Math.GetRandomNumber(7)
    EndFor
    
    'Infinite loop moving shapes
    While ("True")
      'Update positions
      For i = 1 To 4
        'Bounce blocks
        If (blockPosX[i] < 45 Or blockPosX[i] > gw-45) Then
          blockVelX[i] = -blockVelX[i]
        EndIf
        If (blockPosY[i] < 45 Or blockPosY[i] > gh-45) Then
          blockVelY[i] = -blockVelY[i]
        EndIf
        'Update position
        blockPosX[i] = blockPosX[i] + blockVelX[i]
        blockPosY[i] = blockPosY[i] + blockVelY[i]
        'Move block
        Shapes.Move(block[i],blockPosX[i]-45,blockPosY[i]-45)
        'Declare sides of square
        left=objectx
        top=objecty
        right=objectx+objectw
        bottom=objecty+objecth
        'Check if square hits blocks
        If blockPosX[i]-45<right And blockPosX[i]+45>left And blockPosY[i]-45< bottom And blockPosY[i]+45> top Then
          GraphicsWindow.ShowMessage("You Have Lost","Game Over")
          GraphicsWindow.Clear()
          Goto begin
          EndIf
      EndFor
      Program.Delay(20)
    EndWhile
    
    Sub OnMouseMove
      objectx=GraphicsWindow.MouseX-objectw/2
      objecty=GraphicsWindow.MouseY-objecth/2
      Shapes.Move(object,objectx,objecty)
    EndSub


    I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'


    • Edited by Math Man Monday, December 3, 2012 3:27 PM
    • Proposed as answer by Math Man Monday, December 3, 2012 10:47 PM
    • Marked as answer by litdevModerator Monday, December 10, 2012 7:28 PM
    Monday, December 3, 2012 3:26 PM