Working on Cannonball simulator

  • Thread starter Pain
  • Start date
  • Tags
    Simulator
In summary, the goal of the conversation is to create a catapult mini-game using physics and math. The speaker is a hobbyist game programmer who is struggling to understand the physics behind it. They have tried using resources and comparing their code to others but have not been successful. They are now seeking outside help and have downloaded a working code to compare. The game is not set up for animating projectile paths, so the speaker is using visual effects instead. They are also using a language similar to C++ but with limited features. They are also interested in adding crosswinds to the game.
  • #1
Pain
2
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
  • #2
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:

Related to Working on Cannonball simulator

What is a Cannonball simulator?

A Cannonball simulator is a computer program that simulates the trajectory and motion of a cannonball, taking into account various factors such as initial velocity, angle of launch, air resistance, and gravity.

Why would someone work on a Cannonball simulator?

A Cannonball simulator can be used for various purposes, such as understanding the physics behind cannonball motion, predicting the trajectory of a cannonball in different scenarios, or as a tool for teaching and learning about projectile motion.

What skills are needed to work on a Cannonball simulator?

To work on a Cannonball simulator, one would need a strong understanding of physics, specifically in the areas of projectile motion, mechanics, and kinematics. Proficiency in computer programming and mathematical modeling would also be necessary.

What are the challenges of working on a Cannonball simulator?

One of the main challenges of working on a Cannonball simulator is accurately modeling the complex physics involved in cannonball motion. This may require advanced mathematical equations and algorithms. Additionally, accounting for external factors such as wind and terrain can also be challenging.

How can a Cannonball simulator be used in real-world applications?

A Cannonball simulator can be used in various real-world applications, such as designing and testing artillery weapons, predicting the trajectory of projectiles in sports such as baseball or golf, or even in military training exercises. It can also be used in educational settings to demonstrate and visualize the principles of projectile motion.

Similar threads

  • Introductory Physics Homework Help
Replies
11
Views
808
Replies
5
Views
3K
Replies
5
Views
2K
  • Programming and Computer Science
Replies
26
Views
3K
  • Introductory Physics Homework Help
Replies
21
Views
2K
  • Other Physics Topics
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Electrical Engineering
2
Replies
43
Views
4K
Back
Top