Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:12:50+00:00 2026-05-25T14:12:50+00:00

I am doing simple application that should put circle on Canvas when the user

  • 0

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

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:12:51+00:00Added an answer on May 25, 2026 at 2:12 pm

    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);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a simple check for some user data that get's put into session
I am creating a simple application with Django. I realized that I am doing
I am writing a fairly simple MVC3 application that allows a user to retrieve
I have a small application that uses a DrawingArea to draw a simple map
I am doing something that should be simple and getting stuck over and over
In My iphone Application,I am doing simple image animation in a View and in
hello i m doing a very simple Asp.net application project namespace WebApplication1 { public
I've been doing simple multi-threading in VB.NET for a while, and have just gotten
I have a WCF service hosted in IIS6. It is doing simple WebRequest. When
on my master page, I have referenced jquery file. I am doing simple hover

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.