|
-
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|