Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Game Engine – Advanced topics. Base project Download our base project and open it in NetBeans

Similar presentations

Presentation on theme: "Android Game Engine – Advanced topics. Base project Download our base project and open it in NetBeans"— Presentation transcript:

1 Android Game Engine – Advanced topics

2 Base project Download our base project and open it in NetBeans Change the android sdk location path Per-user properties ( sdk.dir=change path here Start an emulator Build Run

3 Animation Change of a value in time What? Position Color orientation … How to store? Can be multi dimensional Array of floats How to define the change of the value Keyframe animation Interpolation (step/linear)

4 AnimationTrackData - AnimationTrack AnimationTrackData Represents a keyframe animated value Stores a float array for each keyframe Keys are evenly placed in time The arrays should have the same size (the dimension of the value) AnimationTrack Represents a keyframe animation Works on an AnimationTrackData Handles animation time Updates current value (interpolation) Playback speed can be given Animation can loop

5 AnimationTrack example float keys[][] = {{-1,0},{1,0},{-1,0}}; AnimationTrackData atd = new AnimationTrackData(2); atd.addKeyframe(keys[0]); atd.addKeyframe(keys[1]); atd.addKeyframe(keys[2]); AnimationTrack animTrack = new AnimationTrack(atd); animTrack.setUseLinearInterpolation(true); animTrack. setLoop(true); //dt between two frames should be 0.5 sec. animTrack. setPlaybackSpeed(2.0); … animTrack.update(t,dt); float currentX = (animTrack. getCurrentValue())[0]; float currentY = (animTrack. getCurrentValue())[1]; mySceneNode.setPosition(currentX, currentY, 0);

6 AnimatedTexture A texture that contains frames of animations Can contain several animations (assigned with a unique name) Texture coordinates will be transformed with the texture matrix to use only a subregion of the texture Subregion is defined with a bounding box (min/max corners) The bounding box is calculated in each rendered frame using an AnimationTrack AnimationTrackData contains 4 floats: minX, minY, maxX, maxY

7 AnimatedTexture example AnimatedTexture at = new AnimatedTexture("myAnimTexture"); AnimationTrackData atd = new AnimationTrackData(4); float keys0[] = {0.0f, 0.0f, 0.2f, 0.2f}; float keys1[] = {0.2f, 0.0f, 0.4f, 0.2f}; float keys2[] = {0.4f, 0.0f, 0.6f, 0.2f}; float keys3[] = {0.0f, 0.0f, 0.2f, 0.2f}; atd.addKeyframe(keys0); atd.addKeyframe(keys1); atd.addKeyframe(keys2); atd.addKeyframe(keys3); at.addAnimation("walk", atd, loopAnimation, animationPlaybackSpeed ); at.setFileName("animtex.png"); at.load();... at.lookLeft(true); //mirror subimage in the x direction at.playAnimation("walk"); at.update(t, dt);

8 MeshEntity public void update(float t, float dt) { Texture texture = this.material.getTexture(); if(texture != null && texture instanceof AnimatedTexture) ((AnimatedTexture) texture).update(t, dt); }

9 AnimatedTexture example

10 SpriteMap example Also a texture that contains subimages defined with bounding boxes Each subimage has its unique name (literal) The texture coordinates are transformed with the texture matrix Only one literal can be active at a time Can draw the subimage to screen at a given position and with a given height (width is adjusted to bounding box ratio) Can be used to store several small images in one texture (these images are related) E.g.: GUI strings Images of numbers or letters This class can be extended to implement a bitmap font drawing (StringDrawer)

11 SpriteMap example

12 SpriteMap sm = new SpriteMap("MySMap"); sm.setFileName(“HUD.png"); sm.addLiteral("score", scoreMinX, scoreMinY, scoreMaxX, scoreMaxY); sm.addLiteral("life", lifeMinX, lifeMinY, lifeMaxX, lifeMaxY); sm.load();... sm.draw("score", x, y, height, alignLeft, alignBottom);

13 StringDrawer package; import; public class StringDrawer { public static void drawString(SpriteMap map, String s, float x, float y, float h){ char[] chars = s.toCharArray(); float t = x; for(int i = 0; i < chars.length; i++) { t += map.draw(""+chars[i], t, y, h, true, true); }

14 Script loaders I. Store material and texture definitions in scripts Easier to edit Not hardwired in code Human readable (no programming skills needed) We will use XML files to store properties Place new classes in new package: Engine.Scripts

15 Script loaders II. ScriptLoader class Base class XML pull parser, state machine handling TextureScriptLoader loads a Texture, AnimatedTexture or SpriteMap MaterialScriptLoader Loads a Material Usually refers to a texture name KeyframeAnimationDataScriptLoader Loads an AnimationTrackData The data can be passed to an AnimationTrack or AnimatedTexture MeshEntityScriptLoader Loads a MeshEntity Refers to a material name and a mesh name Physics2DObjectScriptLoader Loads a Physics2DObject Box and circle shapes can be defined Friction, density, restitution and type (static/dynamic/kinematic) can be given

16 Script loaders III. Game related script loaders are in PlatformGame package GameObjectScriptLoader Loads a game object MeshEntity and Physics2DObject can be defined Initial position and orientation can be defined LevelScriptLoader Loads a level Level scripts list gameobjects

17 Resource managers with script loading public class TextureManager extends ResourceManager { private TextureScriptLoader loader = new TextureScriptLoader(); protected Texture loadImpl(String name){ this.loader.loadScript(name + ".texture.xml"); return this.loader.getTexture(); } public class MaterialManager extends ResourceManager { private MaterialScriptLoader loader = new MaterialScriptLoader(); protected Material loadImpl(String name){ this.loader.loadScript(name + ".material.xml"); return this.loader.getMaterial(); }

18 Texture script examples

19 Material script examples

20 MeshEntity script example

21 Physics2DObject script examples

22 GameObject script example

23 AnimatedTexture script example

24 Excersise Change material and texture loading in LevelGenerator to use the automatic load from script functionality of material and texture managers !! (We have to write the scripts too)

25 Physics contact events I. Create new class in Physics package: public interface PhysicsContactEventListener { public abstract void onContact(PhysicsObject other); public abstract PhysicsObject getObject(); }

26 Physics contact events II. PhysicsEngine2D: public class PhysicsEngine2D { class ContactEvent{ public PhysicsContactEventListener listener; public PhysicsObject otherObject; public ContactEvent(PhysicsContactEventListener l, PhysicsObject o){ listener = l; otherObject = o; } }; private LinkedList contactEvents = new LinkedList (); …

27 Physics contact events III. class MyContactListener implements ContactListener{ public void beginContact(Contact contact) { PhysicsObject o1 = (PhysicsObject) contact.m_fixtureA.m_body.m_userData; PhysicsObject o2 = (PhysicsObject) contact.m_fixtureB.m_body.m_userData; for(PhysicsContactEventListener l : contactEventListeners) { if(o1 == l.getObject()) contactEvents.add(new ContactEvent(l, o2)); if(o2 == l.getObject()) contactEvents.add(new ContactEvent(l, o1)); } public void endContact(Contact contact) { } public void preSolve(Contact contact, Manifold manifold) { } public void postSolve(Contact contact, ContactImpulse impulse) { } }; private LinkedList contactEventListeners = new LinkedList ();

28 Physics contact events IV. private PhysicsEngine2D(){ world.setContactListener(new MyContactListener()); } public Contact getContacts(){ return world.getContactList(); } public void update(float dt) { … for(ContactEvent e : contactEvents){ e.listener.onContact(e.otherObject); } contactEvents.clear(); } public void registerContactListener(PhysicsContactEventListener l){ contactEventListeners.add(l); }


Download ppt "Android Game Engine – Advanced topics. Base project Download our base project and open it in NetBeans"

Similar presentations

Ads by Google