Special Effects

8 02 2009

This is one of the last core parts of the game, at least when it comes to the Arenafighting (we got a lot of lobby, networking and boardgame to take care of). This is also one of the best parts as we will be able to handle explosions, lightnings, sparkles and other effects. I have decided to create an EffectEngine similar to the ShotEngine but to tweak it and keep it a little more open as effects will probably act and look very different depending on the effect we want to create.

First, as usual, I start with an Interface:

package interfaces
{
	public interface IEffect
	{
		function update():void
		function updateDying():void
		function remove():void
	}
}

As you can see, the effects will not have many common functions, but the ones they have, the EffectEngine will use. Let’s build an abstract class of the Effects, using the Interface we just made:

package effects
{
	import interfaces.IEffect;

	// abstract
	public class AEffect implements IEffect
	{
		public var life:int
		public var flag_Dying:Boolean
		public var flag_Dead:Boolean

		public function AEffect(life:int)
		{
			this.life = life
			flag_Dying = false
			flag_Dead = false
		}

		public function update():void
		{
			// here the effect will be updated each frametick
		}

		public function updateDying():void
		{
			// if flag_Dying==true , update with this function
			// instead of the normal update()
			// this is to be able to smoothly remove effects
			// when they are done
		}

		public function remove():void
		{
			// when updateDying is done (flag_Dead==true), remove effect
		}

	}
}

The only thing we add in this abstract class is 3 variables. time is the time the effect will be visible in millisecs. Now we are ready to create our EffectEngine.

package engine
{
	import effects.AEffect;
	import managers.Time;

	public class EffectEngine
	{

		static private var instance:EffectEngine
		private var aEffects:Array

		// ************ SINGLETON CLASS *************
		public function EffectEngine()
		{
			init()
		}

		public static function getInstance():EffectEngine
		{
			if (EffectEngine.instance == null)
			{
				EffectEngine.instance = new EffectEngine();
			}
			return EffectEngine.instance;
		}

		private function init()
		{
			aEffects = new Array()
		}

		public function addEffect(effect:AEffect)
		{
			//Add a new effect into the effectlist
			aEffects.push(effect);

			// different from the other engines, the effects
			// has their own addChild procedure as they looke very
			// different to each other.
		}

		public function updateEffects():void
		{
			var tempEffect:AEffect
			var flagDelete:Boolean

			for (var i:int = aEffects.length-1 ; i>-1 ; i--)
			{
				flagDelete = false	// reseting deleteflag.
				tempEffect = aEffects[i]

				if (tempEffect.flag_Dying == false)
				{
					// if effect is alive and kicking!
					tempEffect.update()
					if (tempEffect.life < 0)
					{
						tempEffect.flag_Dying = true
					}
					tempEffect.life -= Time.getInstance().timeElapsed
				}
				else
				{
					// if effect is about to die out
					tempEffect.updateDying()
					if (tempEffect.flag_Dead == true)
					{
						tempEffect.remove()
						flagDelete = true
					}
				}

				if (flagDelete == true)
				{
					trace("Removing effect from list")
					aEffects.splice(i,1) // removing shot from the shot list.
				}
			}
		}
	}
}&#91;/sourcecode&#93;

As usual, it is a Singleton-class preventing several instances of EffectsEngine being created. Also, The normal 'add' function is there just like the shotEngine. Biggest difference is in the updateEffects() function, hopefully it is explaining itself. The procedure of an effect is this:
First it gets created and then immediatly added to the EffectEngine.
The effect updates itself using update() until 'life' has reached 0.
Then the effect update the updateDying() instead until , flag_Dead == true
Last the effect is removed from the list and is running the function remove().

