Working on Cannonball simulator

  • Context: Undergrad 
  • Thread starter Thread starter Pain
  • Start date Start date
  • Tags Tags
    Simulator
Click For Summary
SUMMARY

The discussion centers on developing a catapult mini-game, specifically focusing on simulating projectile motion using physics formulas. The user, a hobbyist game programmer, is utilizing a subset of C++ to implement the cannonball trajectory calculations, but is struggling with the physics involved, particularly in translating angles to radians and calculating distances accurately. The code provided demonstrates the calculation of projectile position based on elapsed time, cannon velocity, and launch angle, but issues with distance and trajectory accuracy persist. The user aims to enhance their understanding of physics to improve their game programming skills.

PREREQUISITES
  • Understanding of basic physics concepts related to projectile motion
  • Familiarity with C++ programming or similar languages
  • Knowledge of trigonometric functions and their applications in programming
  • Experience with game development principles and mechanics
NEXT STEPS
  • Study the physics of projectile motion, focusing on the equations of motion and their application in game development
  • Learn how to implement trigonometric functions in C++ to ensure accurate angle conversions
  • Research methods for simulating wind resistance and its effects on projectile trajectories
  • Explore game physics engines that can simplify the implementation of realistic projectile motion
USEFUL FOR

This discussion is beneficial for hobbyist game developers, programmers interested in physics simulations, and anyone looking to enhance their understanding of projectile motion in game design.

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:

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
21
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
Replies
33
Views
6K
  • · Replies 13 ·
Replies
13
Views
4K