I need to implement a little bit of Ai that can apply an impulse to a rigid body in order to hit a target. Like a cannon ball being fired from a cannon. I'm using Chipmunk Dynamics for the physics engine.
My maths is terrible, as is my physics, but i've been reading up, and with a little help from SO and the following from this wikipedia entry, I got this
float x = target.x = launchPos.x;
float y = target.y = launchPos.y;
float g = 9.8;
float v = 100;
float angle1, angle2;
float tmp = pow(v, 4) - g * (g * pow(x, 2) + 2 * y * pow(v, 2));
if(tmp < 0){
NSLog(@"No Firing Solution");
}else{
angle1 = atan2(pow(v, 2) + sqrt(tmp), g * x);
angle2 = atan2(pow(v, 2) - sqrt(tmp), g * x);
}
// Split the velocities
float vVel = v * sin(angle2);
//NSLog(@"Vertical Velocity: %f", vVel);
float hVel = v / cos(angle2);
//NSLog(@"Horizontal Velocity: %f", hVel);
CGPoint force = cpv(hVel, vVel);
Which should give me the angle, from which I can calculate the horizontal and vertical velocities needed to launch the projectile.
However, it's not working, which doesn't surprise me at all for a number of reasons. Firstly because I'm terrible at Maths and Physics, but also because I'm confused by a couple of other things.
This method doesn't seem to take mass into account. Should it? I would have thought that was quite important? But then, I studied art at college, so I might be wrong about that.
Box2d has PTM_RATIO, but I can't find anything like that in chipmunk, so how do my values correspond to the space coordinates in chipmunk?
I know of radians and how they differ to degrees, and how to convert between the two. But which should I be using here? should I be converting angle1 and angle2 to degrees? Even if I do, It still doesn't work.
In summary, there's a lot about physics and chipmunk that I don't understand. So I'm here, asking for help.
Is there something in chipmunk that I can use to figure this out, or if anyone has had to figure this out themselves, I'd really appreciate some help.