Now I will create two effects that will need some kind of explanation. One Explosion (that is actually two effects in one, I'll explain later) and one BombDust that will dust up the arenaground where the explosion has been.
Let's start with the Explosion :) Here is my visual idea of creating it:

<img class="alignnone size-full wp-image-215" title="explosion" src="https://papergem.files.wordpress.com/2009/02/explosion.jpg" alt="explosion" width="510" height="250" />

I will use two planes. One that is aligned to the ground giving a lightEffect on the ground, and one that is a "billboard". A Billboard is a plane that is always facing the camera, no matter where the camera is placed. The Billboard will have an animated movieclip where I use an explosion I rendered in 3DSMax. The groundaligned plane will just have a BitmapMaterial with an image I created in Photoshop. I will scale this image back and forth to simulate an intensity increase and decrease of the explosion.

One important thing I want to stress right now before we get to coding is that png and alpha-channels doesn't match good with performance and optimization. In some projects it is very convinient to use alpha-channels and transparency but when creating a PV3D-game you will immediatly choke the system with more than a few alphatransparencies on the screen.

My solution to this is to use the blendmode ADD. ADD 'adds' it's RGB values to the normal RGB-values giving a "lighter" effect whereever the RGB is more than 0,0,0. If you didn't catch that just remember this: Whatever on the image you don't want to impact the screen, use 0,0,0. Hmm I maybe should show you the images instead:

<img class="alignnone size-full wp-image-216" title="explosionground1" src="https://papergem.files.wordpress.com/2009/02/explosionground1.jpg" alt="explosionground1" width="90" height="89" /><img class="alignnone size-full wp-image-217" title="010" src="https://papergem.files.wordpress.com/2009/02/010.jpg" alt="010" width="128" height="128" />

Wherever it is black, it will be invisible. The brighter the rgb is, the more it will lighten up the actual screen, AND most important, this is a faster way to create lighteffects with than using alphachannels, transparency and png's. Let's look at our implementation of the Explosion.

package effects
{
    import flash.display.BlendMode;
    import managers.Layers;
    import org.papervision3d.materials.BitmapMaterial;
    import org.papervision3d.materials.MovieMaterial;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.view.layer.ViewportLayer;
    import se.xcom.math.Degrees;

    public class Explosion extends AEffect
    {
        private static var layerIndex:int = 1
        private var airExp:Plane
        private var groundExp:Plane
        private var groundCount:int
        private var layerAir:ViewportLayer
        private var layerGround:ViewportLayer

        public function Explosion(posX:Number,posY:Number, posZ:Number)
        {
            super(1100);
            init(posX,posY,posZ)
        }

        private function init(posX:Number,posY:Number,posZ:Number)
        {
        // create round sphere explosion in air
            var expMat:MovieMaterial = new MovieMaterial(new mcExplosion1(),false,true)
            airExp = new Plane(expMat,1000,1000)
            airExp.x = posX
            airExp.y = posY
            airExp.z = posZ
            airExp.lookAt(View3D.scope.camera)

            layerAir = new ViewportLayer(View3D.scope.viewport,airExp)
            layerAir.blendMode = BlendMode.ADD
            layerAir.layerIndex = layerIndex
            Layers.EFFECTAIR.addLayer(layerAir)

        // create the explosionlight on ground
            var groundMat:BitmapMaterial = new BitmapMaterial(new bmpGroundExplosion1(0,0))
            groundExp = new Plane (groundMat,600,600)
            groundExp.x = posX
            groundExp.y = 0
            groundExp.z = posZ
            groundExp.rotationX = 90

            layerGround = new ViewportLayer(View3D.scope.viewport,groundExp)
            layerGround.blendMode = BlendMode.ADD
            layerGround.layerIndex = layerIndex++
            Layers.EFFECTGROUND_EXP.addLayer(layerGround)

            View3D.scope.scene.addChild(airExp)
            View3D.scope.scene.addChild(groundExp)

        }

        override public function update():void
        {
            airExp.lookAt(View3D.scope.camera)
            airExp.pitch(180)

            groundExp.scaleX = 1+Degrees.dSin(groundCount)*1
            groundExp.scaleY = 1+Degrees.dSin(groundCount)*1
            groundCount += 10
        } 

        override public function updateDying():void
        {
            this.flag_Dead = true
        }

        override public function remove():void
        {
            View3D.scope.scene.removeChild(airExp)
            View3D.scope.scene.removeChild(groundExp)
            Layers.EFFECTAIR.removeLayer(layerAir)
            Layers.EFFECTGROUND_EXP.removeLayer(layerGround)

        }

    }
}

Some explanation here is needed. In my Layers.as class I have created a lot of different layers and put them in a, in my opinion, good indexed order. Now if I just put my objects directly in one of those layers I would get a very strange effect where two explosions would be drawn upon each other like transparent squares. I want the light of the effects to blend with each other if several explosions are on the screen and that is why I put every object in a single unique layer, put the correct blendmode on them and finally put those layers on the parent layer in Layers.as
Yes it could take some time to get your head around, but hey, it works. This is this typical thing that is hard to explain unless you’ve been there yourself.
Well! The explosion now works! This one doesn’t have any long fading when it’s time to “die” but is removed immediatly.

As we’re on it, let’s create a simpler effect, the BombDust.as

package effects
{
	import flash.display.BlendMode;

	import managers.Layers;

	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.layer.ViewportLayer;

	public class BombDust extends AEffect
	{
		private static var layerIndex:int = 1
		private var layer:ViewportLayer
		private var airExp:Plane
		private var dust:Plane
		private var groundCount:int

		public function BombDust(posX:Number,posZ:Number)
		{
			super(8000);
			init(posX,posZ)
		}

		private function init(posX:Number,posZ:Number)
		{
		// create dustPlane
			var dustMat:BitmapMaterial = new BitmapMaterial(new bmpBombDust(0,0))
			dust = new Plane (dustMat,600,600)
			dust.x = posX
			dust.y = 0
			dust.z = posZ
			dust.rotationX = 90
			layer = new ViewportLayer(View3D.scope.viewport,dust)
			layer.blendMode = BlendMode.MULTIPLY
			layer.layerIndex = layerIndex++
			Layers.EFFECTGROUND_DUST.addLayer(layer)
			View3D.scope.scene.addChild(dust)
		}

		override public function updateDying():void
		{
			this.flag_Dead = true
		}

		override public function remove():void
		{
			View3D.scope.scene.removeChild(dust)
			Layers.EFFECTGROUND_DUST.removeLayer(layer)
		}
	}
}

Instead of the ADD-blenmode, this one uses the MULTIPLY, darkening the area instead of brighten it up.
bombdust

Now its just a few lines of code that must be implanted in the GrenadeClass just to spawn these effects whenever the grenade explodes.

override public function killByAge():void
        {
            var exp:Explosion = new Explosion(this.x,this.y,this.z)
            var dust:BombDust = new BombDust(this.x,this.z)

            EffectEngine.getInstance().addEffect(exp)
            EffectEngine.getInstance().addEffect(dust)
        }

Try out the link at the new page above (yes in the menu, called Archon2160). If you’re lucky, there might be a build there where you can see how the effects are triggered. Have fun and hope you learn something. I always do!

Advertisements




Adding Guns and Explosives

7 02 2009

It’s been a while since I wrote about the game in here and as I’ve previously mentioned, I will not cover every single line of code in this blog.

The simple reason is that I would have no time left for developing Archon2160 if I were to comment every adjustment of the previous classes and additions of small elementary things. Instead I will try to focus of the problems I face and how I solve them, trying to be as tutorial-based and educative as possible.

Now, I promised guns and bullets long ago and what would an action game be without that? Previously I created that ShotEngine.as class taking care of every single lethal object, but as usual it has been slightly modified. Today it looks like this:

package engine
{
	import interfaces.IShot;

	import units.AUnit;

	public class ShotEngine
	{

		static private var instance:ShotEngine
		private var aShots:Array

		// ************ SINGLETON CLASS *************
		public function ShotEngine()
		{
			init()
		}

		public static function getInstance():ShotEngine
		{
			if (ShotEngine.instance == null)
			{
				ShotEngine.instance = new ShotEngine();
			}
			return ShotEngine.instance;
		}

		private function init()
		{
			aShots = new Array()
		}

		public function addShot(shot:IShot)
		{
			//Add a new shot into the shotlist
			aShots.push(shot);

			// code to put shot in the 3D scene will be implanted here
			// this is just temporary ugly code
			View3D.scope.scene.addChild(shot)

		}

		public function updateShots():void
		{
			var tempShot:IShot
			var tempUnit:AUnit
			var flagDelete:Boolean

			for (var i:int = aShots.length-1 ; i>-1 ; i--)
			{
				flagDelete = false	// reseting deleteflag.
				tempShot = aShots[i]

				// first make the shot move/rotate/animate/beam...
				tempShot.transformMesh()

				// is it hitting anyone or anything?
				tempUnit = tempShot.checkUnitCollision()
				if (tempUnit != null)
				{
					// returning 'TRUE' if that specific shot wants to remove itself on impact
					flagDelete = tempShot.onUnitCollision(tempUnit);
				}
				if (tempShot.checkStaticCollision()==true)
				{
					// returning 'TRUE' if that specific shot wants to remove itself on impact
					flagDelete = tempShot.onStaticCollision();
				}

				// checks if the bullet/shot has been on the scene for too long.
				// good for exploding shots or bullets that's been flying out of view.
				// has the life run out?
				if (tempShot.isOld())
				{
					// call the new IShot function
					tempShot.killByAge()
					flagDelete = true
				}

				if (flagDelete == true)
				{
					trace("removed!")
					// code will be implanted here to remove the visual shot from the 3D scene
					// this is temporary ugly code
					View3D.scope.scene.removeChild(tempShot)
					aShots.splice(i,1) // removing shot from the shot list.
				}
			}
		}
	}
}

Newest additions is that AShot now recieved a “lifeTime” that decreases for each frame. When it’s less than zero (0) the shot will be taken out of the scene and the ShotList. I will use this function both for deleting shots that has gone out of view for a while and for timeTriggered explosives (like the grenade I am about to show you). When time run out… BOOOM!
So, let’s look at our first real shot then! It’s a grenade :)

