# Create quaternion based on where a unit is hit (shield effect as in Starcraft 2) [duplicate]

I'd like to implement a shield effect like that done for Protoss units in Starcraft 2. I'm guessing that the technique involved uses a partial sphere that is rotated to reflect where a unit is hit.

Assuming this is the idea, how can I create a quaternion to rotate this model to achieve the desired effect? My collision detection system conveniently relies on sphere-sphere intersection.

Edit: this is pretty much a dupe of this, which has a pretty thorough answer.

• For those of us who aren't terribly familiar with Starcraft, can you link to a video or screenshot showing the effect you're referring to? – chaosTechnician Oct 12 '11 at 20:00
• I'm having a really hard time finding a video. Here's a rather poor picture that demonstrates it: starcraft2-elites.com/?attachment_id=173 – notlesh Oct 12 '11 at 20:29
• Imagine a sphere where your unit or building is in the middle. This sphere is invisible, but when it's hit from the outside there is a sort of "splash effect" on the surface. This splash effect has to happen where the incoming projectile hits. That's the question. – Tor Valamo Oct 12 '11 at 23:19
• @chaosTechnician: It's not actually Starcraft specific: Just imagine rendering the effect of a spherical energy shield diffusing energy from an impact. streetjesus.info/the_apprenticeship/wp-content/uploads/2011/07/… or images.wikia.com/starwars/images/b/be/… – Jimmy Oct 13 '11 at 17:30
• @Jimmy yeah, that's what I figured but wasn't certain until seeing a picture. Also, +1 for working Jessica Alba into the thread. :) – chaosTechnician Oct 13 '11 at 17:41

You can use the same "look at" rotation techniques you would use to have a camera look at a target object; just rotate the partial sphere as if it were the camera. I don't know enough about quaternion math to give a specific answer for quaternion computation, but the way you construct the coordinate system is this:

1. Inputs: unit position, hit position, 'up' vector.
2. Subtract hit position from unit position to get 'look at' vector.
3. Normalize it.
4. Take the cross product of 'up' and 'look at' to get the 'right' vector.
5. Replace 'up' with the cross product of 'right' and 'look at'.
6. Use these vectors (which are basis vectors for a coordinate system) to construct your rotation; they are the columns-or-rows-depending of a rotation matrix, and I don't know what they are for a quaternion.

The 'up' vector can be arbitrary, but it must not be parallel to the 'look at' vector; it determines the rotation of your shield geometry around the hit-direction axis. By the hairy ball theorem, there is no way to "smoothly" pick an up vector, so just do it in some arbitrary way.

• @stephelton just to add something, you don't really need to use a quaternion in this case - a matrix will probably do it. – Jonathan Dickinson Oct 13 '11 at 7:55
• It'll need to be a quaternion with my system, but you're right. – notlesh Oct 13 '11 at 18:05

Does your quaternion library have something that's akin to "LookAt" in it's API? If so, author the content so the effect is on the forward vector and just tell your sphere to look at the object that caused the hit effect.

Not sure if it helps, but I was thinking about what you're asking, specifically "create a quaternion to rotate this model" etc..

I think the way it's done in SC2 is quite simple. There are two ways to attack a unit or building... either by air or by ground.

All the ground attacks on a shield is animated at the same height. So it's essentially just a pivoted animation, duplicated depending on all the angles it's being attacked from. Note that a single marine attacking a nexus (really low dps), the shield damage is barely visible, so it's almost transparent so that if a lot of units attack from the same side, it won't just become an opaque block of bad graphics. This sort of proves that it's duplicated for every single hit.

Same with air attacks, except the animation has a slightly bigger "surface area", but it still pivots horizontally around the unit.

Air units are the same, except flipped vertically (they can't be attacked from the top, but from the bottom instead).

So SC2 is quite 2d-oriented with this problem. If your game is more flexible in the Z-dimension then you may need a different solution.

Edit: I also thought of a different way, instead of duplicating the animation, you have a logarithmic opacity setting... so that the more damage is done, the more opaque it is, but being logarithmic it will reach a point where you won't see much difference if you add MORE damage. This way you can also prevent a big block of bad graphics.

• Thanks for expanding on the idea, I think I'll go play some sc2 to study this more ;) – notlesh Oct 13 '11 at 2:40
• Remember to play on high or ultra graphics, otherwise it IS an opaque block of bad graphics :P – Tor Valamo Oct 13 '11 at 11:16