Hi,
I am trying to rotate a ball using Jbox2D. I am new in this field. Please help me.
Here is my code
=====================
public class PhysicsWorld extends View{
protected static final int GUIUPDATEIDENTIFIER = 0x231;
public int targetFPS = 40;
public float timeStep = 10.0f / targetFPS;
public int iterations = 25;
public float angle = 90.0f;
private Body body1, body2;
//private int count = 0;
private AABB worldAABB;
public World world;
public Physics physics;
private PolygonDef groundShapeDef;
public int World_W,World_H;
private Paint paint;
private float radius=15;
public PhysicsWorld(Context context,int W,int H)
{
super(context);
World_W=W;
World_H=H;
// Step 1: Create Physics World Boundaries
worldAABB = new AABB();
Vec2 min = new Vec2(-50, -50);
Vec2 max = new Vec2(World_W + 50, World_H + 50);
worldAABB.lowerBound.set(min);
worldAABB.upperBound.set(max);
// Step 2: Create Physics World with Gravity
Vec2 gravity = new Vec2((float) 0.0, (float) 0.0);
boolean doSleep = true;
world = new World(worldAABB, gravity, doSleep);
body1=new Body();
body2=new Body();
//
paint=new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
}
public void addBall()
{
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.position.set((float)(World_W - 50) , (float)(World_H-50));
body1 = world.createBody(bodyDef);
bodyDef.position.set((float)(World_W - 100) , (float)(World_H-100));
body2 = world.createBody(bodyDef);
// Create Shape with Properties
CircleDef circle1 = new CircleDef();
CircleDef circle2 = new CircleDef();
circle1.radius = (float) radius;
circle1.density = (float) 1.0;
circle1.restitution=0.5f;
circle1.radius = (float) (radius - 10);
circle1.density = (float) 1.0;
circle1.restitution=0.5f;
// Assign shape to Body
body1.createShape(circle1);
body2.createShape(circle2);
}
public void update()
{
world.step( timeStep , iterations);
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas)
{
// draw balls
canvas.drawCircle(body1.getPosition().x,World_H- body1.getPosition().y, radius, paint);
canvas.drawCircle(body2.getPosition().x,World_H- body2.getPosition().y, radius, paint);
RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.body1 = body1;
revoluteJointDef.body2 = body2;
revoluteJointDef.collideConnected = false;
revoluteJointDef.enableMotor = true;
revoluteJointDef.maxMotorTorque = 75;
revoluteJointDef.motorSpeed = 90.0f;// * DEGTORAD;//90 degrees per second
world.createJoint(revoluteJointDef);
}
}
Please help me. Ball1 will be fixed and Ball2 will rotate in respect to ball1. Please help me.
OR
help me to rotate a single ball in respect to a point
Thanks & Regards,
Daina.
I am trying to rotate a ball using Jbox2D. I am new in this field. Please help me.
Here is my code
=====================
public class PhysicsWorld extends View{
protected static final int GUIUPDATEIDENTIFIER = 0x231;
public int targetFPS = 40;
public float timeStep = 10.0f / targetFPS;
public int iterations = 25;
public float angle = 90.0f;
private Body body1, body2;
//private int count = 0;
private AABB worldAABB;
public World world;
public Physics physics;
private PolygonDef groundShapeDef;
public int World_W,World_H;
private Paint paint;
private float radius=15;
public PhysicsWorld(Context context,int W,int H)
{
super(context);
World_W=W;
World_H=H;
// Step 1: Create Physics World Boundaries
worldAABB = new AABB();
Vec2 min = new Vec2(-50, -50);
Vec2 max = new Vec2(World_W + 50, World_H + 50);
worldAABB.lowerBound.set(min);
worldAABB.upperBound.set(max);
// Step 2: Create Physics World with Gravity
Vec2 gravity = new Vec2((float) 0.0, (float) 0.0);
boolean doSleep = true;
world = new World(worldAABB, gravity, doSleep);
body1=new Body();
body2=new Body();
//
paint=new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
}
public void addBall()
{
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.position.set((float)(World_W - 50) , (float)(World_H-50));
body1 = world.createBody(bodyDef);
bodyDef.position.set((float)(World_W - 100) , (float)(World_H-100));
body2 = world.createBody(bodyDef);
// Create Shape with Properties
CircleDef circle1 = new CircleDef();
CircleDef circle2 = new CircleDef();
circle1.radius = (float) radius;
circle1.density = (float) 1.0;
circle1.restitution=0.5f;
circle1.radius = (float) (radius - 10);
circle1.density = (float) 1.0;
circle1.restitution=0.5f;
// Assign shape to Body
body1.createShape(circle1);
body2.createShape(circle2);
}
public void update()
{
world.step( timeStep , iterations);
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas)
{
// draw balls
canvas.drawCircle(body1.getPosition().x,World_H- body1.getPosition().y, radius, paint);
canvas.drawCircle(body2.getPosition().x,World_H- body2.getPosition().y, radius, paint);
RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.body1 = body1;
revoluteJointDef.body2 = body2;
revoluteJointDef.collideConnected = false;
revoluteJointDef.enableMotor = true;
revoluteJointDef.maxMotorTorque = 75;
revoluteJointDef.motorSpeed = 90.0f;// * DEGTORAD;//90 degrees per second
world.createJoint(revoluteJointDef);
}
}
Please help me. Ball1 will be fixed and Ball2 will rotate in respect to ball1. Please help me.
OR
help me to rotate a single ball in respect to a point
Thanks & Regards,
Daina.