package shots
{
	import engine.UnitList;

	import managers.MeshConstants;
	import managers.Time;

	import org.papervision3d.objects.DisplayObject3D;

	import se.xcom.framework.managers.MeshManager;
	import se.xcom.math.Degrees;

	import units.AUnit;

	public class Grenade extends AShot
	{
		private const speed:Number = 90
		private const grav:Number = 8
		private var speedX:Number
		private var speedY:Number
		private var speedZ:Number
		private var rotX:Number
		private var rotY:Number

		public function Grenade(pitch:Number, yaw:Number, id:uint)
		{
			var tMesh:DisplayObject3D = initMesh()

			// number 1300 is the lifeTime of the grenade.
			super(tMesh,1300,40,id,false)

			setSpeed(pitch,yaw)	

		}

		private function setSpeed(pitch:Number, yaw:Number)
		{
			// here all calculations take place to see at which direction the
			// grenade should go and how high the angle should be.
			// also sets a random rotation to the grenade.

			// pitch = rotating along X-axis
			// yaw = rotation along Y-axis
			speedY = Degrees.dSin(pitch)*speed
			var tempSpeed:Number = Degrees.dCos(pitch)*speed
			speedX = Degrees.dCos(yaw)*tempSpeed
			speedZ = -Degrees.dSin(yaw)*tempSpeed
			rotX = Math.random()*40-20
			rotY = Math.random()*40-20 

		}

		override public function checkUnitCollision():AUnit
		{
			var aUnits:Array = UnitList.getInstance().units
			for each (var tUnit:AUnit in aUnits)
			{
				if (tUnit.mesh.distanceTo(this) < this.csRadius+tUnit.csRadius && tUnit.unitId != this.unitId)
				{
					trace("BOOOM!!")
					return UnitList.getInstance().units&#91;1&#93;
				}
				return null
			}

			return null;
		}

		override public function checkStaticCollision():void
		{
		}

		override public function onUnitCollision(unit:AUnit):Boolean
		{
			return true;
		}

		override public function onStaticCollision():Boolean
		{
			return false;
		}

		override public function transformMesh():void
		{
			/*
			here is our first real use of the DELTA-TIMING I wrote
			about in my previous article. This grenade will move in
			exactly the same speed regardless of the framerate of the
			computer.
			*/

			var delta:Number = Time.DELTA

			// move the Grenade
			this.x +=speedX*delta
			this.y +=speedY*delta
			this.z +=speedZ*delta

			// rotate it
			this.yaw(rotX*delta)
			this.pitch(rotY*delta)

			// check Y boundary
			// if grenade hits the floor. Bounce upwards
			if (this.y < 0)
			{
				this.y = 0
				speedY = -speedY
			}

			// set fake gravity
			speedY -= grav*delta

		}

		private function initMesh():DisplayObject3D
		{
			/* 	here I am putting the grenade into another DisplayObject3D
				and slightly adjusting it's x-position. Later when I rotate
				the wholeMesh, the grenade will "wobble" as it is not placed
				exactly in the center. Not a big thing but it's a nice little
				detail giving some character to the grenade.
			*/

			var wholeMesh:DisplayObject3D = new DisplayObject3D()
			var tMan:MeshManager = MeshManager.getInstance()
			var mBody:DisplayObject3D = tMan.getMesh(MeshConstants.NADE)
			mBody.scale = 70
			mBody.x = -40
			wholeMesh.addChild(mBody)

			return wholeMesh;
		}
	}
}
&#91;/sourcecode&#93;

