I used to know the maths, been awhile since I've studied kinematics but I can probably guess most of it.
If I'm visualising the arc correctly you have 2 variables ( angle and initial velocity ) as well as the position of the player ( which is technically constant in this situation ). As there are 2 variables you end up with effectively infinite combinations, so it can't be solved easily. If you pick an arbitrary value for the angle ( say 45 degrees ) and just vary the power there should only be 1 solution, so we can solve it.
I tried to work out a way to do it if the player is on a different height to the enemy, but couldn't figure it out ( guess I'm a bit rusty ).
Consider vertical movement to the top of the arc ( where the vertical velocity will be 0 and acceleration will be negative g )
0 = u + ( -g ) * t
u = g * t
t = u / g
We also know that as there is no air resistance that no horizontal declaration occurs, hence when considering the distance travelled initial and final velocity are the same
As the angle is 45 degrees the initial horizontal and vertical velocity are the same. The arc is symmetrical so we know that t to the top of the arc is exactly half the total time. So let's combine the equations.
s = u * (2 * (u / g))
s = 3 * u / g
s * g = 3 * u
u = (s * g) / 3
Little g is a constant, and we can calculate the horizontal displacement trivially so you can figure out the initial velocity quite easily. I'm not sure how your actually simulating this, so you might have to fudge the math a little to match up. Changing the target elevation relative to start break the symmetry of the arc, which makes things more complicated to work out.
I think my math is right, but it's untested and I haven't done much like this for around a decade
Sometimes I surprise myself. Try this. Just make a Cannon sprite (with an image point 1 at the end of the barrel), a Bullet sprite with bullet behavior and a Badman enemy sprite with drag and drop behaviour. Works perfectly and really cool. bullets allays hit the badman as long as in range. sorry I cant upload anything as Im at work. I just copied the formula off Wikipedia
| Global number RangeX = 0
| Global number AltitudeY = 0
| Global number GravityG = 5000
| Global number BulletVelocityV = 3000
| Global number TanAngleA = 0
| Global number AngleA = 0
+ System: Every tick
-> System: Set RangeX to Badman.X-Cannon.X
-> System: Set AltitudeY to Cannon.Y-Badman.Y
-> System: Set TanAngleA to ((BulletVelocityV^2)+((√((BulletVelocityV^4)-(GravityG×((GravityG×(RangeX^2))+(2×AltitudeY×(BulletVelocityV^2))))))))÷(GravityG×RangeX)
-> System: Set AngleA to atan(TanAngleA)
+ System: Every 0.3 seconds
-> Cannon: Spawn Bullet on layer 0 (image point 1)
-> Bullet: Set Bullet gravity to GravityG
-> Bullet: Set Bullet speed to BulletVelocityV
+ System: Every tick
-> Cannon: Set angle to -AngleA degrees
Nepeo NetOne - hehe, you might actually have triggered me to take up math again, and that is an achievement itself guys. :D I did not recall all these factors and it might have been a somewhat more simple tutorial. But I will certainly dig into it and show you what it ended up like. Much much appreciated and cheers guys! :0)
Arh mann, thank you NetOne! That is just as great as it could be. I owe you one m8. :)
There are 1 visitors browsing this topic (0 users and 1 guests)