Perpetuum mobil

Guten Tag, lieber Benutzer habr.com! Dies ist der dritte Artikel zum Thema. Ich arbeite den ganzen Tag, ich kann mich nicht von der erstaunlichen Box2D-Bibliothek losreißen.

Wenn Sie den ersten und zweiten Artikel nicht gelesen haben, schauen Sie unbedingt nach, es wird Spaß machen! Ich arbeite in Eclipse , ich schreibe in Java. Warum habe ich meinen Artikel so genannt? Lesen Sie weiter - und schon bald wird alles klar! Spoiler: Wir werden unsere eigene Perpetual-Motion-Maschine (auch für Autos) herstellen und vielleicht die Maschine selbst herstellen!

Bild

Abbildung 1. Perpetual Motion Machine.

Also werden wir heute versuchen, so etwas zu bekommen:

Bild

Abbildung 2. Maschine mit Motor.

Ja, das ist kein Tippfehler! Heute werden wir eine Maschine mit einem echten Motor bauen, der von dem echten nicht zu unterscheiden ist! Dies ist nicht der "Einkaufswagen" für Sie aus dem ersten Artikel.

Informationen zum Verbinden von libGDX finden Sie im ersten Artikel.

Hier ist ein Bild, das zeigt, wie meine Baugruppe aussieht. Ich habe das Utils-Paket mit der Constants-Klasse zum Core-Ordner hinzugefügt, der nur eine Konstante enthält - die Anzahl der Pixel pro Meter. Dies ist so, dass die Welt nicht gigantisch ist.

Bild

Abbildung 3. Mein Build.

Hier ist der Code für die DesktopLauncher-Klasse von com.mygdx.game.desktop:

