gamecreator Posted December 23, 2011 Share Posted December 23, 2011 Hi there. I'm making a simpler version of Worms and I need code (C preferred) to get a projectile from X1, Y1 to X2, Y2 but I'm horrible with math. But it gets a bit more complicated than that. Depending on the situation, there may not be enough velocity to hit the target. So the problem is, given V, is there an angle that exists that will hit the target. If so, what is it (I think there may be two so I would need the steeper of the two). I think this may be the formula I'm looking for (from the wiki): Also, if I understand it right, that assumes X1, Y1 is at 0, 0 so I think that needs to be subtracted from (added to??) the target. Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
Canardia Posted December 23, 2011 Share Posted December 23, 2011 You don't need math with LE. Just use a pivot and do a PointEntity(pivot,target), then you can read the pivot's direction using EntityRotation(). Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Pixel Perfect Posted December 23, 2011 Share Posted December 23, 2011 If I understand this correctly he's not asking for the angle to the target as a straight line, which is what your suggestion would give but rather the vertical angle the projectile would need to be fired at given an x,y target position and a velocity to ensure the projectile hits it. Presumably this is a parabola flight path. Quote Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++ Link to comment Share on other sites More sharing options...
gamecreator Posted December 23, 2011 Author Share Posted December 23, 2011 Thank you Metatron but yes, Pixel is right. I'm looking for code that would calculate the steepest angle that would fire something like this (over hills): float getangle(float x1, float y1, float x2, float y2, float velocity) { float angle; ... ? return angle; } Quote Link to comment Share on other sites More sharing options...
Canardia Posted December 23, 2011 Share Posted December 23, 2011 I would use physics for that. That's what made Angry Birds famous too There's incredibly little physics based games on the market, and they will have huge success if made well. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Scott Richmond Posted December 24, 2011 Share Posted December 24, 2011 No I believe he will be using physics but I think he's currently programming the AI so it knows what angle and velocity to shoot the projectile at to hit the target. Gamecreator - To answer your question regarding the (x,y) I believe that they are relative variables. That is to say that the position of A is relative to that of B. So: x = targetA.x - targetB.x // That'll get the meters between the launch site and the target y = targetA.y - targetB.y // That'll get hte height of the launch site relative to the target You'll have to mess around with the order of subtraction there I'm guessing, so you don't get any negative values. Do you know how you're going to solve whether it will hit geometry or not? Quote Programmer, Modeller Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64 Visual Studio 2008 | Photoshop CS3 | Maya 2009 Website: http://srichnet.info Link to comment Share on other sites More sharing options...
gamecreator Posted December 24, 2011 Author Share Posted December 24, 2011 Scott, I have collision detection taken care of with EntityCollisionCallback, so I'm set there. Right now my computers are just shooting randomly but I'd love for them to not be so aimless (ha ha). Quote Link to comment Share on other sites More sharing options...
Scott Richmond Posted December 24, 2011 Share Posted December 24, 2011 Right well that equation should get you that, but it won't tell you whether it'll reach its target. You'll need to test each angle and velocity with a collision check to find one that won't hit geometry. Quote Programmer, Modeller Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64 Visual Studio 2008 | Photoshop CS3 | Maya 2009 Website: http://srichnet.info Link to comment Share on other sites More sharing options...
gamecreator Posted December 24, 2011 Author Share Posted December 24, 2011 The good thing is that there are second chances. The computer, like the player, is allowed to miss. But if the projectile doesn't hit, it would be because the shot can't be made (because there won't be any wind or overhead obstructions). In that case, the computer will need to switch to a different target. Or a bigger weapon. Quote Link to comment Share on other sites More sharing options...
Josh Posted December 24, 2011 Share Posted December 24, 2011 Looks like a calculus problem. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Naughty Alien Posted December 24, 2011 Share Posted December 24, 2011 http://www.xinventions.com/main/spud/motion_equ.htm or http://zonalandeducation.com/mstm/physics/mechanics/curvedMotion/projectileMotion/generalSolution/generalSolution.html or http://www.pha.jhu.edu/~broholm/l5/node3.html pretty much straight forward.. Quote Link to comment Share on other sites More sharing options...
Flexman Posted December 24, 2011 Share Posted December 24, 2011 Angry Birds gets mentioned a lot and while yes it's a simple physics game, its success (and they made 50 games prior to that) is the huge effort and experience applied to presentation then giving it away to create it as a brand. For ballistics see this for a quick simple example of what you want. http://warpycode.wordpress.com/2009/10/10/aiming-a-projectile-uphill/ Quote 6600 2.4G / GTX 460 280.26 / 4GB Windows 7 Author: GROME Terrain Modeling for Unity, UDK, Ogre3D from PackT Tricubic Studios Ltd. ~ Combat Helo Link to comment Share on other sites More sharing options...
Road Kill Kenny Posted December 24, 2011 Share Posted December 24, 2011 Haven't had a look at those sites that NA posted but this is what I came up off the top of my head. Its basically calculus and Newton's law F=MA or rather F=MU for instantaneous speed (U initial velocity) Apologies that this is hand written. I have simplified the calculus part by using pre-determined physics acceleration equations. Hope you find it useful. using these equations you should be able to determine the force required for a given mass and the angle to apply the force at. good luck some definitions: s- distance (m) v- final velocity (m/s) u- initial velocity (m/s) a- acceleration (m/s2) t - time (s) Quote STS - Scarlet Thread Studios AKA: Engineer Ken Fact: Game Development is hard... very bloody hard.. If you are not prepared to accept that.. Please give up now! Link to comment Share on other sites More sharing options...
gamecreator Posted December 25, 2011 Author Share Posted December 25, 2011 Thank you all very much, especially Ken for the generous contribution. That gives me a lot to look through and try to implement when I have a little time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.