android - Bouncing ball in libgdx using bullet3d physics -
android - Bouncing ball in libgdx using bullet3d physics -
i trying create programme of bouncing balls uisng libgdx in 3d.i finish beginner started getting hands on libgdx. able observe collision between bat , ball unable bounced back
here finish code public class main implements applicationlistener { final static short ground_flag = 1 << 8; final static short object_flag = 1 << 9; final static short all_flag = -1; class mycontactlistener extends contactlistener { @override public boolean oncontactadded(int uservalue0, int partid0, int index0, int uservalue1, int partid1, int index1) { //instances.get(uservalue0).moving = false; //instances.get(uservalue1).moving = false; homecoming true; } } static class mymotionstate extends btmotionstate { matrix4 transform; @override public void getworldtransform(matrix4 worldtrans) { worldtrans.set(transform); } @override public void setworldtransform(matrix4 worldtrans) { transform.set(worldtrans); } } static class gameobject extends modelinstance implements disposable { public final btrigidbody body; public boolean moving; public final mymotionstate motionstate; public gameobject(model model, string node, btrigidbody.btrigidbodyconstructioninfo constructioninfo) { super(model, node); motionstate = new mymotionstate(); motionstate.transform = transform; body = new btrigidbody(constructioninfo); body.setmotionstate(motionstate); } @override public void dispose() { // todo auto-generated method stub body.dispose(); motionstate.dispose(); } static class constructor implements disposable { public final model model; public final string node; public final btcollisionshape shape; public final btrigidbody.btrigidbodyconstructioninfo constructioninfo; public static vector3 inertia = new vector3(); public constructor(model model, string node, btcollisionshape shape, float mass) { this.model = model; this.node = node; this.shape = shape; if (mass > 0f) { shape.calculatelocalinertia(mass, inertia); } else { inertia.set(0, 0, 0); } this.constructioninfo = new btrigidbody.btrigidbodyconstructioninfo( mass, null, shape, inertia); } public gameobject construct() { homecoming new gameobject(model, node, constructioninfo); } @override public void dispose() { shape.dispose(); constructioninfo.dispose(); } } } perspectivecamera camera; environment environment; modelbatch modelbatch; model model; array<gameobject> instances; arraymap<string, gameobject.constructor> constructors; btcollisionconfiguration configuration; btdispatcher dispatcher; btbroadphaseinterface broadphaseinterface; btdynamicsworld dynamicworld; btconstraintsolver solver; mycontactlistener contactlistener; float spawtimer; @override public void create() { // todo auto-generated method stub bullet.init(); modelbatch = new modelbatch(); environment = new environment(); environment.set(new colorattribute(colorattribute.ambientlight, 0.4f, 0.4f, 0.4f, 1f)); environment.add(new directionallight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f)); photographic camera = new perspectivecamera(67, gdx.graphics.getwidth(), gdx.graphics.getheight()); camera.position.set(3f, 7f, 10f); camera.lookat(0, 4f, 0); camera.near = 1f; camera.far = 300f; camera.update(); modelbuilder builder = new modelbuilder(); builder.begin(); builder.node().id = "ground"; builder.part("ground", gl20.gl_triangles, usage.position | usage.normal, new material(colorattribute.creatediffuse(color.red))).box(5f, 1f, 5f); builder.node().id = "sphere"; builder.part("sphere", gl20.gl_triangles, usage.position | usage.normal, new material(colorattribute.creatediffuse(color.green))) .sphere(1f, 1f, 1f, 10, 10); model = builder.end(); constructors = new arraymap<string, main.gameobject.constructor>( string.class, gameobject.constructor.class); constructors.put("ground", new gameobject.constructor(model, "ground", new btboxshape(new vector3(2.5f, .5f, 2.5f)), 0f)); constructors.put("ball", new gameobject.constructor(model, "sphere", new btsphereshape(0.5f), 3f)); configuration = new btdefaultcollisionconfiguration(); dispatcher = new btcollisiondispatcher(configuration); solver = new btsequentialimpulseconstraintsolver(); broadphaseinterface = new btdbvtbroadphase(); dynamicworld = new btdiscretedynamicsworld(dispatcher, broadphaseinterface, solver, configuration); dynamicworld.setgravity(new vector3(0, -10, 0)); contactlistener = new mycontactlistener(); instances = new array<main.gameobject>(); gameobject object = constructors.get("ground").construct(); object.body.setcollisionflags(object.body.getcollisionflags() | btcollisionobject.collisionflags.cf_kinematic_object); instances.add(object); dynamicworld.addrigidbody(object.body); object.body.setactivationstate(collision.disable_deactivation); createball(); } public void createball() { gameobject obj = constructors.get("ball").construct(); //obj.moving = true; obj.transform.setfromeulerangles(mathutils.random(360f), mathutils.random(360f), mathutils.random(360f)); obj.transform.trn(0f, 9f, 0f); //obj.body.proceedtotransform(obj.transform); obj.body.setrestitution(1.0f); obj.body.setfriction(1.0f); obj.body.setworldtransform(obj.transform); obj.body.setuservalue(instances.size); obj.body.setcollisionflags(obj.body.getcollisionflags() | btcollisionobject.collisionflags.cf_custom_material_callback); instances.add(obj); // dynamicworld.addcollisionobject(obj.body, object_flag, ground_flag); dynamicworld.addrigidbody(obj.body); } @override public void resize(int width, int height) { } float angle, speed = 10f; @override public void render() { // todo auto-generated method stub final float delta = gdx.graphics.getdeltatime(); angle = (angle + delta+speed) % 360; instances.get(0).transform.settranslation(0f,mathutils.sindeg(angle),0f); //instances.get(0).body.setworldtransform(instances.get(0).transform); dynamicworld.stepsimulation(delta, 5, 1 / 60f); gdx.gl.glclearcolor(0.3f, 0.3f, 0.3f, 1.f); gdx.gl.glclear(gl20.gl_color_buffer_bit | gl20.gl_depth_buffer_bit); modelbatch.begin(camera); modelbatch.render(instances, environment); modelbatch.end(); } @override public void pause() { } @override public void resume() { } @override public void dispose() { // todo auto-generated method stub (gameobject obj : instances) obj.dispose(); instances.clear(); (gameobject.constructor ctor : constructors.values()) ctor.dispose(); constructors.clear(); dynamicworld.dispose(); solver.dispose(); broadphaseinterface.dispose(); dispatcher.dispose(); configuration.dispose(); contactlistener.dispose(); modelbatch.dispose(); model.dispose(); } } } here updated code right able bounce ground not objective objective bounce ball when hits code applied moving of ground unable appied ball. please help.
here code.finally ball bounce
public class main implements applicationlistener { final static short ground_flag = 1 << 8; final static short object_flag = 1 << 9; final static short all_flag = -1; public float delta; class mycontactlistener extends contactlistener { @override public boolean oncontactadded(int uservalue0, int partid0, int index0, int uservalue1, int partid1, int index1) { // gdx.app.log("oncontact", "oncontact added"); instances.get(1).body.setlinearvelocity(new vector3(0, 10f, 0f)); homecoming true; } } static class mymotionstate extends btmotionstate { matrix4 transform; @override public void getworldtransform(matrix4 worldtrans) { worldtrans.set(transform); } @override public void setworldtransform(matrix4 worldtrans) { transform.set(worldtrans); } } static class gameobject extends modelinstance implements disposable { public final btrigidbody body; public boolean moving; public final mymotionstate motionstate; public gameobject(model model, string node, btrigidbody.btrigidbodyconstructioninfo constructioninfo) { super(model, node); motionstate = new mymotionstate(); motionstate.transform = transform; body = new btrigidbody(constructioninfo); body.setmotionstate(motionstate); } @override public void dispose() { // todo auto-generated method stub body.dispose(); motionstate.dispose(); } static class constructor implements disposable { public final model model; public final string node; public final btcollisionshape shape; public final btrigidbody.btrigidbodyconstructioninfo constructioninfo; public static vector3 inertia = new vector3(); public constructor(model model, string node, btcollisionshape shape, float mass) { this.model = model; this.node = node; this.shape = shape; if (mass > 0f) { shape.calculatelocalinertia(mass, inertia); } else { inertia.set(0, 0, 0); } this.constructioninfo = new btrigidbody.btrigidbodyconstructioninfo( mass, null, shape, inertia); } public gameobject construct() { homecoming new gameobject(model, node, constructioninfo); } @override public void dispose() { shape.dispose(); constructioninfo.dispose(); } } } perspectivecamera camera; environment environment; modelbatch modelbatch; model model; array<gameobject> instances; arraymap<string, gameobject.constructor> constructors; btcollisionconfiguration configuration; btdispatcher dispatcher; btbroadphaseinterface broadphaseinterface; btdynamicsworld dynamicworld; btconstraintsolver solver; mycontactlistener contactlistener; float spawtimer; gameobject obj; @override public void create() { // todo auto-generated method stub bullet.init(); modelbatch = new modelbatch(); environment = new environment(); environment.set(new colorattribute(colorattribute.ambientlight, 0.4f, 0.4f, 0.4f, 1f)); environment.add(new directionallight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f)); photographic camera = new perspectivecamera(67, gdx.graphics.getwidth(), gdx.graphics.getheight()); camera.position.set(3f, 7f, 10f); camera.lookat(0, 4f, 0); camera.near = 1f; camera.far = 300f; camera.update(); modelbuilder builder = new modelbuilder(); builder.begin(); builder.node().id = "ground"; builder.part("ground", gl20.gl_triangles, usage.position | usage.normal, new material(colorattribute.creatediffuse(color.red))).box(5f, 1f, 5f); builder.node().id = "sphere"; builder.part("sphere", gl20.gl_triangles, usage.position | usage.normal, new material(colorattribute.creatediffuse(color.green))) .sphere(1f, 1f, 1f, 10, 10); model = builder.end(); constructors = new arraymap<string, main.gameobject.constructor>( string.class, gameobject.constructor.class); constructors.put("ground", new gameobject.constructor(model, "ground", new btboxshape(new vector3(2.5f, .5f, 2.5f)), 0f)); constructors.put("ball", new gameobject.constructor(model, "sphere", new btsphereshape(0.5f), 0.5f)); configuration = new btdefaultcollisionconfiguration(); dispatcher = new btcollisiondispatcher(configuration); solver = new btsequentialimpulseconstraintsolver(); broadphaseinterface = new btdbvtbroadphase(); dynamicworld = new btdiscretedynamicsworld(dispatcher, broadphaseinterface, solver, configuration); dynamicworld.setgravity(new vector3(0, -8f, 0)); contactlistener = new mycontactlistener(); instances = new array<main.gameobject>(); gameobject object = constructors.get("ground").construct(); object.body.setcollisionflags(object.body.getcollisionflags() | btcollisionobject.collisionflags.cf_kinematic_object); instances.add(object); dynamicworld.addrigidbody(object.body); object.body.setactivationstate(collision.disable_deactivation); createball(); } public void createball() { obj = constructors.get("ball").construct(); obj.transform.trn(0f, 11f, 0f); obj.body.setworldtransform(obj.transform); obj.body.setuservalue(instances.size); obj.body.setcollisionflags(obj.body.getcollisionflags() | btcollisionobject.collisionflags.cf_custom_material_callback); obj.body.setfriction(1.0f); obj.body.setrestitution(3.0f); instances.add(obj); dynamicworld.addrigidbody(obj.body); } @override public void resize(int width, int height) { } float angle, speed = 10f, ytranslate = 0f, zangle = 0f; @override public void render() { // todo auto-generated method stub delta = gdx.graphics.getdeltatime(); angle = (angle + delta + speed) % 360; if (gdx.input.istouched()) { if (ytranslate > -60f) { zangle -= 0.2f; } else { // ytranslate = 30f; } instances.get(0).transform.settorotation(0f, 0f, 2f, zangle); } else { instances.get(0).transform.settorotation(0f, 0f, 1f, 30f); instances.get(0).transform.settranslation(0f, 0f, 0f); } //instances.get(0).transform.settranslation(0f,0f,0f); dynamicworld.stepsimulation(delta, 5, 1 / 60f); gdx.gl.glclearcolor(0.3f, 0.3f, 0.3f, 1.f); gdx.gl.glclear(gl20.gl_color_buffer_bit | gl20.gl_depth_buffer_bit); modelbatch.begin(camera); modelbatch.render(instances, environment); modelbatch.end(); } @override public void pause() { } @override public void resume() { } @override public void dispose() { // todo auto-generated method stub (gameobject obj : instances) obj.dispose(); instances.clear(); (gameobject.constructor ctor : constructors.values()) ctor.dispose(); constructors.clear(); dynamicworld.dispose(); solver.dispose(); broadphaseinterface.dispose(); dispatcher.dispose(); configuration.dispose(); contactlistener.dispose(); modelbatch.dispose(); model.dispose(); } } android libgdx game-physics bulletphysics
Comments
Post a Comment