# Working on Cannonball simulator

1. Jan 1, 2012

### Pain

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 (Text):

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

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.

2. Jan 1, 2012

### Pain

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 [Broken]

which equates to the following codes
Code (Text):

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: May 5, 2017