I am trying to throw a ball in an arc, either an arc going left or right.
Here is my code:
var gravity = 2;
this.velocity.y += gravity;
_angle = 5;
var theta:Number;
switch(_direction) {
case "left":
theta = _angle * Math.PI/180;
this.velocity.x = Math.cos(theta) - Math.sin(theta);
break;
case "right":
theta = _angle * Math.PI/180;
this.velocity.x = Math.cos(theta) - Math.sin(theta)
break;
}
this.x += this.velocity.x;
this.y += this.velocity.y;
It doesn’t really look like the ball is “arcing” at all, it seems to be more of a diagonal line?
When throwing you have two components.
A vertical acceleration due to the magics of gravity. This will be ay.
A horizontal component: Without air friction this is a constant velocity.
Let’s say you throw the ball and at the moment of leaving your hand it has a velocity v0 = (v0x, v0y) and is at position p0. Then v0x will be constant for all time.
The speed of the ball at time t would be v(t) = (v0x, v0y + t * ay)
For each tick of your animation, add deltat * v(t) to the current position of the ball and you should be set.
Everytime the ball bounces, you should mirror its velocity vector on the surface it bounced and substract a certain percentage of its total energy (Ekin + Epot, although Epot will be 0 if it is on the ground and the gound is zero potential), in order to get a logarithmic bouncing.
If you want air friction too, just substract a certain small percentage of the total energy with every animation tick.
Here some code, not in ActionScript, but I hope readable. (The parameters to the ctor are both Vector2d; clone() used implicitly but you can guess what it does):