I have now also put a mouseButton parameter into the controls so now I can actually drive the Defender, move the cannon up and down and fire a grenade that bounces on the ground that after a while EXPLODES!!  ... well no not yet. It just traces "boom" and gets removed from the screen and from the ShotList.
<h4>Several instances of the same object</h4>
During the implementation and testing of the grenade I stumbled over the problem of using several meshes using the same original model. Firing one grenade worked just fine but as soon as I pressed fire again I either got an errormessage because I tried to add the mesh on the scene when it was already added or I got the effect of the first grenade disappearing and spawning by the gun again. All this just because I was using the same DO3D for all grenades.

So how do we avoid this? Well, if we know for sure that we will not alter the geometrydata of the object in any way, we can use the same geometrydata to create several "instances" of the DO3D. Having a quick look, not at the documentation but checking the class as of DisplayObject3D you will find a function called <strong>COPY()</strong>. It actually copys the geometrydata into a new DO3D so we can instead of taking our originalmesh to the scene, we copy the geometry to a DO3D and use that instead. There are two problems to solve though...

<strong>Problem 1: </strong>Using parsed meshes just like *.3ds or *.dae creates a DO3D and puts all models in there that also got their own DO3D's. So the first actual DO3D that you control as your mesh is just a container for another DO3D inside. Now this has worked so far but now that we need to recieve the geometrydata fo the 3DS we need to get our hands of that DO3D-child. Somehow for some hightech reason beyond me, you cannot just take it. You will need to know it's name<strong> (!)</strong>

