none
Creating a ball object in a tick loop V see below for more accurate detail RRS feed

  • Question

  • Hello, i am trying to create a ball object that will spawn and fling itself for a certain distance in the direction of the mouse (floating blue ball)
    GraphicsWindow.Title= "NARUTO RPG"
    GraphicsWindow.BackgroundColor = "SkyBlue"
    GraphicsWindow.Width = 600
    GraphicsWindow.Height=400
    GraphicsWindow.CanResize = "False"
    
    GraphicsWindow.DrawLine(0, 300, 600, 300)
    head = Shapes.AddEllipse(20, 20)
    headpoint["x"] = 20
    headpoint["y"] = 220
    Shapes.Move(head,headpoint["x"], headpoint["y"])
    GraphicsWindow.BrushColor = "Red"
    leftarm = Shapes.addRectangle(10, 30)
    GraphicsWindow.BrushColor = "White"
    torso = Shapes.AddRectangle(10, 30)
    GraphicsWindow.BrushColor = "Blue"
    rightarm = Shapes.AddRectangle(10, 30)
    Shapes.Move(leftarm,  headpoint["x"]+5, headpoint["y"] +20)
    Shapes.Move(torso, headpoint["x"]+5, headpoint["y"] +20)
    Shapes.Move(rightarm,  headpoint["x"]+5, headpoint["y"] +20)
    GraphicsWindow.BrushColor = "Red"
    leftleg = Shapes.AddRectangle(10, 30)
    GraphicsWindow.BrushColor = "Blue"
    rightleg = Shapes.AddRectangle(10, 30)
    Shapes.Move(rightleg,headpoint["x"]+5, headpoint["y"] +50 )
    Shapes.Move(leftleg,headpoint["x"]+5, headpoint["y"] +50 )
    direction = -1
    GraphicsWindow.KeyDown = move
    GraphicsWindow.KeyUp = stop
    GraphicsWindow.MouseMove = movehand
     reticle = Shapes.AddEllipse(20,20)
    framestart = Clock.ElapsedMilliseconds ' getsthe time
    frames = 60
      frameLength = 1000/frames
    While 1=1
      ProcessKey() 'processes the key at the current frame
      ProcessMouse()
      framedelay()'frame delay
    endwhile
    sub ProcessMouse
      dividebyzero = (mx-( headpoint["x"]+5))
      If dividebyzero = 0 Then 
        dividebyzero = 0.0001
        endif
      degreeofinclination = (my-(headpoint["y"] +20))/(mx-( headpoint["x"]+5))
     angleh = Math.ArcTan(degreeofinclination)
     angleh = Math.GetDegrees(angleh)
     TextWindow.WriteLine(angleh)
    If(mx > headpoint["x"]) Then
      displacementy = Math.Sin(math.GetRadians(angleh))*-10
      displacementx = Math.Cos(math.GetRadians(angleh))*10
    Else
       displacementy = Math.Sin(math.GetRadians(angleh))*10
      displacementx = Math.Cos(math.GetRadians(angleh))*-10
        endif
    Shapes.Rotate(rightarm,angleh-90)
    Shapes.Move(rightarm,  headpoint["x"]+5+displacementx, headpoint["y"] +10 - displacementy)
    
    EndSub
    
    
    Sub movehand
      mx = GraphicsWindow.MouseX
      my=GraphicsWindow.MouseY
      
      Mouse.HideCursor()
      GraphicsWindow.BrushColor="Red"
     
      moving="True"
      Shapes.Move(reticle, mx, my)
      
      
      
      endsub
    Sub move
      pressed = "true"
      
      EndSub
    Sub ProcessKey
      'process the keys once per frame
      If pressed = "True" Then
        key = GraphicsWindow.LastKey
        If key = "D" Then ' headpoint is the "center" point, everything else follows the head. 
        If headpoint["x"] > 390 Then
          
        Else
          headpoint["x"] = headpoint["x"] +3
        EndIf 
      ElseIf key = "A" then
        If headpoint["x"] < 20 Then
          
        Else
          
          headpoint["x"] = headpoint["x"] -3
        EndIf
      EndIf
      Shapes.Move(head,headpoint["x"], headpoint["y"])
      
      angle = angle + (deltaarm*7) ' angle change for arms and legs
      displacementx = angle/5 ' horizontal chage for arms and legs
      displacementy = math.Abs(angle/9) ' vertical change for arms and legs
      displacementforhead = (angle/300) ' vertical change for entire body
      direction = direction
      
      If direction = -1 Then ' direction of arms, -1 means their arms are moving back.
        deltaarm = 1
        If angle > 60 Then
          direction = 1 
        EndIf
      ElseIf direction = 1 then ' arms moving forward. this cam be applied to the legs as well.
        deltaarm = -1
        If angle < -60 then
          direction = -1
        EndIf
      EndIf
      Shapes.Rotate(leftarm, angle)
      'Shapes.Rotate(rightarm, -angle)
      Shapes.Rotate(leftleg, -angle)
      Shapes.Rotate(rightleg, angle)
      
      
      Shapes.Move(leftarm,  headpoint["x"]+5-displacementx, headpoint["y"] +20 - displacementy )
      
      Shapes.Move(torso, headpoint["x"]+5, headpoint["y"] +20)
      Shapes.Move(rightarm,  headpoint["x"]+5, headpoint["y"] +20)
      
      Shapes.Move(rightleg,headpoint["x"]+5-displacementx, headpoint["y"] +50 - displacementy)
      Shapes.Move(leftleg,headpoint["x"]+5+displacementx, headpoint["y"] +50 - displacementy)
      
        
        
        '===========this is the bounds check, makes sure the player doesnt leave the field.
        
        
      EndIf
    EndSub
    
    Sub stop
      pressed="False"
      'Shapes.Move(player, playerx, playery)
       angle = 0 
      Shapes.Rotate(leftarm, angle)
      Shapes.Rotate(rightarm, angle)
      Shapes.Rotate(leftleg, angle)
      Shapes.Rotate(rightleg, angle)
      
      Shapes.Move(leftarm,  headpoint["x"]+5, headpoint["y"] +20)
      Shapes.Move(rightarm,  headpoint["x"]+5, headpoint["y"] +20)
      Shapes.Move(rightleg,headpoint["x"]+5, headpoint["y"] +50 )
      Shapes.Move(leftleg,headpoint["x"]+5, headpoint["y"] +50 )
    EndSub
    
    Sub frameDelay'codingCat is the person to thank for this.
      
      
      'Precise frame timing. More exact that program.delay() because it only
      'pauses for the length of time remiaining in the current frame, rather than for the 
      'entire time slice of the frame. Even with longer, or shorter frame processing
      'times, each fram will still be exactly frameLength in duration.
      While Clock.ElapsedMilliseconds - framestart < frameLength ' if the time right now subtracted from the start time is les than frame length, then keep it.
        'do nothing
        
        
      EndWhile
      framestart = Clock.ElapsedMilliseconds
    EndSub

    . issue is that i want to be able to move the person while it is in motion. See the code and you will see what i mean. if you have any further questions, please ask.

    Matthew

    Sunday, June 12, 2016 10:42 PM

