Skip to content

Introduction to Actors

TraxNet edited this page Nov 27, 2012 · 7 revisions

Around the Actor class pivots many important concepts required to understand ShadinZen. This little guide explains what Actors are, how to create your own Actors and how to easily animated them.

The Actor class

Actor is an abstract that inherits from Entity. By taking a quick look to its implementation we can notice it has some threedimensional properties like position, rotation, scale and so on. It has a list of child actors and some rendering properties like a Shape, a ShaderProgram and a Texture. This are basic properties mostly all actors will need.

When you design your next hit game, you will probably need to represent the player, the enemies or some automated elevator, all of these can be represented using actors. For example one actor can be the player, a child actor its missile launcher and spawn one missle each time the user fires. This approach makes natural adding to each actor the logic that drives its behaviour. The missile would only have trajectory and collision detection logic whetever the player actor will translate user input into animation and actor movement.

There are some methods that must be implemented by derived classes, onUpdate and onDraw are the critical ones. onUpdate is called on each engine tick which may or may not have the same drawing frecuency. We must code here all the logic that drives the actor's hebaviour (although Actions can be used for this to some extent). Inside the onDraw method we must send down to the rendering service all RenderTask we need to draw this actor.

Creating Actors

Probably the first actors in your project will be attached to an Scene, but you may be tempted to just create a new instance using the java operator new. Unfortunelly if you do so, the engine will not know a new Actor has been created. We need to use the spawn method inherited from Entity class:

MyMissileClass missile = my_scene.spawn(MyMissileClass.class, "my id");

The spawn method take two parameters, a class (which must extend from Actor) and actor ID

Simple Actor movement and rotations

Actor position is stored in this._position within the actor's code or getPosition/setPosition accessor methods. Position is relative to parent Actor (if any) as the engine will multiply parent's transform matrix with current actor matrix before drawing it.

Rotation are stored as quaternions (this manual by MIT is probably the best resource for understanding quaternions). Again, rotation are relative to parent's rotation, in fact parent's rotation and translation are applied before current actor's affine transform matrix. During drawing step, the engine processes the hierarchy tree from top down, concatenating the parent transforms before calling Actor's onDraw method. Let's see all this with an example:

Using Actions

Actor's life cycle

Clone this wiki locally