Android Game Development 2 D physics Base project
Android Game Development 2 D physics
Base project Download our base project and open it in Net. Beans cg. iit. bme. hu/gamedev/KIC/11_Android. Development/11 _03_Android_Levels. Game. Objects_Final. zip Change the android sdk location path Per-user properties (local. properties) sdk. dir=change path here Start an emulator Build Run
Physics Box 2 D 2 D physics engine Java JBox 2 D Android version JBox 2 d 4 Android JBox 2 D 4 Android_2. 1. 2. jar should be copied to the. /libs folder in our project folder Create new package: Engine. Physics
Physics. Object Create Physics. Object class in Physisc package public abstract class Physics. Object{ protected Object user. Data = 0; public Object get. User. Data(){return user. Data; } public void set. User. Data(Object data){user. Data = data; } public abstract void destroy(); public abstract Vector 3 get. Position(); public abstract Vector 3 get. Velocity(); public abstract void limit. Velocity(Vector 3 limit); public abstract void set. Velocity(float x, float y, float z); public abstract void set. Linear. Damping(float value); public abstract Quaternion get. Orientation(); public void update(){} public abstract void add. Force(float x, float y, float z); public abstract void move. To. Pos(float x, float y, float z); public abstract void rotate. To. Angles(float x, float y, float z); }
Physics. Engine 2 D I. Create Physics. Engine 2 D class public class Physics. Engine 2 D { private static final Physics. Engine 2 D instance = new Physics. Engine 2 D(); private World world = new World(new Vec 2(0. 0 F, -10. 0 F), false); public static Physics. Engine 2 D get. Singleton(){ return instance; } private Physics. Engine 2 D(){} public Body create. Body(Body. Def def){ return this. world. create. Body(def); } public void destroy. Body(Body body){ world. destroy. Body(body); }
Physics. Engine 2 D II. public void clear(){ Body next. B = world. get. Body. List(); while(next. B != null) { Body b = next. B; next. B = b. get. Next(); world. destroy. Body(b); } } public void update(float dt){ this. world. step(dt, 6, 2); this. world. clear. Forces(); } public void set. Gravity(float x, float y) { this. world. set. Gravity(new Vec 2(x, y)); } }
Physics 2 DObject I. Create Ohysics 2 DObject class public class Physics 2 DObject extends Physics. Object{ Body body = null; Vector 3 position = new Vector 3(); Vector 3 velocity. Limit = new Vector 3(-1, -1); Quaternion orientation = new Quaternion(); String group. Name = null; public Physics 2 DObject(Body b) { this. body = b; this. position. set(b. get. Position(). x, b. get. Position(). y, 0. 0 F); this. orientation. set(0. 0 F, b. get. Angle()); if(body. m_type != Body. Type. DYNAMIC){ Fixture f = body. get. Fixture. List(); while(f!=null){ f. m_restitution = 0; f = f. get. Next(); } } this. body. m_user. Data = this; }
Physics 2 DObject II. public void destroy(){ Physics. Engine 2 D. get. Singleton(). destroy. Body(body); } public Body get. Body(){return body; } public Vector 3 get. Position(){return this. position; } public Vector 3 get. Velocity(){return new Vector 3(this. velocity); } public void set. Velocity(float x, float y, float z){ velocity. set(x, y, z); body. set. Linear. Velocity(new Vec 2(x, y)); } public void limit. Velocity(Vector 3 limit){ velocity. Limit. set(limit. x(), limit. y(), limit. z()); } public void set. Linear. Damping(float value){body. set. Linear. Damping(value); } public Quaternion get. Orientation(){return this. orientation; }
Physics 2 DObject III. public void update(){ this. velocity. set(body. get. Linear. Velocity(). x, body. get. Linear. Velocity(). y, 0); if(velocity. Limit. x() > 0){ if(velocity. x() > velocity. Limit. x()) body. set. Linear. Velocity(new Vec 2(velocity. Limit. x(), velocity. y())); if(velocity. x() < -velocity. Limit. x()) body. set. Linear. Velocity(new Vec 2(-velocity. Limit. x(), velocity. y())); } if(velocity. Limit. y() > 0){ if(velocity. y() > velocity. Limit. y()) body. set. Linear. Velocity(new Vec 2(velocity. x(), velocity. Limit. y())); if(velocity. y() < -velocity. Limit. y()) body. set. Linear. Velocity(new Vec 2(velocity. x(), -velocity. Limit. y())); } this. position. set(this. body. get. Position(). x, this. body. get. Position(). y, 0. 0 F); this. orientation. set(0. 0 F, this. body. get. Angle()); this. velocity. set(body. get. Linear. Velocity(). x, body. get. Linear. Velocity(). y, 0); }
Physics 2 DObject IV. public void add. Force(float x, float y, float z) { body. apply. Linear. Impulse(new Vec 2(x, y), new Vec 2(0. 0 f, 0. 0 f)); } public void move. To. Pos(float x, float y, float z) { float angle = this. body. get. Angle(); this. body. set. Transform(new Vec 2(x, y), angle); } public void rotate. To. Angles(float x, float y, float z) { Vec 2 pos = this. body. get. Position(); this. body. set. Transform(pos, z); } }//Physics 2 DObject end
Main. Engine. update add new line: level. pre. Update(t, dt); Physics. Engine 2 D. get. Singleton(). update(dt); level. update(t, dt);
Generic. Game. Object I. Modify Generic. Game. Object to contain rigid body representation New member: protected Physics. Object physics. O = null; Constructor public Generic. Game. Object(String name, Mesh. Entity entity, Physics. Object po) { super(name); if (entity != null) { this. render. O = entity; this. render. Node = Scene. Manager. get. Singleton(). get. Root. Node(). create. Child(); this. render. Node. attach. Object(this. render. O); } } if (po != null) { this. physics. O = po; this. physics. O. set. User. Data(this); }
Generic. Game. Object II. public void destroy(){ if(render. O != null){ render. Node. detach. Object(render. O); render. Node. get. Parent(). remove. Child(render. Node); } if(physics. O != null) { physics. O. destroy(); } }
Generic. Game. Object III. public void update(float t, float dt) { if (this. physics. O != null) { this. physics. O. update(); if (this. render. Node != null) { this. render. Node. set. Position(this. physics. O. get. Position()); this. render. Node. set. Orientation(this. physics. O. get. Orientation()); this. render. O. update(t, dt); } } } public Vector 3 get. Position() { return this. physics. O. get. Position(); } public Vector 3 get. Velocity() { return this. physics. O. get. Velocity(); }
Generic. Game. Object IV. public void set. Position(float x, float y, float z) { if (this. physics. O != null) this. physics. O. move. To. Pos(x, y, z); if (this. render. Node != null) this. render. Node. set. Position(x, y, z); } public void set. Orientation(float yaw, float pitch, float roll) { if (this. physics. O != null) this. physics. O. rotate. To. Angles(yaw, pitch, roll); if (this. render. Node != null) this. render. Node. set. Orientation(yaw, pitch, roll); }
Level. Generator I. create. Plane() Generic. Game. Object go = new Generic. Game. Object(name, null); New method protected Physics 2 DObject create. Static. Phy. Box(float w, float h) { Polygon. Shape shape = new Polygon. Shape(); shape. set. As. Box(w, h); Body. Def bd = new Body. Def(); bd. type = Body. Type. STATIC; Fixture. Def fd = new Fixture. Def(); fd. shape = shape; fd. restitution = 0; Body body = Physics. Engine 2 D. get. Singleton(). create. Body(bd); body. create. Fixture(fd); } return new Physics 2 DObject(body);
Level. Generator II. protected void create. Ground. Element(float pos. X) throws Exception{ Physics 2 DObject ground. Phy = create. Static. Phy. Box(1. 0 f, 1); Generic. Game. Object ground = new Generic. Game. Object("ground_" + ground. Count, ground. ME, ground. Phy); … } protected void create. Pit. Element(float pos. X) throws Exception{ Physics 2 DObject pit. Phy = create. Static. Phy. Box(1. 0 f, 0. 2 f); Generic. Game. Object pit = new Generic. Game. Object("pit_" + pit. Count, null, pit. Phy); … } protected void create. Floor 1 Element(float pos. X) throws Exception{ Physics 2 DObject floor. Phy = create. Static. Phy. Box(1. 0 f, 0. 1 f); Generic. Game. Object floor 1 = new Generic. Game. Object("floor 1_" + floor 1 Count, floor 1 ME, floor. Phy); … } protected void create. Floor 2 Element(float pos. X) throws Exception{ Physics 2 DObject floor. Phy = create. Static. Phy. Box(1. 0 f, 0. 1 f); Generic. Game. Object floor 2 = new Generic. Game. Object("floor 2_" + floor 2 Count, floor 2 ME, floor. Phy); … }
Level. Generator III. protected int box. Count = 0; protected void create. Box(float x, float y) { Polygon. Shape shape = new Polygon. Shape(); shape. set. As. Box(0. 3 f, 0. 3 f); Body. Def bd = new Body. Def(); bd. type = Body. Type. DYNAMIC; Fixture. Def fd = new Fixture. Def(); fd. shape = shape; fd. restitution = 0. 13 f; fd. density = 100. 0 f; fd. friction = 0. 5 f; Body body = Physics. Engine 2 D. get. Singleton(). create. Body(bd); body. create. Fixture(fd); try{ Mesh. Entity item. ME = new Mesh. Entity("box_" + box. Count, "Unit. Quad"); item. ME. set. Mesh. Scale(0. 3 f, 1); item. ME. set. Material("g_Box"); Physics 2 DObject item. Phy = new Physics 2 DObject(body); Generic. Game. Object item = new Generic. Game. Object("box_" + box. Count, item. ME, item. Phy); item. set. Type(Game. Object. Type. Useful. Things); item. set. Position(x, y + 0. 5 f, 0); level. add. Game. Object(item); } catch(Exception e) { android. util. Log. e("Level. Generator", "Unable to create item object: " + e. to. String()); } box. Count++; }
Level. Generator IV. public Level generate(){ … Generic. Game. Object ground. L = new Generic. Game. Object("ground_l_" + i, ground. ME, null); … Generic. Game. Object ground. R = new Generic. Game. Object("ground_r_" + i, ground. ME, null); … create. Box(-level. Size + 3, 10); create. Box(-level. Size + 3. 2 f, 13); create. Box(-level. Size + 2. 8 f, 16); for(int i = 0; i < level. Size; ++i){ …. }
Build and run
Make the simulation more stable Physics 2 DEngine: public void update(float dt){ } float max. Dt = 0. 09 f; while(dt > max. Dt){ this. world. step(max. Dt, 5, 2); dt -= max. Dt; } this. world. step(dt, 5, 2); this. world. clear. Forces(); Play with these parameters!!
Build and run
Border: invisible physics boxes Level. Generator. generate public Level generate() { … //generate background … Physics 2 DObject b. Phy = create. Static. Phy. Box(1. 0 f, 10); Generic. Game. Object b 1 = new Generic. Game. Object("border 1", null, b. Phy); b 1. set. Position(-level. Size - 1, 0, 0); level. add. Game. Object(b 1); Physics 2 DObject b. Phy 2 = create. Static. Phy. Box(1. 0 f, 10); Generic. Game. Object b 2 = new Generic. Game. Object("border 2", null, b. Phy 2); b 2. set. Position(level. Size + 1, 0, 0); level. add. Game. Object(b 2); for(int i = 0; i < 3; i++)
The End
- Slides: 24