java - libGdx with Box2D loader bicycle wheel joint -
i'm using libgdx framework box2d physics editor. can't figure out wrong. why can't joint correctly wheels chassis. wheels not holding chassis. sorry mistake. here pic. get:
<img src='http://s4.postimg.org/a0d6ujpjt/test.jpg' border='0' alt="test" />
code application:
package com.ernestyno.test; import com.ernestyno.text.bodyeditorloader.bodyeditorloader; import com.badlogic.gdx.gdx; import com.badlogic.gdx.inputadapter; import com.badlogic.gdx.graphics.texture; import com.badlogic.gdx.graphics.texture.texturefilter; import com.badlogic.gdx.graphics.g2d.sprite; import com.badlogic.gdx.graphics.g2d.spritebatch; import com.badlogic.gdx.math.mathutils; import com.badlogic.gdx.math.vector2; import com.badlogic.gdx.physics.box2d.body; import com.badlogic.gdx.physics.box2d.bodydef; import com.badlogic.gdx.physics.box2d.circleshape; import com.badlogic.gdx.physics.box2d.bodydef.bodytype; import com.badlogic.gdx.physics.box2d.fixturedef; import com.badlogic.gdx.physics.box2d.world; import com.badlogic.gdx.physics.box2d.joints.wheeljoint; import com.badlogic.gdx.physics.box2d.joints.wheeljointdef; public class dviratis extends inputadapter { private world world; private float x, y, width; private body dviracioremasbody, kairysratasbody, desinysratasbody; private wheeljoint kairysratasjoint, desinysratasjoint; private vector2 dviracioremasorigin, ratasorigin; private sprite remassprite, kairysratassprite, desinysratassprite; private texture remastexture, ratastexture; //fixtures private fixturedef dviracioremasfixture, dviracioratofixture; private float //remas fixture duomenys remas_density = 5f, remas_friction = .4f, remas_restitution = .3f, //ratu fixture duomenys ratas_density = remas_density * 1.5f, ratas_friction = 50f, ratas_restitution = .4f; private string body_editor_file = "json/dviratis.json", body_editor_remas_name = "dviracioremas", body_editor_ratas_name = "ratas"; public dviratis(world world, float x, float y, float width) { this.world = world; this.x = x; this.y = y; this.width = width; this.setfixture(); this.setsprite(); this.setbody(); } public void setfixture() { this.dviracioremasfixture = new fixturedef(); this.dviracioratofixture = new fixturedef(); this.dviracioremasfixture.density = this.remas_density; this.dviracioremasfixture.friction = this.remas_friction; this.dviracioremasfixture.restitution = this.remas_restitution; this.dviracioratofixture.density = this.ratas_density; this.dviracioratofixture.friction = this.ratas_friction; this.dviracioratofixture.restitution = this.ratas_restitution; } public void setsprite() { this.remastexture = new texture(gdx.files.internal("json/dviracio_remas.png")); this.remastexture.setfilter(texturefilter.linear, texturefilter.linear); this.remassprite = new sprite(this.remastexture); this.remassprite.setsize(this.width, this.width * this.remassprite.getheight() / this.remassprite.getwidth()); this.ratastexture = new texture(gdx.files.internal("json/dviracio_ratas.png")); this.ratastexture.setfilter(texturefilter.linear, texturefilter.linear); this.desinysratassprite = new sprite(this.ratastexture); this.desinysratassprite.setsize(this.width / 3, this.width / 3); this.kairysratassprite = new sprite(this.ratastexture); this.kairysratassprite.setsize(this.width / 3, this.width / 3); } public void setbody() { bodydef bodydef = new bodydef(); bodyeditorloader loader = new bodyeditorloader(gdx.files.internal(this.body_editor_file)); bodydef.type = bodytype.dynamicbody; bodydef.position.set(this.x, this.y); this.dviracioremasbody = this.world.createbody(bodydef); loader.attachfixture(this.dviracioremasbody, this.body_editor_remas_name, this.dviracioremasfixture, this.width); this.dviracioremasorigin = loader.getorigin(this.body_editor_remas_name, this.width).cpy(); this.desinysratasbody = this.world.createbody(bodydef); loader.attachfixture(this.desinysratasbody, this.body_editor_ratas_name, this.dviracioratofixture, this.width / 3); this.kairysratasbody = this.world.createbody(bodydef); loader.attachfixture(this.kairysratasbody, this.body_editor_ratas_name, this.dviracioratofixture, this.width / 3); this.ratasorigin = loader.getorigin(this.body_editor_ratas_name, this.width).cpy(); //masinos kairio rato susiejimas prie masinos kuno wheeljointdef axisdef = new wheeljointdef(); axisdef.bodya = this.dviracioremasbody; axisdef.bodyb = this.desinysratasbody; //nuo centro ratu susiejimo atittraukimas x ir y pozicija axisdef.localanchora.set(0.73f, -0.45f); axisdef.localanchorb.set(0f, 0f); axisdef.localaxisa.set(vector2.y); //vector2.y = new vector2(0, 1); - ratu amortizacija y asije, o ne x asije this.desinysratasjoint = (wheeljoint) world.createjoint(axisdef); axisdef.bodyb = this.kairysratasbody; axisdef.localanchora.set(-1f, -0.45f); axisdef.frequencyhz = this.dviracioremasfixture.density; //amortizatoriaus galia axisdef.maxmotortorque = this.dviracioremasfixture.density * 10; //variklio galia this.kairysratasjoint = (wheeljoint) world.createjoint(axisdef); } public void render() { vector2 pozicija = this.dviracioremasbody.getposition().sub(this.dviracioremasorigin); this.remassprite.setposition(pozicija.x, pozicija.y); this.remassprite.setorigin(this.dviracioremasorigin.x, this.dviracioremasorigin.y); this.remassprite.setrotation(this.dviracioremasbody.getangle() * mathutils.radianstodegrees); pozicija = this.desinysratasbody.getposition().sub(this.ratasorigin); this.desinysratassprite.setposition(pozicija.x, pozicija.y); this.desinysratassprite.setorigin(this.ratasorigin.x, this.ratasorigin.y); this.desinysratassprite.setrotation(this.desinysratasbody.getangle() * mathutils.radianstodegrees); pozicija = this.kairysratasbody.getposition().sub(this.ratasorigin); this.kairysratassprite.setposition(pozicija.x, pozicija.y); this.kairysratassprite.setorigin(this.ratasorigin.x, this.ratasorigin.y); this.kairysratassprite.setrotation(this.kairysratasbody.getangle() * mathutils.radianstodegrees); } public void draw(spritebatch batch) { this.remassprite.draw(batch); this.desinysratassprite.draw(batch); this.kairysratassprite.draw(batch); } public body getremas() { return dviracioremasbody; } public body getdviracioremasbody() { return dviracioremasbody; } }
huh... found wrong. program "physics-body-editor" generate's wrong circle center position in .json file. coordinates cx , cy. replaced bodyeditorloader.java
file. created attachfixture()
method allows describe circle coordinates.
instead of circles:
loader.attachfixture(this.desinysratasbody, this.body_editor_ratas_name, this.dviracioratofixture, this.width / 3);
use:
loader.attachfixture(this.desinysratasbody, this.body_editor_ratas_name, this.dviracioratofixture, this.width / 3, new vector2(0,0));
if want can download bodyeditorloader file
now wheels @ correct position. result image
Comments
Post a Comment