java - How can I create Kinematic Objects in jBullet? -
i trying make game engine , want camera controlled player , effected other jbullet entities in java. got suggested use kinematic objects looked them. couldn't find documentation understand.
can explain how set , use kinematic objects or @ least show me can start?
the documentation kinematiccharactercontroller, found here isn't entirely helpful, source in characterdemo can be. 2 main properties defined in demo.
public kinematiccharactercontroller character; public paircachingghostobject ghostobject;
the ghost can used dynamic collision detection, not automatically react events. character can moved changing transform.
//from source src\com\bulletphysics\demos\character\characterdemo.java transform starttransform = new transform(); starttransform.setidentity(); starttransform.origin.set(0.0f, 4.0f, 0.0f); vector3f worldmin = new vector3f(-1000f,-1000f,-1000f); vector3f worldmax = new vector3f(1000f,1000f,1000f); axissweep3 sweepbp = new axissweep3(worldmin, worldmax); ghostobject = new paircachingghostobject(); ghostobject.setworldtransform(starttransform); sweepbp.getoverlappingpaircache().setinternalghostpaircallback(new ghostpaircallback()); float characterheight = 1.75f * characterscale; float characterwidth = 1.75f * characterscale; convexshape capsule = new capsuleshape(characterwidth, characterheight); ghostobject.setcollisionshape(capsule); ghostobject.setcollisionflags(collisionflags.character_object); float stepheight = 0.35f * characterscale; character = new kinematiccharactercontroller(ghostobject, capsule, stepheight); dynamicsworld.addcollisionobject(ghostobject, collisionfiltergroups.character_filter, (short)(collisionfiltergroups.static_filter | collisionfiltergroups.default_filter)); dynamicsworld.addaction(character);
it wise extend motionstate class hold transform
public class mymotionstate extends motionstate { private transform worldtransform; public mymotionstate() { worldtransform = new transform(); worldtransform.setidentity(); } @override public transform getworldtransform(transform worldtrans) { worldtrans.set(worldtransform); return worldtrans; } @override public void setworldtransform(transform worldtrans) { worldtransform.set(worldtrans); } }
and linking kinematic rigidbody applying physics character, , getting info on rendering.
rigidbody.setcollisionflags(rigidbody.getcollisionflags() | collisionflags.kinematic_object); rigidbody.setactivationstate(collisionobject.disable_deactivation);
don't forget update physics engine once every iteration of game loop.
transform transform = new transform(); transform.setidentity(); transform.origin.set(input.getx(), input.gety(), input.getz()); mymotionstate.setworldtransform(transform); rigidbody.setcenterofmasstransform(mymotionstate.getworldtransform());
if prefer, put these in maincharacter class or whatever call (i object oriented feel , ease understand)
public class maincharacter implements keylistener, mouselistener { private dynamicsworld world; private mymotionstate mymotionstate; private rigidbody rigidbody; private kinematiccharactercontroller character; private convexshape shape; private texture texture; private ghostobject ghost; private vector3f pos; public maincharacter(dynamicsworld world, vector3f initialposition, convexshape shape, texture texture) { this.world = world; rigidbodyconstructioninfo constructinfo = new rigidbodyconstructioninfo(...); this.mymotionstate = mymotionstate; rigidbody = new rigidbody(constructinfo); ghost = new ghostobject(); character = new kinematiccharactercontroller(ghost,shape,1); } public void render() { glbegin(gl_quads); glvertex3f(... ... glend(); } public void mousemoved(mouseevent e) { //pseudocode this.yaw = e.getdx(); this.pitch = e.getdy(); } public void keypressed(keyevent e) { vector3f dpos = null; if(e.getkeychar() == 'w') { dpos.x = 10; } else if(e.getkeychar() == 's') { dpos.x = -10; } etc... move(dpos.x,dpos.y,dpos.z); } public void move(float dx, float dy, float dz) { pos.z += dx * (float) math.cos(math.toradians(yaw - 90)) + dz * math.cos(math.toradians(yaw)); pos.x -= dx * (float) math.sin(math.toradians(yaw - 90)) + dz * math.sin(math.toradians(yaw)); pos.y += dy * (float) math.sin(math.toradians(pitch - 90)) + dz * math.sin(math.toradians(pitch)); //pseudocode rigidbody.update(pos); world.update(pos); } }
i hope have helped you.
Comments
Post a Comment