I need to implement a little bit of Ai that can apply an impulse to a chipmunk in order to hit a target. Like a cannon ball being fired from a cannon.
My maths is terrible, as is my physics, but i've been reading up and came up with the following (from this wikipedia entry)
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 / tan(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.
1. This method doesn't seem to take mass into account. Should it? I would have thought that was quite important?
2. Box2d has PTM_RATIO, but I can't find anything like that in chipmunk, so how do my values correspond to the space values in chipmunk?
3. 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. I did find this thread which seems to touch on the same issue, but there's no mention of angles, and it doesn't seem to work in this case either.
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.
cheers
-t