Fügen Sie diesen Code in die Klasse ein und vergessen Sie ihn.
package com.mygdx.game.desktop; import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; import com.mygdx.game.MyGdxGame; public class DesktopLauncher { public static void main(String[] arg) { LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); //   config.width = 720; //   config.height = 480; config.backgroundFPS = 60; config.foregroundFPS = 60; new LwjglApplication(new MyGdxGame(), config); } } 


Der folgende Code für die MyGdxGame-Klasse stammt aus dem Paket com.mygdx.game. Alles ist in den Codekommentaren.

Wir erschaffen die Welt und die Maschine.
 package com.mygdx.game; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.BodyDef; import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; import com.badlogic.gdx.physics.box2d.CircleShape; import com.badlogic.gdx.physics.box2d.FixtureDef; import com.badlogic.gdx.physics.box2d.PolygonShape; import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef; import utils.Constants; public class MyGdxGame extends ApplicationAdapter { private OrthographicCamera camera; private boolean DEBUG = false; private World world; private Box2DDebugRenderer b2dr; //  private Body box; //   private Body backweel; //   private Body frontweel; private Body floor; private Body triangle; //   private float m = -20; public void create() { float w = Gdx.graphics.getWidth(); float h = Gdx.graphics.getHeight(); camera = new OrthographicCamera(); camera.setToOrtho(false, w / 2, h / 2); world = new World(new Vector2(0, -9.8f), false); b2dr = new Box2DDebugRenderer(); //   floor = createFloor(); //   triangle = createTriangle(20f, 40f, 30f, -2f, -2f, 5f); triangle = createTriangle(50f, 60f, 80f, -2f, 10f, -2f); triangle = createTriangle(100f, 160f, 200f, -2f, 20f, -2f); triangle = createTriangle(280f, 290f, 300f, -2f, 100f, -2f); //   box = createBox(); //   backweel = createWeel(-1f, 2f); frontweel = createWeel(4.5f, 2f); //        connected(); } //      public void connected() { revJoint(box, backweel, -1f * 10 / utils.Constants.PPM, -2f * 10 / utils.Constants.PPM); revJoint(box, frontweel, 4.5f * 10 / utils.Constants.PPM, -2f * 10 / utils.Constants.PPM); } //   !  ! public void revJoint(Body body1, Body body2, float xo, float yo) { //   RevoluteJointDef rjd = new RevoluteJointDef(); //    rjd.enableMotor = true; //  .      ,  ,  //  .    .   -   ! rjd.motorSpeed = m; //   rjd.maxMotorTorque = 30; rjd.collideConnected = false; rjd.bodyA = body1; rjd.bodyB = body2; rjd.localAnchorA.set(xo, yo); world.createJoint(rjd); } public void render() { update(Gdx.graphics.getDeltaTime()); Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); b2dr.render(world, camera.combined.scl(Constants.PPM)); } public void resize(int width, int height) { camera.setToOrtho(false, width / 2, height / 2); } public void dispose() { world.dispose(); b2dr.dispose(); } public void update(float delta) { world.step(1 / 60f, 6, 2); cameraUpdate(delta); inputUpdate(delta); } //       ,     public void inputUpdate(float delta) { if (Gdx.input.isKeyPressed(Keys.SPACE)) { box.setLinearVelocity(5, 5); } } //     public void cameraUpdate(float delta) { Vector3 position = camera.position; position.x = box.getPosition().x * Constants.PPM; position.y = box.getPosition().y * Constants.PPM; camera.position.set(position); camera.update(); } //     public Body createBox() { Vector2[] verticles = new Vector2[8]; verticles[0] = new Vector2(2f * 10 / utils.Constants.PPM, 2f * 10 / utils.Constants.PPM); verticles[1] = new Vector2(2f * 10 / utils.Constants.PPM, -2f * 10 / utils.Constants.PPM); verticles[2] = new Vector2(-2f * 10 / utils.Constants.PPM, 2f * 10 / utils.Constants.PPM); verticles[3] = new Vector2(-2f * 10 / utils.Constants.PPM, -2f * 10 / utils.Constants.PPM); verticles[4] = new Vector2(6f * 10 / utils.Constants.PPM, -2f * 10 / utils.Constants.PPM); verticles[5] = new Vector2(6f * 10 / utils.Constants.PPM, 0f * 10 / utils.Constants.PPM); verticles[6] = new Vector2(-1f * 10 / utils.Constants.PPM, -2f * 10 / utils.Constants.PPM); verticles[7] = new Vector2(4.5f * 10 / utils.Constants.PPM, -2f * 10 / utils.Constants.PPM); PolygonShape shape = new PolygonShape(); Body fBody; BodyDef def = new BodyDef(); def.type = BodyDef.BodyType.DynamicBody; def.position.set(0, 0); def.fixedRotation = false; fBody = world.createBody(def); shape.set(verticles); //    FixtureDef fd = new FixtureDef(); // ,     fd.shape = shape; //   fd.friction = 0.5f; //   fd.density = 3f; //   fd.restitution = 0.5f; fBody.createFixture(fd); fBody.createFixture(fd); shape.dispose(); return fBody; } //   public Body createWeel(float xo, float yo) { CircleShape shape = new CircleShape(); Body fBody; BodyDef def = new BodyDef(); def.type = BodyDef.BodyType.DynamicBody; def.position.set(xo * 10 / utils.Constants.PPM, yo * 10 / utils.Constants.PPM); def.fixedRotation = false; fBody = world.createBody(def); shape.setRadius(2.0f * 10 / utils.Constants.PPM); //    FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.7f; fd.density = 3f; fd.restitution = 0.5f; fBody.createFixture(fd); shape.dispose(); return fBody; } //   public Body createFloor() { Vector2[] verticles = new Vector2[4]; verticles[0] = new Vector2(-100f * 10 / utils.Constants.PPM, -2f * 10 / utils.Constants.PPM); verticles[1] = new Vector2(300f * 10 / utils.Constants.PPM, -2f * 10 / utils.Constants.PPM); verticles[2] = new Vector2(300f * 10 / utils.Constants.PPM, -5f * 10 / utils.Constants.PPM); verticles[3] = new Vector2(-100f * 10 / utils.Constants.PPM, -5f * 10 / utils.Constants.PPM); PolygonShape shape = new PolygonShape(); Body fBody; BodyDef def = new BodyDef(); def.type = BodyDef.BodyType.StaticBody; def.position.set(0, 0); def.fixedRotation = true; fBody = world.createBody(def); shape.set(verticles); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.5f; fd.density = 3f; fd.restitution = 0.5f; fBody.createFixture(fd); shape.dispose(); return fBody; } //    ! public Body createTriangle(float xo, float x1, float x2, float yo, float y1, float y2) { Vector2[] verticles = new Vector2[3]; verticles[0] = new Vector2(xo * 10 / utils.Constants.PPM, yo * 10 / utils.Constants.PPM); verticles[1] = new Vector2(x1 * 10 / utils.Constants.PPM, y1 * 10 / utils.Constants.PPM); verticles[2] = new Vector2(x2 * 10 / utils.Constants.PPM, y2 * 10 / utils.Constants.PPM); PolygonShape shape = new PolygonShape(); Body fBody; BodyDef def = new BodyDef(); def.type = BodyDef.BodyType.StaticBody; def.position.set(0, 0); def.fixedRotation = true; fBody = world.createBody(def); shape.set(verticles); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.5f; fd.density = 10f; fd.restitution = 0f; fBody.createFixture(fd); shape.dispose(); return fBody; } } 



Bild
Abbildung 4. Was bekommen wir beim Kompilieren?

Wir schaffen aus unserem Auto einen Motor, der seine Räder dreht. Aber es funktioniert für immer, weil es keine Energieverluste gibt. So schaffen wir unsere Perpetual Motion Maschine!

Und du fragst dich, was am Ende des Levels ist? Senden Sie Screenshots in den Kommentaren, während Sie gehen. Vielen Dank, dass Sie den Artikel bis zum Ende gelesen haben! Ich hoffe, Sie können Ihr eigenes Auto in Box2D erstellen!

PS Ich freue mich auf Ihre Fragen in den Kommentaren! Es lebe die Spielphysik!

Source: https://habr.com/ru/post/de450442/


All Articles