Box2d example -- demo3 bicycle

Source: Internet
Author: User
Package COM. cvte. game; import COM. badlogic. GDX. applicationadapter; import COM. badlogic. GDX. GDX; import COM. badlogic. GDX. input. keys; import COM. badlogic. GDX. inputprocessor; import COM. badlogic. GDX. graphics. gl20; import COM. badlogic. GDX. graphics. orthographiccamera; 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. bodydef. bodytype; import COM. badlogic. GDX. physics. box2d. box2ddebugrenderer; import COM. badlogic. GDX. physics. box2d. circleshape; import COM. badlogic. GDX. physics. box2d. edgeshape; import COM. badlogic. GDX. physics. box2d. fixturedef; import COM. badlogic. GDX. physics. box2d. world; import COM. badlogic. GDX. physics. box2d. joints. distancejoint; import COM. badlogic. GDX. physics. box2d. joints. extends; public class demo3 extends applicationadapter implements inputprocessor {Private Static final float pxtm = 80; protected orthographiccamera mcamera; private extends mrenderer; private world mworld; private body mbodya; private body mbodyb; private distancejoint mdistancejoint; @ overridepublic void create () {float camerawidth = GDX. graphics. getwidth ()/pxtm; float cameraheight = GDX. graphics. getheight ()/pxtm; mcamera = new orthographiccamera (camerawidth, cameraheight); mcamera. position. set (camerawidth/2, cameraheight/2, 0); mcamera. update (); mrenderer = new box2ddebugrenderer (); mworld = New World (New vector2 (0,-10), true ); /// ground // {// bodydef BD = new bodydef (); // BD. type = bodytype. staticbody; // BD. position. set (toworldsize (0), toworldsize (20); // edgeshape = new edgeshape (); // edgeshape. set (New vector2 (toworldsize (0), toworldsize (0), new vector2 (toworldsize (GDX. graphics. getwidth (), toworldsize (0); // fixturedef FD = new fixturedef (); // FD. shape = edgeshape; // body ground = mworld. createbody (BD); // ground. createfixture (FD); //} // wall {bodydef BD = new bodydef (); BD. type = bodytype. staticbody; edgeshape = new edgeshape (); fixturedef FD = new fixturedef (); FD. shape = edgeshape; body box = mworld. createbody (BD); edgeshape. set (New vector2 (toworldsize (0), toworldsize (0), new vector2 (toworldsize (GDX. graphics. getwidth (), toworldsize (0); box. createfixture (FD); edgeshape. set (New vector2 (toworldsize (GDX. graphics. getwidth (), toworldsize (0), new vector2 (toworldsize (GDX. graphics. getwidth (), toworldsize (GDX. graphics. getheight (); box. createfixture (FD); edgeshape. set (New vector2 (toworldsize (GDX. graphics. getwidth (), toworldsize (GDX. graphics. getheight (), new vector2 (0, toworldsize (GDX. graphics. getheight (); box. createfixture (FD); edgeshape. set (New vector2 (toworldsize (0), toworldsize (GDX. graphics. getheight (), new vector2 (0, toworldsize (0); box. createfixture (FD);} // ball {bodydef BD = new bodydef (); BD. type = bodytype. dynamicbody; BD. position. set (toworldsize (400), toworldsize (800); circleshape = new circleshape (); circleshape. setradius (toworldsize (20); fixturedef FD = new fixturedef (); FD. shape = circleshape; FD. density = 1; FD. restitution = 1; body ball = mworld. createbody (BD); ball. createfixture (FD);} createbodies (); createdistancejoint (); GDX. input. setinputprocessor (this) ;}@ overridepublic void dispose () {mrenderer. dispose (); mworld. dispose (); mcamera = NULL ;}@ overridepublic void render () {GDX. GL. glclearcolor (1, 1, 1, 1); GDX. GL. glclear (gl1_gl _ color_buffer_bit); mworld. STEP (GDX. app. getgraphics (). getdeltatime (), 6, 2); mrenderer. render (mworld, mcamera. combined);} private float toworldsize (float POS) {return (POS/pxtm);}/*** create two rigid bodies for joint connection */private void createbodies () {vector2 POSA = new vector2 (toworldsize (100), toworldsize (100); vector2 POSB = new vector2 (toworldsize (200), toworldsize (100 )); {bodydef BD = new bodydef (); BD. type = bodytype. dynamicbody; BD. position. set (POSA. x, POSA. y); circleshape = new circleshape (); circleshape. setradius (toworldsize (30); fixturedef FD = new fixturedef (); FD. shape = circleshape; FD. density = 1; FD. friction = 0.5f; FD. restitution = 0; mbodya = mworld. createbody (BD); mbodya. createfixture (FD) ;}{ bodydef BD = new bodydef (); BD. type = bodytype. dynamicbody; BD. position. set (POSB. x, POSB. y); circleshape = new circleshape (); circleshape. setradius (toworldsize (15); fixturedef FD = new fixturedef (); FD. shape = circleshape; FD. density = 1; FD. friction = 0.5f; FD. restitution = 0; mbodyb = mworld. createbody (BD); mbodyb. createfixture (FD) ;}}/*** create distance joint */private void createdistancejoint () {distancejointdef djd = new distancejointdef (); djd. initialize (mbodya, mbodyb, mbodya. getposition (), mbodyb. getposition (); djd. collideconnected = true; mdistancejoint = (distancejoint) mworld. createjoint (djd) ;}@ overridepublic Boolean keydown (INT keycode) {If (keycode = keys. left) {mbodya. setangularvelocity (2f);} else if (keycode = keys. right) {mbodya. setangularvelocity (-2f);} return false ;}@ overridepublic Boolean keyup (INT keycode) {return false ;}@ overridepublic Boolean keytyped (char character) {return false ;} @ overridepublic Boolean touchdown (INT screenx, int screeny, int pointer, int button) {return false ;}@ overridepublic Boolean touchup (INT screenx, int screeny, int pointer, int button) {return false ;}@ overridepublic Boolean touchdragged (INT screenx, int screeny, int pointer) {return false ;}@ overridepublic Boolean mousemoved (INT screenx, int screeny) {return false ;} @ overridepublic Boolean scrolled (INT amount) {return false ;}}

Box2d example -- demo3 bicycle

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.