Grapple Hook
The idea is twofold when you shoot out a grapple
If it hits a static object (i.e. world geometry) you can pull yourself to it
used as a traversal mechanic
If it hits a movable object (or somehting light) you can pull it to YOU
used as a combat tool (pull enemies in and kick / stab them)
used in puzzles to bring objects close to you or turn on/off switches
Physical Setup
The player has a Grapple Anchor which is a slot to which the grapple originates from
Then a sphere is shot out from that location to where the player is looking for some distance to see if it connects with anything
if it does
and the object is static it pulls the anchor to the location
(WIP) and the object is dynamic it attaches itself to the object and retracts to the anchor
if it doesn't, it retracts back

Projectile Motion problem
This is a classic projectile motion problem where we know everything except the angle needed for our object (player) to reach the target (grapple).
In our case we know the start (anchor) and end (attachment) location, as well as we can define a constant initial velocity - so all we have to do is solve for the angle

We have the projectile motions equations
x = x0 + (v0x * t)
y = y0 + (v0y * t) + 1/2gt^2
We want to achieve an equation in the form ax^2 + by +c so we can use the quadratic formula similar to the derivations done in this GDC Talk: Math for Game Programmers: Predictable Projectiles (which has a link to the slides for formula derivation).
After substituting and solving for time we achieve the equation
g*x^2 / 2v0^2 T^2 + x T + (g*x^2 / 2v0^2) - y = 0
which is in the for aT^2 + bT + c = 0 which is the quadratic formula, where T = tan θ
I break up each a, b, c individually in code to help ease readability, but at the end of the day all we're doing is solving the quadratic and taking the smaller value which results in the lesser angle needed (since there are commonly two solutions to projectile motion problems)


Last updated