Jump to content
Sign in to follow this  
rihapat

Dev Diary #306 Projecting Projectiles to Snowballs

Recommended Posts

Ahoy Ylanders!

As you might have gathered from the previous programming dev diary, changes always carry the risk of introducing bugs – and this goes double for last minute changes. Programmers are conditioned to avoid risks where possible; after all, it’s our job to make sure that this huge machine of a game works as expected. But in some situations – such as when a winter festival is to be held for the first time and the game lacks throwable snowballs – you just have to pick up the gauntlet and roll with it despite the short timeframe, because you feel that the fun potential is worth the risk.

DD_306_Steam_1920x1080_02.jpg

We already had throwing controls complete for grenades, but wanted the snowballs to behave more like an arrow does, just hitting a target and disappearing instead of exploding violently. Sometimes, programming new features is just like LEGO, you take existing pieces and creatively combine them in new ways, and that’s it. There’s also another way programming is like LEGO – sometimes to get what you want, the only way is to tear the current construction down and rebuild from scratch. Fortunately, for snowballs it was the first case, because we have already invested time into making projectiles work well beforehand.

So let's have a quick look at how projectiles actually work in Ylands! There are two main approaches to simulating motion – kinematics and dynamics. Kinematics is where we directly describe the motion via an equation based on position, velocity, and acceleration. Dynamics behaves more closely to how the actual physics works, by applying various forces to an object, which can then generate motion based on its physical properties – mass, inertia, shape, etc.

Looks like dynamics must be the way to go, being the more accurate option, right? Well, there's a catch. Due to its complexity, dynamic motion is much harder to predict, analyze, and make deterministic – key properties for a multiplayer game, since they allow us to have exactly the same projectile trajectory for all players. In fact, most motion in Ylands is kinematic, except for vehicles and ragdolls, which are too complex for kinematics. Choosing a simpler but a more controllable approach where possible is actually very common in game development! So we opt to simulate projectiles via kinematics, but we need an equation for projectile motion. Very fast projectiles such as bullets can be thought of as having a constant velocity, and thus moving on a line. Having an initial position P₀ and velocity v, their position at time t after firing can be easily calculated as P = P₀ + v ⋅ t. In fact, we can take bullets even further, thinking of their movement as instantaneous – their velocity is so large that they can reach any point we shoot at immediately after they are fired. This simplifies the simulation to checking whether any object lies in the line of fire between the origin point and the target point, which the physics engine can conveniently calculate for us via a raycast query.

The simplified equation can only take us so far however, because it excludes the effects of a very common force - gravity. With bullets, we could ignore it because the initial velocity was so large that we hit the target before gravity has a significant impact on the trajectory. But that won't work for slower moving objects such as arrows, grenades, and, you've guessed it, snowballs. What gravity does is it applies uniform downward acceleration (gravitational acceleration) to the object, building up velocity over time. The equation for uniformly accelerated motion is P = P₀ + (a ⋅ t²) / 2 where a is the acceleration, in our case the gravitational acceleration g. If we combine this with the effect of initial velocity as seen on the bullets, we get the final equation P = P₀ + v ⋅ t + (g ⋅ t²) / 2. This is a quadratic equation, meaning that the final trajectory will be a parabolic curve – which is what we expect from snowballs and arrows.

Once we have the equation for calculating projectile position at time t, it becomes a simple problem of tracking how much time has elapsed since the shot was fired and moving the projectile to the calculated position based on current time. Except now we are ignoring all the obstacles along the way! First thing that comes to mind would be checking for collisions at the current position – if we overlap with some object, we have just hit it and can deal damage to it.

Unfortunately, this won’t work for fast projectiles and thin obstacles – the way the game simulation works, we can only update the projectile position after a fixed amount of time passes from the previous update. If we just check the previous and current positions there might be no collisions, but only because we have already fully passed through the obstacle – a phenomenon known as “tunneling”. But hey, we’ve already solved this for bullets! We can again look at the previous and current position and check if anything lies between these two points via a physics engine raycast. If we hit something, we determine whether the obstacle stops the projectile movement – in which case we stick the projectile into it and apply damage – or if the projectile should pass through the obstacle unhindered.

And that’s the gist of it! This small bit of kinematics allows you to throw a snowball (or a grenade if you’re not feeling particularly festive) in the face of your fellow Ylanders. It also allowed us to implement gun and bow aim assist for mobile and gamepad controls in 2.2, so stay tuned for that – and stay classy!

  • Like 1
  • Fire 2

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×