by
386 12

SNIPPET - Box2d_Java

Codice Box2D per creare una simulazione fisica con oggetti fisici
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/198228/SNIPPET---Box2d_Java?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
/* <SNIPPET> <TAGS="JAVA,LIBGDX,BOX2D" /> <NAME="Box2d_Java" /> <DESCR>Codice Box2D per creare una simulazione fisica con oggetti fisici</DESCR> </SNIPPET> */ // Codice Generico Box2D (only box2d debug mode is visible) // 100 pixels <-> 1 meter static final float WORLD_TO_BOX=0.01f; static final float BOX_WORLD_TO=100f; float ConvertToBox(float x){ return x*WORLD_TO_BOX; } // Step 1 - Create World World world=new World(new Vector2(0,-20),true) // World(Vector2 gravity, boolean doSleep) // com.badlogic.gdx.math // Step 2 - Create Body public void CreateBody(World world,Vector2 pos,float angle){ BodyDef bodyDef = new BodyDef(); bodyDef.type = bodyType; // The body type: static, kinematic, or dynamic bodyDef.position.set(ConvertToBox(pos.x),ConvertToBox(pos.y)); // The world position of the body. bodyDef.angle=angle; // The world angle of the body in radians. body = world.createBody(bodyDef); } // Step 3 - Create Fixture/Shape private void MakeRectFixture(float width,float height,BodyDef.BodyType bodyType, float density,float restitution, Vector2 pos,float angle){ PolygonShape bodyShape = new PolygonShape(); float w=ConvertToBox(width/2f); float h=ConvertToBox(height/2f); bodyShape.setAsBox(w,h); FixtureDef fixtureDef=new FixtureDef(); fixtureDef.density=density; // The density, usually in kg/m^2. fixtureDef.restitution=restitution; // The restitution (elasticity) usually in the range [0,1]. fixtureDef.shape=bodyShape; /* com.badlogic.gdx.physics.box2d.Shape com.badlogic.gdx.physics.box2d.PolygonShape {ChainShape, CircleShape, EdgeShape, PolygonShape} PolygonShape (->setAsBox(float hx, float hy) Build vertices to represent an axis-aligned box.) */ body.createFixture(fixtureDef); bodyShape.dispose(); // lo puoi fare perchè la shape è clonata quando viene assegnata alla fixture } private void MakeCircleFixture(float radius,BodyDef.BodyType bodyType, float density,float restitution, Vector2 pos,float angle){ FixtureDef fixtureDef=new FixtureDef(); fixtureDef.density=density; fixtureDef.restitution=restitution; fixtureDef.shape=new CircleShape(); fixtureDef.shape.setRadius(BoxObjectManager.ConvertToBox(radius)); body.createFixture(fixtureDef); fixtureDef.shape.dispose(); } // Step 4 - World Update Fixed TimeStep static final float BOX_STEP=1/120f; static final int BOX_VELOCITY_ITERATIONS=8; static final int BOX_POSITION_ITERATIONS=3; float accumulator; public void Update(float dt){ accumulator+=dt; while(accumulator>BOX_STEP){ world.step(BOX_STEP,BOX_VELOCITY_ITERATIONS,BOX_POSITION_ITERATIONS); accumulator-=BOX_STEP; } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus