Home > other >  JBox2d beginner... Why not free fall and collision rebound?
JBox2d beginner... Why not free fall and collision rebound?

Time:09-24

According to use under android Box2d, online tutorials wrote a demo, vertical drops to the ground, a ball ball drop speed is uniform, and fell to the ground will not rebound, is want to broken head,,, the HTML code is as follows, also please people ah,,, Fried head
Public class DemoView extends the View implements Runnable {
//box2d
Private World myWorld.
Private Body circle;
Private Body mGround;
Private Vec2 gravity;
Private int the radius=10;
Private Boolean isSleep=true;
Private Boolean isStatic=false;
//screen
Private float density;
Private float s_w, s_h;
//the draw
Private Paint mPaint;
Private Paint mPaintGround;
//thread
Private ExecutorService ExecutorService;
Private Boolean isThreadWork=true;
Private final static int DUR_TIME=16;
Public DemoView Context (Context) {
super(context);
Initial ();
CreateWorld ();

}
//initial
Private void initial () {
Density=getResources (). GetDisplayMetrics (). The density;
S_w=getResources (). GetDisplayMetrics () widthPixels;
S_h=getResources (). GetDisplayMetrics () heightPixels;
MPaint=new Paint ();
MPaint. SetStyle (Paint. Style. The FILL);
MPaint. SetColor (Color. BLUE);
MPaint. SetAntiAlias (true);
MPaintGround=new Paint ();
MPaintGround. SetStyle (Paint. Style. The FILL);
MPaintGround. SetColor (Color. GRAY);
The executorService=Executors. NewCachedThreadPool ();
}
//create world
Private void createWorld () {
Gravity=new Vec2 (10 f, f);
MyWorld=new World (gravity, isSleep);
MyWorld. SetContinuousPhysics (true);
Circle=createCircle ();
MGround=createGround ();

}
//create Circle
Private Body createCircle () {
CircleShape CircleShape=new CircleShape ();
CircleShape. M_radius=radius;
FixtureDef FixtureDef=new FixtureDef ();
FixtureDef. Density=1.0 f;
FixtureDef. Friction=0.2 f;
FixtureDef. Restitution=0.8 f;
FixtureDef. Shape=circleShape;

BodyDef BodyDef=new BodyDef ();
BodyDef. Type=BodyType. DYNAMIC;
BodyDef. Position. Set (s_w/2, 10);

Body Body=myWorld. CreateBody (bodyDef);
Body. CreateFixture (fixtureDef);

Return the body;
}
//create ground
Private Body createGround () {
PolygonShape PolygonShape=new PolygonShape ();
PolygonShape. SetAsBox (s_w/2, 2);

FixtureDef FixtureDef=new FixtureDef ();
FixtureDef. Shape=polygonShape;
FixtureDef. Density=0 f;
FixtureDef. Friction=0.3 f;
FixtureDef. Restitution=0.2 f;

BodyDef BodyDef=new BodyDef ();
BodyDef. Type=BodyType. STATIC;
BodyDef. Position. Set (s_w/2, s_h/2);

Body Body=myWorld. CreateBody (bodyDef);
Body. CreateFixture (fixtureDef);
Return the body;
}

@ Override
Protected void ontouch (Canvas, Canvas) {
Super. Ontouch (canvas);
Float x, y;
X=circle. GetPosition (). X;
Y=circle. GetPosition (.) y;
Canvas. Methods like drawCircle (x, y, radius, mPaint);
X=mGround. GetPosition (). X;
Y=mGround. GetPosition (.) y;
Canvas. DrawRect (0, s_h/2, s_w, s_h/2 + 30, mPaintGround);
The v (" roytest ", "x is" + x + ", y is "+ y);

}

@ Override
Public void onWindowFocusChanged (Boolean hasWindowFocus) {
Super. OnWindowFocusChanged (hasWindowFocus);
If (hasWindowFocus) {
IsThreadWork=true;
The executorService. Execute (this);
} else {
IsThreadWork=false;
}
The v (" roytest ", "on the focus changed" + hasWindowFocus);
}

@ Override
Public void the run () {
While (isThreadWork) {
MyWorld. Step (30,10,8);
Float t1=System. CurrentTimeMillis ();
PostInvalidate ();
Float t2=System. CurrentTimeMillis ();
Float t=t2 - t1;
If (tTry {
Thread.sleep ((long) (DUR_TIME - t));
{} catch InterruptedException (e)
The v (" roytest ", "interrupt" + Log. GetStackTraceString (e));
}
}
PostInvalidate ();
}
}
}
  • Related