Phaser 4: Throw a Grenade That Lands Exactly on the Cursor
A grenade that flies in a nice high arc is easy. A grenade that flies in a nice high arc and lands exactly where the player clicked is where most attempts go wrong. It falls short on long throws, overshoots on short ones, or drifts a few pixels every time.
There are two clean ways to do it. Both land on the cursor every time. Which one you want depends on whether anything else in your game needs real gravity.
Try the live demo, press 5 for the grenade and click anywhere. That's method 1.
Method 1: a straight line with an arc on top
This is the technique the demo uses (its numbers are tuned to the screen size). The trick is to stop thinking about physics and split the throw into two parts:
- Slide the grenade in a straight line from the hand to the target.
- Lift it off that line by an arc that is zero at both ends.
Because the lift is zero at the end, the grenade always finishes on the target, whatever the distance.
throwGrenade(from, to) { const dist = Phaser.Math.Distance.BetweenPoints(from, to); const g = this.add.image(from.x, from.y, 'grenade'); g.from = from; g.to = to; g.t = 0; // 0 = in the hand, 1 = landed g.flight = 0.45 + dist / 1000; // seconds: longer throws take longer g.arc = 60 + dist * 0.25; // apex height in pixels: longer throws go higher this.grenades.push(g); } update(time, delta) { const s = delta / 1000; this.grenades = this.grenades.filter((g) => { g.t = Math.min(1, g.t + s / g.flight); g.x = Phaser.Math.Linear(g.from.x, g.to.x, g.t); g.y = Phaser.Math.Linear(g.from.y, g.to.y, g.t) - Math.sin(g.t * Math.PI) * g.arc; if (g.t < 1) return true; this.explode(g.x, g.y); g.destroy(); return false; }); } Math.sin(t * Math.PI) is 0 at the start, 1 at the halfway point and 0 again at the end. That is what makes the landing exact.
Two small touches make it feel right:
- Scale the flight time and the height with distance. A short lob and a long throw shouldn't take the same time or reach the same height.
- Cap the range. In the demo the throw is clamped to a maximum distance, so clicking far away throws as far as the arm can, in the right direction.
Want a true parabola instead of a sine curve? Swap the arc line for 4 * g.t * (1 - g.t) * g.arc. It also lands exactly, with a slightly sharper peak.
Method 2: real gravity
If grenades need to bounce off walls, or other things in your game fall under gravity, you want a real launch velocity. You pick how long the throw should take, and the maths gives you the velocity that lands on the target in exactly that time.
With dx and dy the distance to the target, T the flight time in seconds, and g your gravity in pixels per second squared (positive, because y points down in Phaser):
launchVelocity(from, to, T, g) { const dx = to.x - from.x; const dy = to.y - from.y; return { vx: dx / T, vy: dy / T - 0.5 * g * T, // negative = upwards }; } The horizontal speed covers the distance in time T. The vertical speed is whatever it takes to cover dy while gravity pulls down the whole way. The - 0.5 * g * T part is the extra upward push that cancels the fall.
A longer T gives a higher, floatier throw. For a target at the same height, the peak is g * T * T / 8 above the hand, so you can pick T from the arc height you want.
The gotcha: your update loop can miss by pixels
Method 2 is exact on paper. Whether it's exact in your game depends on how you move the grenade each frame.
A very common update loop adds gravity to the velocity, then moves by the new velocity:
g.vy += gravity * s; g.x += g.vx * s; g.y += g.vy * s; In a test (60 frames per second, a 1-second throw, gravity 900), this landed 7.5 pixels low. Every frame it falls slightly further than the real curve.
Add half a step of gravity to the movement and it lands dead on:
g.x += g.vx * s; g.y += g.vy * s + 0.5 * gravity * s * s; g.vy += gravity * s; Same test: a miss of 0.0000 pixels.
If you use Phaser's Arcade physics bodies instead of moving the grenade yourself, check the landing spot with a quick test throw. Don't assume it matches the formula to the pixel.
Which one should you use?
- Method 1 if the grenade just needs to look good and land where you clicked. It is short, easy to tune, and the landing can't drift.
- Method 2 if the grenade has to live in a physics world: bouncing, rolling, or being hit by things on the way.
The demo uses method 1, with a short blinking fuse after landing and an explosion radius check against the targets.
How it was tested
Method 1 is the technique running in the live demo. Both methods were also run in Node.js over 100,000 random throws, with random distances, arc heights, flight times and gravity. Both landed on the target to within a rounding error (under a billionth of a pixel). The 7.5-pixel miss above came from the same test, stepping the throw frame by frame.
Get the grenade
The plasma grenade in the demo comes in three tiers, with the pin point and pivot stored in the atlas JSON, as part of Retro Arsenal. I made it, along with the pistol, rifle, shotgun and energy blade. If you want to start with something free, the laser pistol is in the first tutorial.
How this post was made: Written by Khaya, drafted with help from Claude (AI). Tested by Claude (AI), not yet by Khaya. The arc code is the code running in the live Retro Arsenal demo, and both methods were checked in Node.js over 100,000 random throws.
原文:https://www.a8q9.cn/en/article/phaser-4-throw-a-grenade-that-lands-exactly-on-the-cursor-0c7300.html