Results 1 to 3 of 3

Thread: Ball Bouncing Trig

  1. #1
    New Member
    Join Date
    May 08
    Posts
    2

    Ball Bouncing Trig

    ello, i am trying to have a ball bounce around the screen but i want to be able to draw lines and have the ball bounce off of the line accordingly. Unfortunately i am only in Pre-calc and have not learned enough to be sufficient. I've been trying to decipher the physics in this ACTIONSCRIPT CODE
    Code:
    ACTIONSCRIPT:
    
       1.
          _root.attachMovie("arrow", "arrow", 10001);
       2.
          _root.attachMovie("bounce", "bounce", 1000);
       3.
          arrow._x = 150;
       4.
          arrow._y = 150;
       5.
          bounce._x = 150;
       6.
          bounce._y = 150;
       7.
          arrow.onEnterFrame = function() {
       8.
              mousex = _xmouse-150;
       9.
              mousey = (_ymouse-150)*-1;
      10.
              angle = Math.atan(mousey/mousex)/(Math.PI/180);
      11.
              if (mousex<0) {
      12.
                  angle += 180;
      13.
              }
      14.
              if (mousex>=0 && mousey<0) {
      15.
                  angle += 360;
      16.
              }
      17.
              bounce_angle = 180-angle;
      18.
              if (bounce_angle<0) {
      19.
                  bounce_angle += 360;
      20.
              }
      21.
              _root.angletext.text = "Angle: "+angle;
      22.
              _root.bouncetext.text = "Angle: "+bounce_angle;
      23.
              this._rotation = angle*-1;
      24.
              bounce._rotation = -bounce_angle;
      25.
          };
    but i can't seem to get it to work.
    This is the code that i have so far.
    Code:
    Dim mousex, mousey As Double
    Dim AendX, AendY As Double
    Dim linelength As Single
    Dim angle As Single
    Const Pi As Double = 3.1415926
    Dim AngleRadians As Double
    Dim AngleDegrees As Double
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    mousex = X
    mousey = Y
    End Sub
    
    Private Sub Timer1_Timer()
    arrow.X2 = mousex
    arrow.Y2 = mousey
    AendX = mousex - linelength
    AendY = mousey - linelength
    linelength = Sqr((arrow.X2 - arrow.X2) ^ 2 + (arrow.Y2 - arrow.Y1) ^ 2)
    Label1.Caption = linelength
    AngleDegrees = Atn(mousey / mousex) / (Pi / 180)
    Label2.Caption = AngleDegrees
    End Sub
    Right now i am trying just to get the core trig out of the way using this

    The x2,y2 coords follow the mouse and i'm trying to find the angle which the line makes with the flat line(landscape)

    Please help me and please try to explain the trig easily

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: Ball Bouncing Trig

    Moved To Games and Graphics Programming
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 08
    Location
    Sol 3
    Posts
    325

    Re: Ball Bouncing Trig

    Ah... Trigonometry. Bane of the unwary.

    Here's how you can determine the angle given by any two points, in radians:

    Code:
    angle = Math.Atan2((Point1.X - Point2.X), (Point1.Y - Point2.Y))
    To work out the bounceangle from two angles, think about the logic involved: The bounce angle is the entry angle plus x*2 (see attachment).

    So you need to work out the ground normal, which is easy: simply add PI/2)radians or 90 degrees to your ground angle.

    Then take away the entry angle from your ground normal to give you "x", and add x*2 to your entry angle to get your bounce angle.

    Hope this helps,
    Qu.
    Attached Images Attached Images  
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •