Working on Cannonball simulator

  • Thread starter Thread starter Pain
  • Start date Start date
  • Tags Tags
    Simulator
AI Thread Summary
The discussion centers on developing a catapult mini-game using physics for projectile motion, inspired by games like Pocket Tanks and Angry Birds. The programmer, who has experience with Neverwinter Nights 2, is struggling with the mathematical and physics concepts necessary for accurate projectile trajectories, particularly due to a lack of calculus background. They are currently comparing their code to existing Java programs and using Excel to visualize trajectories, but are facing issues with distance calculations and cannon facing conversions. The goal is to eventually incorporate features like wind resistance and dynamic weather effects into the game. The programmer seeks to improve their understanding of physics and math to enhance their game development skills.
Pain
Messages
2
Reaction score
0
Well the goal is actually a catapult mini-game, but its the same thing as the cannonball trajectory on every tutorial site out there.

I am a hobbyist game programmer, and i spend a lot of time developing features for Neverwinter Nights 2 - and adding features to Community Script Library which i maintain as a resource for other programmers of that game. I really don't have enough math background for this, well it's not that i don't know math as i do a lot of advanced math, it's that i don't know how to read the physics formula and never took calculus, and i am reading and re-reading physics for dummies ( which has a page showing a cannonball ), and comparing my code to any open code i can compare what i have to. I am a bit frustrated as i started trying to figure this out over a year ago, and it's still not working, and i usually just am able to get things working through trial and error and determination.

At this point i think i need to just get some outside help to aim myself in the right direction. Just downloaded a working java programs source code, comparing it to what i have shown, and seeing similar logic.

This is not homework, however a major goal for myself is to understand physics/math and how it works so i can use it in my game programming, and make a mini-game in the RPG similar to pocket tanks, scorch, or even angry birds. To a degree it does not have to be accurate even, but i'd like to learn this properly. If it helps moving this to homework, or moving it to computers, not sure exactly if this is correct spot to post this as i am really trying to fill in holes in my education.

Now this game is not set up for animating projectile paths exactly, so what i am doing is doing smoke-puff visual effects along that projectile path. Just to determine the position that puff of smoke has to be at, at a given point in time given by elapsed time, and then loop thru the projectiles flight incrementing by small increments of time and basically plot it's position in game, each time checking for a collision. So all i need here is to input the relevant values for elapsed time, and then i create a visual at that moment in time.

The language is similar to C++, well it's a subset of that language with very limited features in comparison, but I've added in my functions to make it very similar to excel. I tried to make some of these things make sense with the variable names, hopefully they are understandable to the non-programmer, or at least to someone competent in excel.

Code:
location CSLPlotProjectile( location lCannonLoc, float fElapsedTime = 0.0f, float fCannonVelocity = 50.0f, float fLaunchAngle = 45.0f )
{
	vector vProjectilePosition; // this determines the projectiles position and has x, y and z ( vertical )
	vector vCannonPosition = GetPositionFromLocation( lCannonLoc );
	float fCannonFacing = GetFacingFromLocation( lCannonLoc );
	object oArea = GetAreaFromLocation( lCannonLoc );
	
	float fVvelocityVD = fCannonVelocity*cos(CSLDeg2Rad(fLaunchAngle)); // xY
	float fDistance = ( fCannonVelocity*cos(CSLDeg2Rad(fLaunchAngle)) )*fElapsedTime; // x
	float fVvelocityVZ = fCannonVelocity*sin(CSLDeg2Rad(fLaunchAngle))-CSL_ACCELERATION_FROM_GRAVITY*fElapsedTime; // vY
	float fPositionZ = fCannonVelocity*sin(CSLDeg2Rad(fLaunchAngle))*fElapsedTime-0.5f*CSL_ACCELERATION_FROM_GRAVITY*fElapsedTime*fElapsedTime; // or Y
	
	// now i adjust the position, i get the height, and convert the distance relative to the facing so it can fire in any direction.
    vProjectilePosition.z = vCannonPosition.z + fPositionZ;
    vProjectilePosition.x = vCannonPosition.x + ( fDistance * cos(fCannonFacing) ); // change in x ( fDistance * cos(fAngle) )

    vProjectilePosition.y = vCannonPosition.y + ( fDistance * sin(fCannonFacing) );  // change in y ( fDistance * sin(fAngle)

	return Location(oArea, vProjectilePosition, fCannonFacing);
}

