Here are some notes on how I’d go about it, and most of the math worked out. Hopefully I’ve provided sufficient explanations.
The 3D physics of the ball is probably the easiest. We already have xy and we can do z with a instance variable. Just add three other instance variables to keep track of velocities: vx,vy,vz. Also we can use a variable g for gravity.
Then the motion can be done with:
Add g*dt to vz
Set x to x+vx*dt
Set y to y+vy*dt
Set z to z+vz*dt
The result is parabolic motion on the z axis. You can make it bounce off the ground with:
— set z to -abs(z)
You can control where the ball will land by setting the initial velocities.
t = distance(x,y, goalx, goaly)/speed
Vx = (goalx-x) /t
Vy = (goaly-y) /t
Vy = -g*t
To see the z motion we need to rotate everything around in 3D somehow. You can do that on the same objects, but it’s probably simpler to just position new objects from the old ones. It turns out a 3D rotation is basically the same as a 2d one.
Rotate on the YZ plane:
a = the amount to rotate
y, z = the point to rotate
cy, cz = the center of rotation
NewY = (y-cy)*cos(a)-(z-cz)*sin(a)+cy
NewZ = (y-cy)*sin(a)+(z-cz)*cos(a)+cz
We can then add perspective fairly easily. It’s basically just dividing x and y by z.
X,y,z = the position
Cx, cy = the center of perspective
Fov = field of vision. 300 is a decent default.
Eye = camera distance from scene.
NewX = (x-cx)*fov/(z+eye) +cx
NewY = (y-cy)*fov/(z+eye) +cy
Scale = 1*fov/(z+eye)
Perspective is wonky when behind or too close. Increase eye till everything is in front.