Libgdx revolutejointdef Joint

Source: Internet
Author: User
Tags libgdx

Original link http://blog.csdn.net/cng1991/article/details/7307881

I recently read about box2d in libgdx. It is introduced on the Internet that libgdx encapsulates box2d through JNI.

Today we saw the revolutejointdef joint. The function of the revolutejointdef joint is to connect two objects with a line to achieve the effect of mutual motion between the two objects.

First paste the code

package com.cng;import com.badlogic.gdx.ApplicationListener;import com.badlogic.gdx.Gdx;import com.badlogic.gdx.InputProcessor;import com.badlogic.gdx.graphics.GL10;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.BodyDef.BodyType;import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;import com.badlogic.gdx.physics.box2d.Fixture;import com.badlogic.gdx.physics.box2d.QueryCallback;import com.badlogic.gdx.physics.box2d.World;import com.badlogic.gdx.physics.box2d.joints.MouseJoint;import com.badlogic.gdx.physics.box2d.joints.MouseJointDef;public abstract class BoxTest implements InputProcessor,ApplicationListener{OrthographicCamera camera;Box2DDebugRenderer renderer;MouseJoint mouseJoint;World world;Body grounpBody;Body hitBody;protected abstract void createWorld(World world);Vector2 tmp=new Vector2();@Overridepublic void create(){camera=new OrthographicCamera(48, 32);camera.position.set(0, 15, 0);renderer=new Box2DDebugRenderer();world=new World(new Vector2(0,-10), true);createWorld(world);BodyDef bodyDef=new BodyDef();grounpBody=world.createBody(bodyDef);Gdx.input.setInputProcessor(this);}@Overridepublic void dispose(){world.dispose();renderer.dispose();world=null;renderer=null;hitBody=null;mouseJoint=null;}@Overridepublic void pause(){}@Overridepublic void render(){GL10 gl=Gdx.graphics.getGL10();gl.glClear(GL10.GL_COLOR_BUFFER_BIT);camera.update();camera.apply(gl);world.step(Gdx.graphics.getDeltaTime(), 3, 3);renderer.render(world, camera.combined);}@Overridepublic void resize(int arg0, int arg1){}@Overridepublic void resume(){}@Overridepublic boolean keyDown(int arg0){return false;}@Overridepublic boolean keyTyped(char arg0){return false;}@Overridepublic boolean keyUp(int arg0){return false;}@Overridepublic boolean scrolled(int arg0){return false;}Vector3 testPoint=new Vector3();QueryCallback callback=new QueryCallback(){@Overridepublic boolean reportFixture(Fixture fixture){if(fixture.testPoint(testPoint.x, testPoint.y)){hitBody=fixture.getBody();return false;}else {return true;}}};@Overridepublic boolean touchDown(int x, int y, int arg2, int arg3){hitBody=null;camera.unproject(testPoint.set(x, y, 0));world.QueryAABB(callback, testPoint.x-0.0001f, testPoint.y-0.0001f, testPoint.x+0.0001f, testPoint.y+0.0001f);if(hitBody==grounpBody) hitBody=null;if(hitBody!=null&&hitBody.getType()==BodyType.KinematicBody){return false;}if(hitBody!=null){MouseJointDef def=new MouseJointDef();def.bodyA=grounpBody;def.bodyB=hitBody;def.collideConnected=true;def.target.set(testPoint.x, testPoint.y);def.maxForce=100*hitBody.getMass();mouseJoint=(MouseJoint) world.createJoint(def);hitBody.setAwake(true);}return false;}Vector2 target=new Vector2();@Overridepublic boolean touchDragged(int x, int y, int arg2){if(mouseJoint!=null){camera.unproject(testPoint.set(x, y, 0));mouseJoint.setTarget(target.set(testPoint.x,testPoint.y));}return false;}@Overridepublic boolean touchMoved(int arg0, int arg1){return false;}@Overridepublic boolean touchUp(int arg0, int arg1, int arg2, int arg3){if(mouseJoint!=null){world.destroyJoint(mouseJoint);mouseJoint=null;}return false;}}

Package COM. CNG; import android. OS. bundle; import COM. badlogic. GDX. backends. android. androidapplication; 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. edgeshape; 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. bodydef. bodytype; import COM. badlogic. GDX. physics. box2d. joints. revolutejointdef; public class mygameactivity extends androidapplication {class mygamelisten extends boxtest {@ overrideprotected void createworld (World) {body ground; {bodydef = new bodydef (); // create a physical object, ground = World. createbody (bodydef); edgeshape shape = new edgeshape (); // display shape of the new physical object. set (New vector2 (-40, 0), new vector2 (40, 0); ground. createfixture (shape, 0.0f); // make the physical object have an external surface, and you can see the image} {body prebody = ground; // define the object before the joint polygonshape shape = new polygonshape (); shape. setasbox (0.5f, 0.2f); revolutejointdef RJ = NULL; For (INT I = 0; I <30; I ++) {bodydef def = new bodydef (); def. type = bodytype. dynamicbody; def. position. set (0.5f + I, 25f); Body = World. createbody (DEF); // defines the object fixturedef FD = new fixturedef (); FD. shape = shape; FD. friction = 0.2f; FD. density = 20366f; body. createfixture (FD); vector2 anchor = new vector2 (I, 25); RJ = new revolutejointdef (); RJ. initialize (prebody, body, anchor); prebody = body; World. createjoint (RJ) ;}}}@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); initialize (New mygamelisten (), false );}}

I can only explain that much... Limited capabilities. After learning more, I will modify it to explain more details.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.