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:
Hi there, im studying nanoscience at the university in Basel. Today I looked at the topic of intertial and non-inertial reference frames and the existence of fictitious forces. I understand that you call forces real in physics if they appear in interplay. Meaning that a force is real when there is the "actio" partner to the "reactio" partner. If this condition is not satisfied the force is not real. I also understand that if you specifically look at non-inertial reference frames you can...
This has been discussed many times on PF, and will likely come up again, so the video might come handy. Previous threads: https://www.physicsforums.com/threads/is-a-treadmill-incline-just-a-marketing-gimmick.937725/ https://www.physicsforums.com/threads/work-done-running-on-an-inclined-treadmill.927825/ https://www.physicsforums.com/threads/how-do-we-calculate-the-energy-we-used-to-do-something.1052162/
I have recently been really interested in the derivation of Hamiltons Principle. On my research I found that with the term ##m \cdot \frac{d}{dt} (\frac{dr}{dt} \cdot \delta r) = 0## (1) one may derivate ##\delta \int (T - V) dt = 0## (2). The derivation itself I understood quiet good, but what I don't understand is where the equation (1) came from, because in my research it was just given and not derived from anywhere. Does anybody know where (1) comes from or why from it the...
Back
Top