And to make things clear on the variable types...

location = data containing vector, facing and area
vector = x and y, 0 is edge of map to size of map in meters, and z for vertical
facing = degrees
CSL_ACCELERATION_FROM_GRAVITY = 9.81f
CSLDeg2Rad = fAngle*(PI/180.0f);

When used it seems that distance is not working. Ie the puffs go up and down a bit but it has no range at all, regardless of input values as if my distance was always 0.

I'd like to add in crosswinds ( i have a control weather spell, dynamic weather ) at some point to this, but that should not be too hard if i can get the basics here working.
 
Physics news on Phys.org
I think i got it working, redid the code in excel ( same basic formulas really ) and did a x y plot chart for the trajectories. I think the issue is the facing for the cannon was not being converted to radians.

http://dl.dropbox.com/u/1172450/Physics.xlsx

which equates to the following codes
Code:
vector vProjectilePosition; // this determines the projectiles position and has x, y and z ( vertical )
vector vCannonPosition = GetPositionFromLocation( lCannonLoc );
float fCannonFacing = GetFacingFromLocation( lCannonLoc );
object oArea = GetAreaFromLocation( lCannonLoc );

// the following is not needed at all
float fVvelocityVD = fCannonVelocity*cos(CSLDeg2Rad(fLaunchAngle)); // xY
float fDistance = ( fCannonVelocity*cos(CSLDeg2Rad(fLaunchAngle)) )*fElapsedTime; // x
// the following is not needed at all
float fVvelocityVZ = fCannonVelocity*sin(CSLDeg2Rad(fLaunchAngle))-CSL_ACCELERATION_FROM_GRAVITY*fElapsedTime; // vY
float fPositionZ = fCannonVelocity*sin(CSLDeg2Rad(fLaunchAngle))*fElapsedTime-0.5f*CSL_ACCELERATION_FROM_GRAVITY*fElapsedTime*fElapsedTime; // or Y

vProjectilePosition.z = vCannonPosition.z + fPositionZ;
vProjectilePosition.x = vCannonPosition.x + ( fDistance * cos(CSLDeg2Rad(fCannonFacing)) ); // change in x ( fDistance * cos(fAngle) )
vProjectilePosition.y = vCannonPosition.y + ( fDistance * sin(CSLDeg2Rad(fCannonFacing)) );  // change in y ( fDistance * sin(fAngle)

Now I've got to figure out wind resistance (drag based on mass) and prevailing winds and how to integrate into the above.
 
Last edited by a moderator:
The rope is tied into the person (the load of 200 pounds) and the rope goes up from the person to a fixed pulley and back down to his hands. He hauls the rope to suspend himself in the air. What is the mechanical advantage of the system? The person will indeed only have to lift half of his body weight (roughly 100 pounds) because he now lessened the load by that same amount. This APPEARS to be a 2:1 because he can hold himself with half the force, but my question is: is that mechanical...
Some physics textbook writer told me that Newton's first law applies only on bodies that feel no interactions at all. He said that if a body is on rest or moves in constant velocity, there is no external force acting on it. But I have heard another form of the law that says the net force acting on a body must be zero. This means there is interactions involved after all. So which one is correct?
Thread 'Beam on an inclined plane'
Hello! I have a question regarding a beam on an inclined plane. I was considering a beam resting on two supports attached to an inclined plane. I was almost sure that the lower support must be more loaded. My imagination about this problem is shown in the picture below. Here is how I wrote the condition of equilibrium forces: $$ \begin{cases} F_{g\parallel}=F_{t1}+F_{t2}, \\ F_{g\perp}=F_{r1}+F_{r2} \end{cases}. $$ On the other hand...
Back
Top