This isn't too hard once you realise the way to do it is to split the movement into it's horizontal and vertical components. For simplicity we'll assume 3d, and you can translate that into your 2d perspective however you like.

Your inputs will be your start and end positions, and gravity. Your output you're trying to find will be the launch velocity you need to give to the projectile. Once you've got that, you can simulate it's movement under gravity the normal way and it'll hit the target.

First, you need to solve horizontally:

Ye olde movement equation:s = ut + 0.5at^2

Horizontally we have no acceleration, so eliminate 0.5at^2

We can calculate distance s by finding the vector between the start and end points, then calculating it's length.

Then we pick an arbitrary speed that we want it to move along the ground, substitute in and we can work out t, the time it will take to go from start point to end point.

Vertically:s = ut + 0.5at^2

We want to start and end at the same height, so distance s is zero.Acceleration is just gravity (careful of the sign).Time we know from the horizontal calculation.

That simply gives us an equation with just unknown vertical speed u:u = (-0.5 * gravity * time)^2) / time

You calculate that u, and that's your vertical speed. So now your launch velocity is just the horizontal speed and the vertical speed. Done!

Now the throwing speed is constant, so no delays at the beginning or end.Now you just have to calculate an halve circle (z) depending on where the granade is between the enemy and the player.Add the value of z to the y position whe drawing the granade and done.

Now the throwing speed is constant, so no delays at the beginning or end.Now you just have to calculate an halve circle (z) depending on where the granade is between the enemy and the player.Add the value of z to the y position whe drawing the granade and done.

It's better to at least reuse sin and cos between the two, but it's even better to just calculate the direction vector instead. One sqrt is better than an atan2 + sin + cos.

Ok guys =) First i wish to thank you really much.Thanks for your answers.Great forum, Great people !Ok, so, i saw yours answers, but It’s not yet absolutely good for me.I note I’m in the good to topic : Newbie and debugging ^^

publicvoidupdate() {if (x > Configuration.WINDOW_WIDTH || y > Configuration.WINDOW_HEIGHT || y < 0 || x < 0 ) game.removeShotsPlayerEntity(this);else {// It is here where i must update the position of the grenade.// I delete my linear function (the straight shot)// But I didn’t know how to implement yours code… Sorry…// Do you think the structure is good ?// Do you think I’ll clear my pb… I wish}}

If you could still help me...

-----------------------------------------------------I am French, be indulgent with my written words please

You have x and y, and you know the direction to the target, the distance (break out sqrt(dx*dx + dy*dy) into a variable) and the (2D) velocity. From this you can calculate the time it takes from it to get from the thrower to the target (time = distance / velocity). Since what you're trying to do is throw a grenade in 3D, you have to do the calculations in 3D. We just add a third coordinate Z which represent the height the grenade is above ground. We then do the calculations that Orangy Tang posted to calculate the start velocity U (u = (-0.5 * gravity * time)^2) / time). With this we can use the original formula (s = ut + 0.5at^2) to calculate at what height the grenade is at at a certain time t.

//Then just draw the grenade at x, y-z. y-z moves the grenade "up" based on the z coordinate, which should give you the effect of speed.drawGrenade(x, y-z);

GRAVITY should be a constant for the gravity applied to all grenades. It controls the force the grenade is thrown at upwards (higher gravity must be countered by a higher throwing force). You'll have to experiment. You can even try with 0 gravity or negative gravity. =S

You need to work in three dimensions. Use parametric projectile physics to get a simple and accurate projectile path. Your overhead view probably emulates an angled view instead of a direct overhead view. (If you were using a direct overhead view, then you would basically only see the tops of characters' heads and you could throw grenades on straight line paths instead of parabolas. You're probably using an angled view, of course, judging by the sign.) Use theagentd's code. You may need to change the coefficients of y - z part depending on the view angle and other projection-related stuff.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org