So even if you know that the child is in there there is only one function to get it and that is DO3D.getChildByName() ... damn. One quick solution to this is to trace out the DO3D.childrenList() string and then see the name and after that code: DO3D.getChildByName(nameTraced) this does somehow seem pretty unconvinient because we don't at this stage have a clue how many meshes we will need to do this for and it somehow is considered ugly code with this hardcoded names if you later on would like to change the model of your meshes.

<strong>Here's my solution:</strong>


			var mesh:DisplayObject3D = parsedMesh
			// getting first child name
			var nameOfChild:String = String(mesh.childrenList()).substring(0,String(mesh.childrenList()).indexOf(":"))
			// extracting child
			var newMesh:DisplayObject3D = mesh.getChildByName(nameOfChild)

Without knowing the name of the child you can now retrieve it. Remember that this only works if the containerObject only contains one single mesh.

If not, the above code will only grab the first object.
Problem 2: When importing different meshes using different parsers the models comes from very different 3D-environments. One typical thing is that there is a difference in what actually is up/down, left/right and in/out. Some uses the idea of letting Y becoming the in/out depth meanwhile Z s going up and down. This is often handled by the parser turing it so it looks “correct” in PV3D. That transformationdata could get lost when copying geometrydata.

Solution: I “ugly”-solved it this time, just rotating every single mesh of mine -90 in x-axis so you wont notice a difference in the end.

