Answered Shape Collision Detection

  • Tuesday, August 07, 2012 10:40 PM
     
     
    Hi Again! I was wondering if there was a short, sweet, and snappy way to detect if a circle shape touched a image shape. Any code will be credited. Thank you.

All Replies

  • Tuesday, August 07, 2012 11:54 PM
     
     Answered

    How about this?

    Rct = Shapes.AddRectangle(50,20)  
    GraphicsWindow.FontSize=20
    msg=Shapes.AddText(" ")
    GraphicsWindow.BrushColor="red"
    For i = 1 To 10
      BD=20+Math.GetRandomNumber(30)
      Ball[i] =  Shapes.AddEllipse(BD,BD)      '  create balls
      Shapes.Move(Ball[i],Math.GetRandomNumber(640),-100-Math.GetRandomNumber(100)) ' initial position
    EndFor

    While "true" 
      Shapes.move(Rct,GraphicsWindow.MouseX,400)    '   move   rectangle   slide Left or Right
      For i = 1 To 10
        Shapes.Move(Ball[i], shapes.GetLeft(Ball[i]), shapes.Gettop(Ball[i])+BD/10)   '  Ball  falls
        If Math.Abs(Shapes.GetLeft(Rct)-Shapes.GetLeft(Ball[i]))<50 And Math.abs(Shapes.GetTop(Rct)-Shapes.Gettop(Ball[i]))<20 Then ' if catch
          NN=NN+1
          Sound.PlayClick()
          Shapes.move(Ball[i] ,Math.GetRandomNumber(640),-Math.GetRandomNumber(100)) '   Ball  initial position (random position)
        elseIf shapes.Gettop(Ball[i])>500 Then
          MM=MM+1
          Shapes.Move(Ball[i], Math.GetRandomNumber(640),-Math.GetRandomNumber(100) )'   Ball  initial position (random position)
        EndIf
      EndFor
      Shapes.SetText(msg,"Hit count=  "+NN+ " Total= "+(MM+NN))    '  show hit  number and  total number
      Program.Delay(10)
    endWhile

    • Marked As Answer by Noah Buscher Wednesday, August 08, 2012 12:42 AM
    •  
  • Wednesday, August 08, 2012 12:42 AM
     
     
    Wow! This is exactly what I needed! Thank you so much!
  • Wednesday, August 08, 2012 12:59 AM
    Answerer
     
     

    Or this type of thing:

    If ballx > recx And ballx < recx + Width And bally > Recy And bally < Recy + height Then


    Zock77

  • Wednesday, August 08, 2012 1:14 AM
     
     

    The ball is constantly moving... 

    Here is a link to the semi-finished program: http://smallbasic.com/program/?CJN303.

    I don't know where I could put the code, because I can only use one timer. Can You help? I would also like to know how to find the x and y position of the asteroid. I know I spelled Asteroid wrong in the game. Sorry! Thank you for any help.

  • Wednesday, August 08, 2012 4:32 AM
    Answerer
     
     

    There is no way of finding the x and y with shape.anamate.

    You will have to use something like

    Ball = Shapes.AddEllipse(10,10)
    x = GraphicsWindow.Width
    y = GraphicsWindow.Height / 2
    While 1 = 1
      Program.Delay(5)
      x = x - 2
      Shapes.Move(Ball,x,y)
      If x < 0 Then
        y = Math.GetRandomNumber(GraphicsWindow.Height)
        x = GraphicsWindow.Width
        EndIf
      EndWhile

    Then you can use the 'x' And 'y' of the ball.


    Zock77

  • Wednesday, August 08, 2012 4:42 PM
     
     
    Ok. That works!