Answers

All replies

  • Hi Matthews! welcome back !!

    i found this little routine , the mouse pointer stick on the ball:

    routine suggested by litdev in this forum in 2010:

    https://social.msdn.microsoft.com/Forums/en-US/5826eefc-503e-4bb1-a8fd-3943d40dc4dc/ball-speed-not-consistent?forum=smallbasic

    _____________________________________________________________________

    ball = Shapes.AddEllipse(50,50)
     GraphicsWindow.MouseMove = OnMouseMove
     mouseMoving = 0
     While ("True")
       If (mouseMoving = 1) Then
         mouseMoving = 0
         Shapes.Move(ball,GraphicsWindow.MouseX-25,GraphicsWindow.MouseY-25)
       EndIf
     EndWhile

     Sub OnMouseMove
       mouseMoving = 1
     EndSub

    ___________________________________________________________________________________

    Sunday, June 12, 2016 11:00 PM
    Answerer
  • Also , a fantastic program wrote by NaoChanON, on the Small Basic gallery.

    program no: mmq961-1

    will help you, this one you throw a basketball with the mouse in the basket, cool !!

    thanks to NaochanON !

    ' Challenge of the month August 2012 Free throw basketball by NaochanON MMQ961-1

    Sunday, June 12, 2016 11:57 PM
    Answerer
  • Thanks, its actually been a while since i have coded in sb. So im glad to know im not that rusty. Erm, i dont know where i would put that, its actually  click event, not a mousemove event. The click will shoot a ball out of the player's hand to the spot where the mouse it (or the moving ball that goes with the mouse cursor). if you could help me find where i would put that, that would be great. Another question, that may answer the question just asked, is would it be a sub routine?, if yes, i would be completely lost on how to do that since im already running like 2 events at the same time that are key registered...

    Matthew

    Sunday, June 12, 2016 11:59 PM
  • Glad that you are back on the horse !!

    So, i find this: when you click the throw button, the ball goes in the basket, so you just have to put somewhere in your graphic window  a button call : '' throw button" and when you click the button the mouse routine detect that clic on the button and then throw the ball.

    here in the program below :

    ' this draw the throw button

    Controls.SetTextBoxText(oAngle, 45)
      oThrow = Controls.AddButton("Throw", 700, 200)

    ' this is the routine checking if button is click

    Sub WaitButton
      Controls.ShowControl(oThrow)
      notClicked = "True"
      Controls.ButtonClicked = OnButtonClicked
      While notClicked
        Program.Delay(200)
      EndWhile
      Controls.HideControl(oThrow)
    EndSub

    Sub OnButtonClicked
      notClicked = "False"
    EndSub

    see that perfect example in a program wrote by in 2012 by Nonki Takahashi

    ' Basketball 0.3

    program no: TKL110-0 

    Monday, June 13, 2016 1:22 AM
    Answerer
  • cool. Ok so i got this so far V. would this work? (its not complete yet)

    While 1=1

      ProcessKey() 'processes the key at the current frame
      ProcessMouse()
      processclick()
      framedelay()'frame delay
    endwhile

    Sub processclick
      first = "true"
      If clicked = "true" or notfinished ="True"Then
        clicked ="false"
        If first = "true" Then
          force = Shapes.AddEllipse(5, 5)
          first = "False"
        Else

        EndIf
       

        startx = headpoint["x"] +(Math.Cos(math.GetRadians(angleh))*20))
        starty = headpoint["xy"] +(Math.sin(math.GetRadians(angleh))*20))  

        addingorsubdist = (startx) ' going to add an end distance maybe like 200 pixs in the direction
        addingorsubdistheight = (starty)
    'not yet complete
      endsub

    the only thing i need to add is the moving program behind it, Not in a while loop, because i want to be able to move while i am shooting from my hand(like iron man). So would this idea be sound, the ball is going to be constantly moving and there will be a check where it will find where the ball is and determine if it has moved square root((200^2 +200^)) pixels

    good idea?

    edit:

    take out all the if's other than the clicked = true


    Matthew


    Monday, June 13, 2016 1:27 AM
  • ok, import code is FBQ317.

    only issue, no animation, and the left side is slightly closer than the right one... why?

    how would i do the animation?

    and i keep getting a divide by zero error for no reason(i put in a if statement called dividebyzero, should catch it.. but its not, why?)


    Matthew


    • Edited by JetFire3201 Monday, June 13, 2016 1:57 AM Divide by zero error
    Monday, June 13, 2016 1:52 AM
  • FBQ317-0

    • added animation to ur ball
    • changed movement by keys left/right
    • Marked as answer by JetFire3201 Monday, June 13, 2016 2:47 PM
    Monday, June 13, 2016 3:58 AM
  • ok, import code is FBQ317.

    only issue, no animation, and the left side is slightly closer than the right one... why?

    • Animation: You should update force's position in the main loop. Cf. Math for Kinetics.
    • Left side: Do you mean DrawLine in line 7?  This line starts from the left side (x = 0).

    how would i do the animation?

    and i keep getting a divide by zero error for no reason(i put in a if statement called dividebyzero, should catch it.. but its not, why?)


    Matthew


    • I recommend you to insert Text.WriteLine() to show what values causes divided by zero.

    Nonki Takahashi

    Monday, June 13, 2016 4:23 AM
    Moderator
  • and i keep getting a divide by zero error for no reason(i put in a if statement called dividebyzero, should catch it.. but its not, why?)


    Matthew


    degreeofinclination = (my-(headpoint["y"] +20))/(mx-( headpoint["x"]+5)+dividebyzero) '<---------- add dividebyzero here

    Monday, June 13, 2016 4:40 AM
    Answerer
  • @Matthew

    NaochanON solution for the div/zero problem work !

    for the animation problem:

    Tryhest solution is great !

    he add  shapes.animate in your Sub processclick

         Shapes.Move(force startx starty)
        Shapes.Animate (force enddistx,enddisty 800)


    also excellent explanation about Math of kinetics by Nonki , great blog !
    • Edited by YLedEditor Monday, June 13, 2016 1:34 PM
    Monday, June 13, 2016 1:28 PM
    Answerer
  • Thanks for the help. I appreciate all the help. 

    Matthew

    Monday, June 13, 2016 2:46 PM