A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: 3D trajectory (more physics)

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    4

    3D trajectory (more physics)

    Okay, I've been working on this for awhile and can't seem to get it right.

    What I'm trying to do is send a projectile (think soccer ball, or ballistics without a cannon, or firing mechanism) on a 3d trajectory.

    What I have:

    time (I'm using a loop that returns how much time has past from first frame to the current)

    Angle of height (think x and y angle. basically this is the angle to the horizon)

    Angle of swivel (this is the angle away from the viewer. I begin with my object in the lower left hand corner of screen, so a 45 degree (yes I know flash uses radians) angle would be swivel INTO your screen away from the X axis.

    Velocity

    Initial position (obviously)

    Scale (I think this is what is ultimately screwing me up, so anyone who can explain a trajectory in terms of scale would be appreciated.)

    I KNOW I have all the variables needed including gravity, no wind resistance, yet I can't seem to get this to work. PLEASE don't just post a few physics formulas. . . I've got more than I can handle as is. I just can't make sense of it. If anyone needs the source code, I'll be happy to share it.

    Thanks

  2. #2
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    think of it like this:

    Gravity acts at 9.81 units (meters) per specified time period ( in this case a second) squared.

    So if I let go of an object, it's initial speed is 0 and every second, its speed ( acceleration over time ) is increasing by 9.81 units squared:

    vectors can be used to define a direction and magnitude so it seems to make sence to use that here right?

    so, we make a vector:

    Actionscript Code:
    class Vector3 {
     public var x: Number;
     public var y: Number;
     public var z: Number;
     public function Vector3( x: Number = 0, y: Number = 0, z: Number = 0 )
     {
       this.x = x;
       this.y = y;
       this.z = z;
     }

     // an add function
     public static function add( v1: Vector3, v2: Vector3 ): Vector3
     {
       return new Avector3( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z );
     }
    }

    You need to define your axis, I'll use the usual, x for left and right on the screen, y for up and down and z for towards or away from the screen.

    Also remember any vector can be added to another vector, so, lets say you add an initial powerful force and some air resistance we can do this easily by adding these each time we do an update.. lets do it!!!

    I need to get some timer going! I'll use flash.utils.getTimer; and make a nice update function!

    Actionscript Code:
    // add some sprite to apply some forces to. Maybe you want to give it a nice gfx.
    var img: Sprite = new Sprite();
    addChild( img );

    // lets just update each frame?!
    addEventListener( Event.ENTER_FRAME, update );

    private static const GRAVITY: Number = 9.81;

    // we can create an initial force to fire it high into the sky
    var boomVector: Vector3 = new Vector3( 50, -50, 0 );

    // update the vectors
    var timeDelta: int = getTimer();
    public function update( e: Event ): void
    {
      var yComponent: Number = Math.pow( ( timeDelta / 1000 ) * GRAVITY, 2 );
      timeDelta = getTimer() - timeDelta;

      // we can construct our gravity vector here intside this update, because it changes over time! :)
      var gravityVector: Vector3 = new Vector( 0, yComponent, 0 );

      // then we could add them all together
      var gravityPlusBoom: AVector = AVector.add( boomVector, gravityVector );

      // now we reposition the sprite
      img.x += gravityPlusBoom.x;
      img.y += gravityPlusBoom.y;
    }


    OK, I wrote this directly into here, so possible syntax issues, but hopefully it'll get you started (consider it pseudo code) ! Good luck

    RipX
    Last edited by RipX; 10-12-2010 at 06:04 PM.

  3. #3
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    P.S. Considering, scale is difficult to understand what you mean since your comment is ambiguous. The vectors values should not change with scaling, the position of your objects should be scaled instead. For this in a 3d sence, you would use a scale matrix, this is usually defined as a 3x3 matrix or 4x4 homogeneous matrix such that the values are as follows (the next matrix is commonly used, but you can do it column or row major, up to you):

    sx, 0, 0, 0
    0, sy, 0, 0
    0, 0, sx, 0
    0, 0, 0, 1

    KEY
    ++++++++++++++++
    sx = scale x
    sy = scale y
    sz = scale z
    ++++++++++++++++

    now your vectors can be multiplied by this matrix, ultimatly resulting in your position information.

    RipX
    Last edited by RipX; 10-12-2010 at 06:12 PM. Reason: removal of ambiguity re matrix definition.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center