Slow Rush Studios logo,
    depicting an apprehensive-looking snail rushing forward

Slow Rush Studios

◂  Elemental Spellcasting
News index
Building a Spellbar  ▸

Rocking Shields

Contents

Continuing from last week's elemental spellcasting, this week I added support for casting Rock spells and Shield spells.

And, you can now aim spells with the mouse!

Rock Spells

You can cast a rock spell by summoning an Earth element, or a bigger rock by summoning more Earth:

The Rock spell charges up over time: taking more time to cast it results in a faster and more deadly rock.

After hitting an enemy or a short time in contact with the floor (or a few bounces), the rock dissolves into its constituent elements.

So if you combine Oil, Fire and a few Earth elements, then you get:

A homemade molotov cocktail.

But some rock spells (e.g. pure Earth or Earth + Fire) feel a little underwhelming, so I gotta figure out something better for them.

Rocks and Walls

Under the hood, the rock spell creates a moving body with an overridden damage calculation: the faster the Rock is going, the more damage it will do.

But! Using a moving body causes a fun problem: what should happen if you cast a rock while standing next to a wall?

The only wrong answer is to just create the rock inside the wall.

For now I decided to just dissolve the rock straight away if that happens, as a "your spell fizzled"1 behaviour.

But that causes a new problem:

Big rocks aimed horizontally or down slopes will dissolve instantly because they always intersect the ground.

To work around this, I tweak where the rock starts from so bigger rocks start up a little higher when aiming down or across:

Tweaked rock spawning; brown line is the aim direction, and white circle shows where the rock will start.
Shooting straight down (or close to it) has an additional special case so you can shoot down from ladders.

It's not perfect! Sometimes, the higher rock start position causes you to miss enemies down-slope, or your rock spell fizzles when you don't expect because it intersects with a single atom of terrain.

A better approach might be to have the rock charging-up process eat away blocking terrain where possible, and, if that is not enough, move the rock's start position upwards just enough.

Shield Spells

One of the iconic features of Magicka is being able to create a Shield which reflects beam attacks and blocks other spells.

So if you summon a single shield element and cast your spell, you get a barrier shield:

Barrier Shield in action, reflecting beam spells. The reflections can be dangerous!
(Not shown but totally also working: shields stopping arrows and making rocks dissolve.)

Making shields work reliably was a little tricky because shields are only a pixel wide - and fast moving objects tend to go through really thin objects.2

But since shields are so thin, I treat them as an infinitely thin line: if an object is moving from A to B, then I check whether the line segment from A to B intersects with the shield's line segment - that solves the problem reliably.3

Shields expire after a short time, and shrink in size as time goes on; each player is allowed one shield active at a time (but I bypassed that limit for the demo above).

Playable web build‎

You can actually aim spells with the mouse now: hold left Shift and press QWER/ASDF (Shield is E, Earth is D),4 move your mouse, then (maybe hold) left or right click.

Click to focus, then play with keyboard and mouse. Mobile devices may or may not work! Give feedback.

I still need to add spell support for "Shield + another element" and for Lightning-based spells, and Cold doesn't do anything yet either.


1

See What is the origin of the term fizzled?

2

This behaviour is called "tunnelling", and it happens because physics systems only check for overlaps between collider shapes at discrete intervals.

You can decrease the time between the checks for either all shapes (use smaller physics timesteps) or specific shapes (turn on continuous collision detection with substeps), but that costs performance - and guaranteeing that it will never happen costs a lot of performance.

3

Well, it's reliable for sufficiently small objects like the arrows that the goblin archers shoot.

Larger objects are a little more complicated - you can imagine the center of a rock happily missing a shield's line segment while its outer edge passes through the line segment. I haven't tried to solve that yet, but probably Real-Time Collision Detection has the proper answer in it somewhere.

4

I know, I know: Earth should be E and Shield should be S. I just copied these keybindings from Magicka for now.

◂  Elemental Spellcasting
News index
Building a Spellbar  ▸