I’ve decided to create a new page in here with just the latest compiled version of the game. Make sure to come in here from time to time to check the progress of the game. Sometimes it might look ugly as I’m testing stuff but most of the time I hope you will see the progress of it.

Now there is only one core object I need to create before I an start putting all these pieces together to something looking like a game. We need special effects (!) explosions, lightbeams, dust on the ground, flares… things that makes all the difference that is :)





ViewportLayers – in depth

4 02 2009

This article has been moved to: http://www.x-com.se/labs

Make sure to visit this new blog for great articles and tutorials regarding PV3D and other flash related subjects.

/Andreas





Why can’t we just get along??

31 01 2009

Weekend is here and I’m going away. Still I spent an hour or two tonight and move code around, which, if you’re into game programming, you know that you will do often. Suddenly you just realize that the code should be somewhere else, in another class, called another thing. Hopefully it will lead to a somewhat cleaner and more logical code. Sometimes it doesn’t :(

There are a some things with PV3D that I really don’t understand why they did like they did. Take size as an example. This is the first time ever (EVER) I stumbled apon a 3dobject that cannot tell me how big it is. It moves around in a 3dspace uses exact coordinates to travel. Still there is no sign of the object knowing what height it has or how wide it is??? There is this bounding box parameter inside the geometry param so it wouldn’t be that hard to implement a recursive check for size both before and after transform. Am I missing something? Why dont I extend the DO3D then and put the functions in there myself? Well, as the whole PV3D is built on DO3D it will not use my extended class so it’s no use.

“So why don’t you just hack the original class then. Just put the features in there?” Well I actually did, and removed it , and did it again and finally removed it  because I know that PV3D is always under development. I am a news freak and always want the latest build and it would be a drag just to update the library manually, putting in my beloved code, each time I update PV3D. Also I wouldn’t be able to write this kind of blog as I cannot expect every single reader out there to hack their basic library classes just to get my examples in here to work.

No, I’m satisfied with just cursing.  :)

So? What’s the deal with layers anyway?? I promised to show you and as I’m leaving in a few minutes, this is an excellent time to bring up that subject.

To be able to draw 3D objects correctly on the screen, the renderer always draws the thing that is furthest away from the camera first. That is logical because if then something appears in the way that is closer to the camera it should overwrite the object far away. But there are times that you actually want to force the renderer to draw the objects in another order thatn it’s default behaviour. This is where layers come in.

In the case of this game, I will use viewportlayers for 2 reasons. 1: the annoying Z-fighting problem and 2: Visual effects.

The Z-problem is a little thingie that has been haunting the 3D world since the beginning of time. As I mentioned earlier the renderer automatically renders the furthest objects first. But how does it know that it is furthest away? Well EVERY single triangle in the 3D-world has got a centerpoint (yes right in the center of the 3 vertices drawing the triangle) and the triangle itself then can calculate how far from that centrepoint it is to the camera. It’s a simple question of distance (and math).

But what happenes if the triangle is a big ass giant triangle? Well it is still the center that decides when it will be drawn. Our arena ground in the game is a perfect example!! It is built up with big ass triangles!! So if our car happens to be on a place on the ground where the centrepoint is behind it. The scene wil be drawn as normal BUT if the car (Defender) moves away at places itself in the ground far away it as a very big chance that the centrepoint is now closer to the camera that the Defendertriangles are. Then the renderer will draw the Defender first and then the ground, causin the defender to suddenly disappear (be drawn uderneath the ground).. hmmm damn.

So now layers is our saviour. We kind of “put” all the objects in different layers and then the renderer will render ALL objects in one layer and then continue to the next layer that has a larger layerindex.  Problem solved!

Effects, we’ll take when we get there. You just think about all cool effects you can do with viewportlayers… mmmm…   in front, in back, in front. Amazing huh? no? Tutorial coming up!