Calculating a Trajectory

Official forum for the Chipmunk2D Physics Library.
Post Reply
gargantuan
Posts: 1
Joined: Tue Dec 29, 2009 8:03 am
Contact:

Calculating a Trajectory

Post by gargantuan »

I've hacked together the following method that should, given a projectiles initial position, target position and a random velocity, calculate the required angle in order for the projectile to hit a target.

Code: Select all

- (void) calculateShotForTarget:(CGPoint)target from:(CGPoint)launchPos
{
	float x = target.x = launchPos.x;
	float y = target.y = launchPos.y;
	float g = 9.8;
	float v = 100; // this will be picked at random from a range
	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);
	}
	
	// Does anyone know what this does? I thought it may be the answer.
	//cpVect angleCPV = cpvforangle(angle1);
	
	angle1 = angle1 * (180 / M_PI);
	angle2 = angle2 * (180 / M_PI);
		
	// Get the horizontal and vertical velocities
	float vVel = v * sin(angle2);
	
	float hVel = v / tan(angle2);
	
	CGPoint force = cpv(hVel, vVel);
	
        // LaunchData is a convenience class for passing CGPoints around. 
	LaunchData *shot = [[[LaunchData alloc] initWithForce:force andPosition:launchPos] autorelease];
	[[NSNotificationCenter defaultCenter] postNotificationName:PlayerDidFinishAiming object:shot];

	
}
My physics is dire, so I'm stumbling through this with help from wikipedia. The above method results in a very weak impulse being applied to the projectile which I suspect has something to do with chipmunk coordinates vs screen coordinates. I know box2d has the PTM_RATIO, but i've been unable to locate anything like that in chipmunk.

If any of you physics boffins can see an error in the formulae, or if anyone knows of some chipmunk feature that I've not used then I'd really appreciate any help in figuring this out.

cheers
-t
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: Calculating a Trajectory

Post by Tam Toucan »

My physics is also dire and this may be wrong and/or of no help, but I've just been mucking around with a similar thing. I've only just written it, but with my limited testing it seems to work ok

BTW, cpvforangle turns an angle (in radians) into the X,Y components e.g. angle=0 then X=1,Y=0

I have a fixed horizontal velocity and need the Y velocity to travel the known horizontal distance (l_playerPos[0])

Code: Select all

// The X velocity is a fixed value
l_vel.x = m_rockVel.x;
// - Get gravity
getPhysicalWorld()->getGravity(l_gx,l_gy);

// Calc time in the air based on traveling to the player
// at the ROCKVEL_X speed and accounting for gravity (which
// may have a horizontal component
//
// s = ut + 1/2at^2
// s = distance to player (l_playerPos[0])
// u = horizontal vel (l_v.x)
// a = horizontal gravity (l_gx)
// t = time in air => at^2 + 2ut -2s = 0
// Solve the quadratic
// => t = ( -2u +- sqrt(4u^2 + 8as) ) / 2a

float l_bSqrMinus4ac = 4*l_v.X*l_v.X + 8*l_gx*l_playerPos[0];
float l_minus2u = -2*l_v.x;
float l_timeInAir = (l_gx) ? (l_minus2u + sqrt(l_bSqrMinus4ac)) / (2*l_gx) : l_playerPos[0]/l_v.x;

// Calc vertical speed so that for given time in air it will reach the player
//
// s = ut + 1/2at^2
// s = 0 (player is at same Y, so distance travel up == distance travel down => distance change = 0)
// u = vertical vel to calculate
// a = vertical gravity (l_gy)
// t = time in air (calculated above)
// => u = (0 - 1/2at^2)/t
l_v.y = (0 - 0.5*l_gy*l_timeInAir*l_timeInAir) / l_timeInAir;
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Calculating a Trajectory

Post by slembcke »

Calculating the angle given a constant muzzle velocity is fairly hard. When varying the muzzle velocity, the ideal angle is always going to be 45 degrees. Assuming that, calculating the muzzle velocity is just a quadratic equation and isn't too hard. I have the solution for this laying around somewhere... I could dig it up later if that isn't enough to go on.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mcneja
Posts: 4
Joined: Mon Feb 01, 2010 1:56 pm
Contact:

Re: Calculating a Trajectory

Post by mcneja »

For the harder problem of a fixed muzzle velocity where you're trying to determine the correct launch angle, I wrote an article that might be helpful:

http://playtechs.blogspot.com/2007/04/a ... arget.html

Having a fixed horizontal velocity and just solving for the necessary vertical velocity to counteract gravity, as you mentioned, is simpler. That's what I did for boss fights in an old game called "Psi Ops: The Mindgate Conspiracy." Tam Toucan's code looks like the right sort of thing.
Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests