jBox2d android drawing dynamic bodies - android

I started off with the tutorial here and added drawing code for a view (covering the entire screen). The scene appears static and not dynamic even though I'm calling world.step(,). How does one make the scene dynamic and is my drawing the right implementation or is there a better way (using jBox2d functions)?
private class PhysicsView extends View {
Paint mPaint;
public PhysicsView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Style.FILL_AND_STROKE);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
synchronized (canvas) {
Body[] b = mWorld.bodies;
for (int i = 0; i < b.length; i++) {
if (b[i] != null) {
Float mass = b[i].getMass();
Vec2 v = b[i].getPosition();
canvas.drawCircle(v.x, v.y, 15, mPaint);
}
}
Paint p2 = new Paint(mPaint);
p2.setColor(Color.GREEN);
Vec2 base = mWorld.groundBody.getPosition();
canvas.drawRect(new RectF((float) base.x - 25.0f,
(float) base.y - 10.0f, (float) base.x + 25.0f,
(float) base.y + 10.0f), p2);
canvas.drawLine(0.0f, 0.0f,
mWorld.world.getWorldAABB().upperBound.x, mWorld.world
.getWorldAABB().upperBound.y, p2);
}
}
}
public class PhysicsWorld {
public int targetFPS = 40;
public int timeStep = (1000 / targetFPS);
public int iterations = 5;
public Body[] bodies = new Body[50];
public Body groundBody;
private int count = 0;
private AABB worldAABB;
public World world;
private BodyDef groundBodyDef;
private PolygonDef groundShapeDef;
private Vec2 screenDimensions;
public void create(Vec2 dimens) {
screenDimensions = dimens;
worldAABB = new AABB();
worldAABB.lowerBound.set(new Vec2((float) -1*dimens.x, (float) -1*dimens.y));
worldAABB.upperBound.set(new Vec2((float) dimens.x, (float) dimens.y));
Vec2 gravity = new Vec2((float) 0.0, (float) 0.0);
boolean doSleep = true;
world = new World(worldAABB, gravity, doSleep);
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) dimens.x/2, (float) dimens.y - 10.0f));
groundBody = world.createBody(groundBodyDef);
groundBody.m_mass = 200.0f;
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float) 50.0, (float) 10.0);
groundShapeDef.density = 1.0f;
groundShapeDef.friction = 0.3f;
groundBody.createShape(groundShapeDef);
}
public void addBall() {
float x = (float) Math.random() * screenDimensions.x;
float y = (float) Math.random() * screenDimensions.y;
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) x, (float) y));
bodyDef.massData.mass = 20.0f;
bodies[count] = world.createBody(bodyDef);
bodies[count].m_type = Body.e_dynamicType;
bodies[count].m_linearVelocity = new Vec2(1.0f, 2.0f);
CircleDef circle = new CircleDef();
circle.radius = (float) 1.8;
circle.density = (float) 1.0;
circle.type = ShapeType.CIRCLE_SHAPE;
bodies[count].createShape(circle);
count++;
}
public void update() {
world.step(timeStep, iterations);
}
public void changeGravity(Vec2 gravity) {
world.setGravity(gravity);
}
}

There are a number of issues with your code....but this answer is probably way too late to be of any help anyways.
Firstly, world.createBody() seems to have been deprecated. Now you have to explicitly call world.createDynamicBody() in the latest version of the Android Box2D jar that I have.
Anyways...my version goes something like this...not perfect but at least its dynamic.
package com.box2D;
import java.util.ArrayList;
import org.jbox2d.collision.AABB;
import org.jbox2d.collision.CircleDef;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
public class PhysicsWorld
{
public float timeStep = 1;
public int iterations = 1;
private ArrayList<Body> bodies;
private int count = 0;
private AABB worldAABB;
private World world;
private BodyDef groundBodyDef;
private PolygonDef boxShapeDef;
//a paint
private Paint paint;
public void create()
{
//create the paint
paint = new Paint();
paint.setColor(0xFFFFFF);
// Step 1: Create Physics World Boundaries
worldAABB = new AABB();
worldAABB.lowerBound.set(new Vec2((float) -100.0, (float) -100.0));
worldAABB.upperBound.set(new Vec2((float) 100.0, (float) 300.0));
// Step 2: Create Physics World with Gravity
Vec2 gravity = new Vec2((float) 0.0, (float)10.0);
boolean doSleep = true;
world = new World(worldAABB, gravity, doSleep);
bodies = new ArrayList<Body>();
// Step 3: Create Ground Box
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) 300.0));
Body groundBody = world.createDynamicBody(groundBodyDef);
boxShapeDef = new PolygonDef();
boxShapeDef.setAsBox((float) 50.0, (float) 10.0);
groundBody.createShape(boxShapeDef);
}
public void addBall()
{
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.position.set((float)16.0+(count * 10), (float)24.0);
Body newBody = world.createDynamicBody(bodyDef);
// Create Shape with Properties
CircleDef circle = new CircleDef();
circle.radius = (float)5;
circle.density = (float)1.0;
// Assign shape to Body
newBody.createShape(circle);
newBody.setMassFromShapes();
newBody.m_userData = "Circle";
bodies.add(newBody);
// Increase Counter
count += 1;
}
public void addBox()
{
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.position.set((float)36.0+(count * 10), (float)24.0);
Body newBody = world.createDynamicBody(bodyDef);
// Create Shape with Properties
boxShapeDef = new PolygonDef();
boxShapeDef.setAsBox((float) 50.0, (float) 10.0);
// Assign shape to Body
newBody.createShape(boxShapeDef);
newBody.setMassFromShapes();
newBody.m_userData = "Box";
bodies.add(newBody);
// Increase Counter
count += 1;
}
/*
*
*
* Physics Update
*/
private Vec2 position;
private float angle;
public void Update(Canvas canvas)
{
// Update Physics World
world.step(timeStep/5, 1);
// Print info of latest body
for (Body i : bodies)
{
position = i.getPosition();
angle = i.getAngle();
if(i.m_userData == "Circle")
canvas.drawCircle(position.x, position.y, 5,paint );
if(i.m_userData == "Box")
canvas.drawRect((float)(position.x -5), (float)(position.y - 5), (float)(position.x + 5), (float)(position.y + 5), paint );
Log.v("Physics Test", "Pos: (" + position.x + ", " + position.y + "), Angle: " + angle);
}
}
}

In response to this part of your question:
"is my drawing the right implementation or is there a better way"
This is from the Box2D website:
What units does Box2D use?
Box2D is tuned for meters-kilograms-seconds (MKS). Your moving objects should be between 0.1 - 10 meters. Do not use pixels as units! You will get a jittery simulation.
How do I convert pixels to meters?
Suppose you have a sprite for a character that is 100x100 pixels. You decide to use a scaling factor that is 0.01. This will make the character physics box 1m x 1m. So go make a physics box that is 1x1. Now suppose the character starts out at pixel coordinate (345,679). So position the physics box at (3.45,6.79). Now simulate the physics world. Suppose the character physics box moves to (2.31,4.98), so move your character sprite to pixel coordinates (231,498). Now the only tricky part is choosing a scaling factor. This really depends on your game. You should try to get your moving objects in the range 0.1 - 10 meters, with 1 meter being the sweet spot.
...
What are the biggest mistakes made by new users?
Using pixels for length instead of meters.
...
Source: http://code.google.com/p/box2d/wiki/FAQ

public class PhysicsWorld extends View{
protected static final int GUIUPDATEIDENTIFIER = 0x231;
public int targetFPS = 40;
public float timeStep = 10.0f / targetFPS;
public int iterations = 5;
private Body[] bodies;
private int count = 0;
private AABB worldAABB;
public World world;
private PolygonDef groundShapeDef;
public int World_W,World_H;
private Paint paint;
private float radius=10;
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) -10.0);
boolean doSleep = true;
world = new World(worldAABB, gravity, doSleep);
// Step 3:
//Create Ground Box :
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
Body groundBody = world.createBody(bodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float) World_W, (float) 10);
groundBody.createShape(groundShapeDef);
// up :
bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) 0.0, (float) (World_H+10.0) ));
groundBody = world.createBody(bodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float) World_W, (float) 10);
groundBody.createShape(groundShapeDef);
// left :
bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) -10, (float) 0.0 ));
groundBody = world.createBody(bodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float)10, (float) World_H);
groundBody.createShape(groundShapeDef);
// right :
bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) World_W+10, (float) 0.0 ));
groundBody = world.createBody(bodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float)10, (float) World_H);
groundBody.createShape(groundShapeDef);
//
// step 4: initialize
bodies=new Body[50];
//
paint=new Paint();
paint.setStyle(Style.FILL_AND_STROKE);
paint.setColor(Color.CYAN);
}
public void addBall() {
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
Random rnd = new Random();
bodyDef.position.set((float) radius*2+rnd.nextInt( (int)(World_W-radius*4) ), (float)2*radius+ rnd.nextInt( (int)(World_H-radius*4) ));
bodies[count] = world.createBody(bodyDef);
// Create Shape with Properties
CircleDef circle = new CircleDef();
circle.radius = (float) radius;
circle.density = (float) 1.0;
circle.friction = 0.1f;
circle.restitution=0.5f;
// Assign shape to Body
bodies[count].createShape(circle);
bodies[count].setMassFromShapes();
// Increase Counter
count += 1;
}
public void addBox() {
BodyDef bodyDef = new BodyDef();
Random rnd = new Random();
bodyDef.position.set((float)2*count+rnd.nextInt( (int)(World_W-4*count) ), (float)2*count+rnd.nextInt( (int)(World_H-count*4) ));
bodies[count] = world.createBody(bodyDef);
// Create Shape with Properties
PolygonDef boxShapeDef = new PolygonDef();
boxShapeDef.setAsBox((float) 10.0, (float) 10.0);
boxShapeDef.density = 1.0f; // A density of 0 will create a fixed Body that doesn't move
boxShapeDef.friction = 0.1f; // How much friction
boxShapeDef.restitution = 0.5f; // How bouncy is this Shape?
// Assign shape to Body
bodies[count].createShape(boxShapeDef);
bodies[count].setMassFromShapes();
bodies[count].m_userData = "Box";
count += 1;
}
public void update() {
world.step(timeStep/5, 1);
postInvalidate();
}
protected void onDraw(Canvas canvas) {
for(int j = 0;j<count;j++)
{
canvas.drawRect((float)(bodies[j].getPosition().x -10), (float)(bodies[j].getPosition().y - 10), (float)(bodies[j].getPosition().x+ 10), (float)(bodies[j].getPosition().y + 10), paint );
canvas.drawCircle(bodies[j].getPosition().x,World_H- bodies[j].getPosition().y, radius, paint);
}
}

Related

Why does wheel movement in Box2d doesn't cause car to move?

I'm working on moving vehicle based on Box 2d physics for my Android game (Java + LibGDX). When I thought I have everything up and running my, my car with two wheels stands on the ground but when I rotate the wheels The car does not move.
That's my Game class:
public class MyGdxGame implements ApplicationListener {
World world = new World(new Vector2(0, -10), true);
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
static final float BOX_STEP=1/60f;
static final int BOX_VELOCITY_ITERATIONS=6;
static final int BOX_POSITION_ITERATIONS=2;
static final int WIDTH=256;
static final int HEIGHT=169;
Hero hero;
MovableBox box;
#Override
public void create() {
camera = new OrthographicCamera();
camera.viewportHeight = HEIGHT;
camera.viewportWidth = WIDTH;
camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);
camera.update();
Vector2[] anArray;
anArray = new Vector2[4];
anArray[0]= new Vector2(-30,0);
anArray[1]= new Vector2(55,55);
anArray[2]= new Vector2(110,55);
anArray[3]= new Vector2(195,0);
//Ground body
BodyDef groundBodyDef =new BodyDef();
groundBodyDef.position.set(new Vector2(-8-13-20-20-10, -44));
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.set(anArray);
//groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);
groundBody.createFixture(groundBox, 0.0f);
//Ground body
BodyDef groundBodyDef2 =new BodyDef();
groundBodyDef2.position.set(new Vector2(13*11-4-7-8-13-20-20-10, -44));
Body groundBody2 = world.createBody(groundBodyDef2);
PolygonShape groundBox2 = new PolygonShape();
groundBox2.set(anArray);
//groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);
groundBody2.createFixture(groundBox2, 0.0f);
hero = new Hero();
hero.create(world, 0, 0);
box.create(world, WIDTH / 4 * 3, HEIGHT / 2);
boxes.add(box);
debugRenderer = new Box2DDebugRenderer();
}
private void update(){
hero.update();
for(MovableBox b : boxes) {
b.update();
}
}
#Override
public void dispose() {
}
#Override
public void render() {
update();
camera.position.set(hero.getPos(), 0f);
camera.update();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
And Hero class (which should be called a car actually)
Body body;
Body wheel, wheel2;
public void create(World world, int posx, int posy){
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(posx, posy);
body = world.createBody(bodyDef);
PolygonShape dynamicCircle = new PolygonShape();
dynamicCircle.setAsBox(8, 3);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = dynamicCircle;
fixtureDef.density = 4.0f;
fixtureDef.restitution = 0.1f;
fixtureDef.friction=0.95f;
body.createFixture(fixtureDef);
BodyDef bodyDef2 = new BodyDef();
bodyDef2.type = BodyDef.BodyType.DynamicBody;
wheel = world.createBody(bodyDef2);
CircleShape dynamicCircle2 = new CircleShape();
dynamicCircle2.setRadius(2);
FixtureDef fixtureDef2 = new FixtureDef();
fixtureDef2.shape = dynamicCircle2;
fixtureDef2.density = 4;
fixtureDef2.friction=3.0f;
fixtureDef2.restitution=0.1f;
wheel.createFixture(fixtureDef2);
wheel2 = world.createBody(bodyDef2);
wheel2.createFixture(fixtureDef2);
RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.bodyA = body;
revoluteJointDef.bodyB = wheel;
revoluteJointDef.localAnchorA.add(-8,-6);
world.createJoint(revoluteJointDef);
RevoluteJointDef revoluteJointDef2 = new RevoluteJointDef();
revoluteJointDef2.bodyA = body;
revoluteJointDef2.bodyB = wheel2;
revoluteJointDef2.localAnchorA.add(8,-6);
world.createJoint(revoluteJointDef2);
}
public Vector2 getPos(){
return body.getPosition();
}
public void update(){
if(Gdx.input.isTouched())
{
if(Gdx.input.getX()<600){
wheel.setTransform(wheel.getPosition().x,wheel.getPosition().y,wheel.getAngle()+0.1f);
wheel2.setTransform(wheel2.getPosition().x,wheel2.getPosition().y, wheel2. getAngle() + 0.1f);
}
else{
wheel.setTransform(wheel.getPosition().x,wheel.getPosition().y,wheel.getAngle()-0.1f);
wheel2.setTransform(wheel2.getPosition().x,wheel2.getPosition().y,wheel2.getAngle()-0.1f);
}
}
}
Well I think there are two things you need to consider.
First of all I think if you want your physics engine to "force" the car to move on wheels spin, you probably should look into damping parameters of BodyDef (linearDamping and angularDamping). Setting those for ground and car wheels might help you move your car on wheels spin.
The second thing is that you really need to analyze if it's the proper approach. Maybe you should think about simulating movement of your car by moving the body of your car itself and spinning your wheels. I think this approach should give you more control on the car movement.

Android draw animated dotted curve

I have been on this for days without finding a good solution (at least to me).
Basically what I need to do is draw an animated dotted curve, that would represent a missile trajectory for a basic game. This trajectory would accept starting, middle and final x,y. I read a lot of Q/A about animating curves or even straight lines just to begin, but they look really complex compared to what I have to do.
I have a button with an onclick listener which triggers the missile launch, but until now it just draws static lines without any animation.
Do you remember this? That's what i would like to achieve:
http://www.youtube.com/watch?v=UDc3ZEKl-Wc
Banana trajectory is not painted but, you got the idea.
Here again, starting from your example i tried to draw simultaneous curves using arrays and for loops, but what i got is that sometimes, not always, app crashes with a nullpointer exception, don't know why. What am i doing wrong? Here's my code:
public class DrawDottedCurve extends View {
Path[] path = new Path[8];
Path[] drawingPath = new Path[8];
PathMeasure[] measure = new PathMeasure[8];
Path[] segmentPath = new Path[8];
float[] length = new float[8];
float[] start = new float[8];
float[] percent = new float[8];
Paint paint = new Paint();
float x1;
float y1;
float x3;
float y3;
long k = 0;
Canvas canvas;
Random r = new Random();
public DrawDottedCurve(Context context, int a, int b, int c, int d) {
super(context);
x1 = a;
y1 = b;
x3 = c;
y3 = d;
paint.setAlpha(255);
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[] { 2, 4 }, 50));
for (int i = 0; i < 8; i++) {
k = r.nextInt(100 - 30) + 30;
path[i] = new Path();
path[i].moveTo(x1 + k, y1 + k); //
path[i].quadTo((x3 + k - x1 + k) / 2, (y3 + k - y1 + k) / 2,
x3 + k, y3 + k); // Calculate Bezier Curves
}
final long DRAW_TIME = 10000;
CountDownTimer timer = new CountDownTimer(DRAW_TIME, 100) {
#Override
public void onTick(long millisUntilFinished) {
Log.d("Timer", "Inizio");
for (int i = 0; i < 8; i++) {
start[i] = 0;
measure[i] = new PathMeasure();
measure[i].setPath(path[i], false);
percent[i] = ((float) (DRAW_TIME - millisUntilFinished))
/ (float) DRAW_TIME;
segmentPath[i] = new Path();
drawingPath[i] = new Path();
length[i] = measure[i].getLength() * percent[i];
measure[i].getSegment(start[i], length[i], segmentPath[i],
true);
start[i] = length[i];
drawingPath[i].addPath(segmentPath[i]);
}
invalidate();
;
}
#Override
public void onFinish() {
for (int i = 0; i < 8; i++) {
measure[i].getSegment(start[i], measure[i].getLength(),
segmentPath[i], true);
drawingPath[i].addPath(segmentPath[i]);
}
invalidate();
Log.d("Timer", "Fine");
}
};
timer.start();
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < 8; i++) {
canvas.drawPath(drawingPath[i], paint);
invalidate();
}
}
}
Assuming that you have a Canvas to draw on and a path you want to trace this code should do the work for animating it.
private void init() {
missilePath = new Path();
missileDrawPath = new Path();
mPaint = new Paint();
mPaint.setStrokeWidth(3);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Style.STROKE);
mPaint.setAntiAlias(true);
PathEffect dashEffect = new DashPathEffect(new float[] {2, 4}, 0);
mPaint.setPathEffect(dashEffect);
final long DRAW_TIME = 5000;
timer = new CountDownTimer(DRAW_TIME, 100) {
PathMeasure measure = new PathMeasure();
Path segmentPath = new Path();
float start = 0;
#Override
public void onTick(long millisUntilFinished) {
measure.setPath(missilePath, false);
float percent = ((float) (DRAW_TIME - millisUntilFinished)) / (float) DRAW_TIME;
float length = measure.getLength() * percent;
measure.getSegment(start, length, segmentPath, true);
start = length;
missileDrawPath.addPath(segmentPath);
invalidate();
}
#Override
public void onFinish() {
measure.getSegment(start, measure.getLength(), segmentPath, true);
missileDrawPath.addPath(segmentPath);
invalidate();
}
};
timer.start();
}
#Override
public void onDraw(Canvas c) {
super.onDraw(c);
c.drawPath(missileDrawPath, mPaint);
}
Basically a timer gets started and depending on the elapsed time a segment is retrieved from missilePath and added to the path that should be currently drawn.
Thank you SceLus that worked really well, here's the full code for people eventually stuck on same problem.
Class constructor takes Bezier Curve starting and final x's and y's as input and simply calculate mid control point, then draws everything according to SceLus code.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.os.CountDownTimer;
import android.view.View;
public class DrawDottedCurve extends View {
Path path = new Path();
Path drawingPath = new Path();
Paint paint = new Paint();
float x1;
float y1;
float x3;
float y3;
public DrawDottedCurve(Context context, int a, int b, int c, int d) {
super(context);
x1 = a;
y1 = b;
x3 = c;
y3 = d;
paint.setAlpha(255);
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[] { 2, 4 }, 50));
path.moveTo(x1, y1); //
path.quadTo((x3 - x1) / 2, (y3 - y1) / 2, x3, y3); // Calculate Bezier Curve
final long DRAW_TIME = 10000;
CountDownTimer timer = new CountDownTimer(DRAW_TIME, 100) {
PathMeasure measure = new PathMeasure();
Path segmentPath = new Path();
float start = 0;
#Override
public void onTick(long millisUntilFinished) {
measure.setPath(path, false);
float percent = ((float) (DRAW_TIME - millisUntilFinished))
/ (float) DRAW_TIME;
float length = measure.getLength() * percent;
measure.getSegment(start, length, segmentPath, true);
start = length;
drawingPath.addPath(segmentPath);
invalidate();
}
#Override
public void onFinish() {
measure.getSegment(start, measure.getLength(), segmentPath,
true);
drawingPath.addPath(segmentPath);
invalidate();
}
};
timer.start();
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(drawingPath, paint);
}
}

jbox2d on Android - circle not applied gravity and other staff

I am doing simple application that should put circle on Canvas when the user taps the screen and then put that circle in the PhysicalWorld I have defined.
I draw the circle in "onDraw" method, but when it is created its position does not change (seems like the gravity and staff are not applied) and the circle is static (stays on the position where it's created).
can you check this code and tell me if I am doing anything wrong:
[update]
public class PhysicsWorld extends SurfaceView implements SurfaceHolder.Callback {
private AABB worldAABB;
private World world;
private PolygonDef groundShapeDef;
public int W_width, W_height;
private static final String TAG = PhysicsWorld.class.getSimpleName();
protected static final int GUIUPDATEIDENTIFIER = 0x231;
public int targetFPS = 40;
public float timeStep = (10.0f/targetFPS);
public int iterations = 5 ;
private int count=0;
private Body[] theBodies ;
private Paint paint;
private float radius = 20;
private MyThread mMyThread;
public PhysicsWorld(Context context) {
super(context);
}
public PhysicsWorld(Context context, AttributeSet set) {
super(context, set);
getHolder().addCallback(this);
W_width = 500;
W_height = 700;
worldAABB = new AABB();
Vec2 min = new Vec2(-50, -50);
Vec2 max = new Vec2(W_width+50, W_height+50);
worldAABB.lowerBound.set(min);
worldAABB.upperBound.set(max);
Vec2 gravity = new Vec2((float) 10.0, (float) 9.8);
boolean doSleep = false;
world = new World(worldAABB, gravity, doSleep);
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
Body groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(W_width, 10);
groundBody.createShape(groundShapeDef);
// up :
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) (W_height + 10.0)));
groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(W_width, 10);
groundBody.createShape(groundShapeDef);
// left :
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2(-10, (float) 0.0));
groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(10, W_height);
groundBody.createShape(groundShapeDef);
// right :
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) W_width + 10, (float) 0.0));
groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(10, W_height);
groundBody.createShape(groundShapeDef);
theBodies = new Body[50];
paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
// setWillNotDraw(false);
}
public void addBall(int x, int y) {
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(x, y);
Log.d(TAG,"Ball Created At: " + Integer.toString(x) + "," + Integer.toString(y));
theBodies[count] = world.createBody(bodyDef);
CircleDef circle = new CircleDef();
circle.radius = radius;
circle.density = (float) 1.0;
circle.restitution = 0.5f;
theBodies[count].createShape(circle);
theBodies[count].setMassFromShapes();
count+=1;
}
public void update() {
world.step(timeStep, iterations);
postInvalidate();
}
#Override
protected void onDraw(Canvas canvas) {
paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
for (int j = 0; j < count; j++) {
canvas.drawCircle(theBodies[j].getPosition().x, W_height - theBodies[j].getPosition().y, radius, paint);
Log.v(TAG + " x: ", String.valueOf(theBodies[j].getPosition().x));
Log.v(TAG + " y:", String.valueOf(W_height - theBodies[j].getPosition().y));
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
addBall((int)event.getX(),(int)event.getY());
}
return true;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mMyThread = new MyThread(holder, this);
mMyThread.setFlag(true);
mMyThread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
My experience with box2D is in C++ and iPhone, but it seems your forgetting to make the Ball dynamic.
I've kinda guessed at the solution as I don't know the exact syntax for JBox2D (Please edit if anyone knows actual code)
bodyDef.type = BodyDef.dynamicBody;
I've had a look at the documentation on google code and I think you should use world.createDynamicBody(bodyDef);

How to draw ball using onDraw() in android

This class use to draw ball but its onDraw() function not call so ball not draw
package com.bounce.app;
import org.jbox2d.collision.AABB;
import org.jbox2d.collision.shapes.CircleDef;
import org.jbox2d.collision.shapes.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class PhysicsWorld extends SurfaceView implements SurfaceHolder.Callback {
private AABB worldAABB;
private World world;
private PolygonDef groundShapeDef;
public int W_width, W_height;
private static final String TAG = PhysicsWorld.class.getSimpleName();
protected static final int GUIUPDATEIDENTIFIER = 0x231;
public int targetFPS = 40;
public float timeStep = (10.0f/targetFPS);
public int iterations = 5;
private int count=0;
private Body[] theBodies ;
private Paint paint;
private float radius = 10;
public PhysicsWorld(Context context, int width, int height) {
super(context);
getHolder().addCallback(this);
W_width = width;
W_height = height;
worldAABB = new AABB();
Vec2 min = new Vec2(-50, -50);
Vec2 max = new Vec2(W_width+50, W_height+50);
worldAABB.lowerBound.set(min);
worldAABB.upperBound.set(max);
Vec2 gravity = new Vec2((float) 0.0, (float) -9.8);
boolean doSleep = true;
world = new World(worldAABB, gravity, doSleep);
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
Body groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(W_width, 10);
groundBody.createShape(groundShapeDef);
// up :
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) (W_height + 10.0)));
groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(W_width, 10);
groundBody.createShape(groundShapeDef);
// left :
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2(-10, (float) 0.0));
groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(10, W_height);
groundBody.createShape(groundShapeDef);
// right :
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) W_width + 10, (float) 0.0));
groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(10, W_height);
groundBody.createShape(groundShapeDef);
theBodies = new Body[50];
paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
}
public void addBall(int x, int y) {
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(x, y);
Log.d(TAG,"Ball Created At: " + Integer.toString(x) + "," + Integer.toString(y));
theBodies[count] = world.createBody(bodyDef);
// Create Shape with Properties
CircleDef circle = new CircleDef();
circle.radius = radius;
circle.density = (float) 1.0;
circle.restitution = 0.5f;
theBodies[count].createShape(circle);
theBodies[count].setMassFromShapes();
count+=1;
}
public void update() {
world.step(timeStep, iterations);
postInvalidate();
}
#Override
protected void onDraw(Canvas canvas) {
Log.v("####*****", "draw ball");
paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
for (int j = 0; j < count; j++) {
canvas.drawCircle(theBodies[j].getPosition().x, W_height - theBodies[j].getPosition().y, radius, paint);
Log.v("####*****", "draw ball");
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.v("####*****", "Add ball");
addBall((int)event.getX(),(int)event.getY());
}
return true;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG,"Surface Created");
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
Probably a duplication of Extended SurfaceView's onDraw() method never called. Try adding setWillNotDraw(false) to your constructor.

how to rotate text using canvas in Android

i was draw a pie chart using canvas in android and using the below code i draw a text on each slice of that pie chart (draw arc on path), now i want to draw the text length wise i.e. from center to end of the each slice,so how to rotate the arc using start and sweep angle.
p.addArc(mEventsRect, fStartAngle, fSweepAngle);
mBgPaints.setColor(iTextColor);
canvas.drawTextOnPath(sTextValue, p, fHOffSet, fVOffSet, mBgPaints);
You can try this snippet: (from: http://www.helloandroid.com/tutorials/how-use-canvas-your-android-apps-part-2)
int x = 75;
int y = 185;
paint.setColor(Color.GRAY);
paint.setTextSize(25);
String rotatedtext = "Rotated helloandroid :)";
//Draw bounding rect before rotating text:
Rect rect = new Rect();
paint.getTextBounds(rotatedtext, 0, rotatedtext.length(), rect);
canvas.translate(x, y);
paint.setStyle(Paint.Style.FILL);
canvas.drawText(rotatedtext , 0, 0, paint);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(rect, paint);
canvas.translate(-x, -y);
paint.setColor(Color.RED);
canvas.rotate(-45, x + rect.exactCenterX(),y + rect.exactCenterY());
paint.setStyle(Paint.Style.FILL);
canvas.drawText(rotatedtext, x, y, paint);
A bit late to the party but I had to figure this one out and it's a bit simpler than what I found around. You'll already have the x and y for your text, use these to rotate the canvas
canvas.rotate(yourDegrees, x, y)
canvas.drawText(yourText, x, y, yourPaint)
canvas.rotate(-yourDegrees, x, y)
The negative sign negates the first rotation. You could swap it around to rotate in the opposite direction.
You could do this in a loop but the rotation cycle must be done each time either coordinate changes.
may be this will help you,,
here 39.5 is radius,, this will perfectly show result on mdpi screen
protected void onDraw(){
canvas.save();
PointF pf = PointOnCircle(35f, 45f, new PointF(39.5f, 39.5f));
canvas.rotate(-45, pf.x, pf.y);
canvas.drawText("67%", pf.x, pf.y, red);//23.5
canvas.restore();
canvas.save();
PointF pfa = PointOnCircle(35f, 135f, new PointF(39.5f, 39.5f));
canvas.rotate(45, pfa.x, pfa.y);
canvas.drawText("33%", pfa.x, pfa.y, red);//23.5
canvas.restore();
canvas.save();
pfa = PointOnCircle(27.5f, 225f, new PointF(39.5f, 39.5f));
canvas.rotate(-45, pfa.x, pfa.y);
canvas.drawText("45%", pfa.x, pfa.y, red);//23.5
canvas.restore();
canvas.save();
pfa = PointOnCircle(27.5f, 315f, new PointF(39.5f, 39.5f));
canvas.rotate(45, pfa.x, pfa.y);
canvas.drawText("55%", pfa.x, pfa.y, red);//23.5
canvas.restore();}
protected static final PointF PointOnCircle(float radius, float angleInDegrees, PointF origin) {
// Convert from degrees to radians via multiplication by PI/180
float x = (float) (radius * Math.cos(angleInDegrees * Math.PI / 180F)) + origin.x;
float y = (float) (radius * Math.sin(angleInDegrees * Math.PI / 180F)) + origin.y;
return new PointF(x, y);
}
Here's how i finally did it after two days of search with help of this library https://github.com/Ken-Yang/AndroidPieChart
And equations to center text done with help of my friends and alot of search
on MainActivity onCreate or oncreateView if you are using fragments:
PieChart pie = (PieChart) rootView.findViewById(R.id.pieChart);
ArrayList<Float> alPercentage = new ArrayList<Float>();
alPercentage.add(2.0f);
alPercentage.add(8.0f);
alPercentage.add(20.0f);
alPercentage.add(10.0f);
alPercentage.add(10.0f);
alPercentage.add(10.0f);
alPercentage.add(10.0f);
alPercentage.add(10.0f);
alPercentage.add(10.85f);
alPercentage.add(9.15f);
try {
// setting data
pie.setAdapter(alPercentage);
// setting a listener
pie.setOnSelectedListener(new OnSelectedLisenter() {
#Override
public void onSelected(int iSelectedIndex) {
Toast.makeText(getActivity(),
"Select index:" + iSelectedIndex,
Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
if (e.getMessage().equals(PieChart.ERROR_NOT_EQUAL_TO_100)) {
Log.e("kenyang", "percentage is not equal to 100");
}
}
public class PieChart extends View {
public interface OnSelectedLisenter {
public abstract void onSelected(int iSelectedIndex);
}
private OnSelectedLisenter onSelectedListener = null;
private static final String TAG = PieChart.class.getName();
public static final String ERROR_NOT_EQUAL_TO_100 = "NOT_EQUAL_TO_100";
private static final int DEGREE_360 = 360;
private static String[] PIE_COLORS = null;
private static int iColorListSize = 0;
ArrayList<Float> array;
private Paint paintPieFill;
private Paint paintPieBorder;
private Paint paintCenterCircle;
private ArrayList<Float> alPercentage = new ArrayList<Float>();
private int mCenterX = 320;
private int mCenterY = 320;
private int iDisplayWidth, iDisplayHeight;
private int iSelectedIndex = -1;
private int iCenterWidth = 0;
private int iShift = 0;
private int iMargin = 0; // margin to left and right, used for get Radius
private int iDataSize = 0;
private Canvas canvas1;
private RectF r = null;
private RectF centerCircle = null;
private float fDensity = 0.0f;
private float fStartAngle = 0.0f;
private float fEndAngle = 0.0f;
float fX;
float fY;
public PieChart(Context context, AttributeSet attrs) {
super(context, attrs);
PIE_COLORS = getResources().getStringArray(R.array.colors);
iColorListSize = PIE_COLORS.length;
array = new ArrayList<Float>();
fnGetDisplayMetrics(context);
iShift = (int) fnGetRealPxFromDp(30);
iMargin = (int) fnGetRealPxFromDp(40);
centerCircle = new RectF(200, 200, 440, 440);
// used for paint circle
paintPieFill = new Paint(Paint.ANTI_ALIAS_FLAG);
paintPieFill.setStyle(Paint.Style.FILL);
// used for paint centerCircle
paintCenterCircle = new Paint(Paint.ANTI_ALIAS_FLAG);
paintCenterCircle.setStyle(Paint.Style.FILL);
paintCenterCircle.setColor(Color.WHITE);
// used for paint border
paintPieBorder = new Paint(Paint.ANTI_ALIAS_FLAG);
paintPieBorder.setStyle(Paint.Style.STROKE);
paintPieBorder.setStrokeWidth(fnGetRealPxFromDp(3));
paintPieBorder.setColor(Color.WHITE);
Log.i(TAG, "PieChart init");
}
// set listener
public void setOnSelectedListener(OnSelectedLisenter listener) {
this.onSelectedListener = listener;
}
float temp = 0;
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i(TAG, "onDraw");
float centerX = (r.left + r.right) / 2;
float centerY = (r.top + r.bottom) / 2;
float radius1 = (r.right - r.left) / 2;
radius1 *= 0.5;
float startX = mCenterX;
float startY = mCenterY;
float radius = mCenterX;
float medianAngle = 0;
Path path = new Path();
for (int i = 0; i < iDataSize; i++) {
// check whether the data size larger than color list size
if (i >= iColorListSize) {
paintPieFill.setColor(Color.parseColor(PIE_COLORS[i
% iColorListSize]));
} else {
paintPieFill.setColor(Color.parseColor(PIE_COLORS[i]));
}
fEndAngle = alPercentage.get(i);
// convert percentage to angle
fEndAngle = fEndAngle / 100 * DEGREE_360;
// if the part of pie was selected then change the coordinate
if (iSelectedIndex == i) {
canvas.save(Canvas.MATRIX_SAVE_FLAG);
float fAngle = fStartAngle + fEndAngle / 2;
double dxRadius = Math.toRadians((fAngle + DEGREE_360)
% DEGREE_360);
fY = (float) Math.sin(dxRadius);
fX = (float) Math.cos(dxRadius);
canvas.translate(fX * iShift, fY * iShift);
}
canvas.drawArc(r, fStartAngle, fEndAngle, true, paintPieFill);
float angle = (float) ((fStartAngle + fEndAngle / 2) * Math.PI / 180);
float stopX = (float) (startX + (radius/2) * Math.cos(angle));
float stopY = (float) (startY + (radius/2) * Math.sin(angle));
// if the part of pie was selected then draw a border
if (iSelectedIndex == i) {
canvas.drawArc(r, fStartAngle, fEndAngle, true, paintPieBorder);
canvas.drawLine(startX, startY, stopX, stopY, paintPieFill);
canvas.restore();
}
fStartAngle = fStartAngle + fEndAngle;
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// get screen size
iDisplayWidth = MeasureSpec.getSize(widthMeasureSpec);
iDisplayHeight = MeasureSpec.getSize(heightMeasureSpec);
if (iDisplayWidth > iDisplayHeight) {
iDisplayWidth = iDisplayHeight;
}
/*
* determine the rectangle size
*/
iCenterWidth = iDisplayWidth / 2;
int iR = iCenterWidth - iMargin;
if (r == null) {
r = new RectF(iCenterWidth - iR, // top
iCenterWidth - iR, // left
iCenterWidth + iR, // right
iCenterWidth + iR); // bottom
}
if (centerCircle == null) {
// centerCircle=new RectF(left, top, right, bottom);
}
setMeasuredDimension(iDisplayWidth, iDisplayWidth);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// get degree of the touch point
double dx = Math.atan2(event.getY() - iCenterWidth, event.getX()
- iCenterWidth);
float fDegree = (float) (dx / (2 * Math.PI) * DEGREE_360);
fDegree = (fDegree + DEGREE_360) % DEGREE_360;
// get the percent of the selected degree
float fSelectedPercent = fDegree * 100 / DEGREE_360;
// check which pie was selected
float fTotalPercent = 0;
for (int i = 0; i < iDataSize; i++) {
fTotalPercent += alPercentage.get(i);
if (fTotalPercent > fSelectedPercent) {
iSelectedIndex = i;
break;
}
}
if (onSelectedListener != null) {
onSelectedListener.onSelected(iSelectedIndex);
}
invalidate();
return super.onTouchEvent(event);
}
private void fnGetDisplayMetrics(Context cxt) {
final DisplayMetrics dm = cxt.getResources().getDisplayMetrics();
fDensity = dm.density;
}
private float fnGetRealPxFromDp(float fDp) {
return (fDensity != 1.0f) ? fDensity * fDp : fDp;
}
public void setAdapter(ArrayList<Float> alPercentage) throws Exception {
this.alPercentage = alPercentage;
iDataSize = alPercentage.size();
float fSum = 0;
for (int i = 0; i < iDataSize; i++) {
fSum += alPercentage.get(i);
}
if (fSum != 100) {
Log.e(TAG, ERROR_NOT_EQUAL_TO_100);
iDataSize = 0;
throw new Exception(ERROR_NOT_EQUAL_TO_100);
}
}
in your Layout:
<com.example.piecharts.PieChart
android:id="#+id/pieChart"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.example.piecharts.PieChart>
This question is pretty old, but I figured I would write a general answer.Here I assume you want to draw your pie chart in the middle of the canvas and that you have your start and seep angles in an array.
x = canvas.getWidth/2 //Horizontal center of canvas view
y = canvas.getHeight/2 //Vertical center of canvas view
canvas.rotate(fStartAngle[i]+ fSweepAngle[i]/2, x ,y ); //Rotates canvas to a line in the middle
//of start and end of arc
canvas.translate(50f,0);//Moves the text a little out of the center of the circle (50f is arbitrary)
paintText.setStyle(Paint.Style.FILL);
canvas.drawText(rotatedtext, x, y, paintText);
//Undo the translations and rotations so that next arc can be drawn normally
canvas.translate(-50f,0);
canvas.rotate(-(temp+ value_degree[i]/2), x ,y );
it's 2023 there might be other answers out there but here is one that is sure to work
//the path where your text/paint will be drawn across
Path path = new Path();
path.addArc(mEventsRect, fStartAngle, fSweepAngle);//add this if you want your path to be drawn across the arc of your sector
//if you are using a text get the width
float textWidth = mTextPaint.measureText("text");
//this is the y co-ordinate your text will start from
int hOffset = 100;
//this is the x co-ordinate your text will start from
int vOffset = 100;
//we will be using the matrix to rotate the bunds of our current path
Matrix matrix = new Matrix();
//we will use this to get the bounds of our current path
RectF bounds = new RectF();
path.computeBounds(bounds,true);
//we are using the matrix to rotate the bound (with is the bound of the path) by 90 degrees
matrix.setRotate(90,bounds.centerX(),bounds.centerY());
the we transform the points in the path using the matrix
path.transform(matrix);
//you can now draw the text on the path
canvas.drawTextOnPath("text", path, hOffset, vOffset , mBgPaints);

Categories

Resources