Box2D linearVelocity problem

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Re: Box2D linearVelocity problem

Postby mzechner » Thu May 19, 2011 11:24 pm

Sorry, i overlooked this thread. In actuality i'm afraid i don't have a lot to add really :/ T4ils tip sounds good to me.
mzechner
Site Admin
 
Posts: 4709
Joined: Sat Jul 10, 2010 3:50 pm

Re: Box2D linearVelocity problem

Postby wizlon » Fri May 20, 2011 10:01 am

Excellent T4ils, It's always nice to know how to use Box2D better. Atm I'm just using it as a collision and gravity simulator but at some point using it for actual object physics might be useful so amassing this knowledge is always useful.

Thanks for the help.
If I've helped you out in any way then please check out my game Critter Rollers, download and rate. It's free (and quite good).
wizlon
 
Posts: 219
Joined: Wed Jan 19, 2011 12:13 pm

Re: Box2D linearVelocity problem

Postby markww » Wed Sep 21, 2011 6:26 pm

Hi wizlon, did you resolve this problem?

I'm running into the same issue. I position a circle body at a set x,y location (it's a bullet), and I apply a set x,y linear velocity to it. Most times the path of the bullet is perfectly reproducable. Once in awhile it seems like a slightly different force is applied, and the bullet's path is not the same as all previous firings. I'm printing out the linear impule, world points, etc, they're all the same between firings:

Code: Select all
    @Override
    public void render() {
        world.step(Gdx.app.getGraphics().getDeltaTime(), 3, 3);
        ...
    }

    // called when the user presses a "fire" button.
    private void fireCannon() {
        Body bullet = ...;
        bullet.setActive(true);
        bullet.setTransform(CONST_LOCATION_X, CONST_LOCATION_Y, 0);
        bullet.setLinearVelocity(0, 0);
        bullet.applyLinearImpulse(
          10, // impulse x
          20, // impulse y
          CONST_LOCATION_X,
          CONST_LOCATION_Y);
    }


Also, there are no bodies that the bullet could collide with when it is initially fired. I don't have a cannon body or anything like that (my game is pretty bare-bones right now!). The only bodies are the floor, ceiling, left and right walls, and a single 1x1 static box body floating in space.

Thanks
markww
 
Posts: 23
Joined: Mon Sep 05, 2011 4:36 pm

Re: Box2D linearVelocity problem

Postby wizlon » Wed Sep 21, 2011 8:17 pm

I'm not sure if I can remember now acctually, it was a long while back I sorted this and my memory is terrible anyway.

One thing I will say is that it is a good idea (kind of necessary actually) to have your world.step() in a delta trap to stop any sluggish behavoiur affecting your physics, who knows that could be the source of your problem. I use Mario's excellect code as such.

Code: Select all
accumulator += delta;
          while (accumulator >= stepTime)
          {
             step(stepTime);
             accumulator -= stepTime;
          }


Give that a try.
If I've helped you out in any way then please check out my game Critter Rollers, download and rate. It's free (and quite good).
wizlon
 
Posts: 219
Joined: Wed Jan 19, 2011 12:13 pm

Re: Box2D linearVelocity problem

Postby markww » Wed Sep 21, 2011 8:32 pm

Hey wizlon, thanks for the info on the delta step, will try that too. I am recycling the bullet bodies, and saw that they still had some values left over from their last shot (I was only setting active = false, etc). Now they seem to be behaving properly. I think this was the problem (dumb mistake by me).

Did you release your cannon shooter game? I looked for it in the marketplace but couldn't find it. It looks awesome!

Thanks
markww
 
Posts: 23
Joined: Mon Sep 05, 2011 4:36 pm

Re: Box2D linearVelocity problem

Postby wizlon » Thu Sep 22, 2011 8:04 am

I am literally putting the final touches to it, getting all the levels created and prepared took ages, I should be releasing it the next couple of days so keep an eye out on the showcase board.
If I've helped you out in any way then please check out my game Critter Rollers, download and rate. It's free (and quite good).
wizlon
 
Posts: 219
Joined: Wed Jan 19, 2011 12:13 pm

Re: Box2D linearVelocity problem

Postby markww » Thu Sep 22, 2011 1:56 pm

That's awesome, congrats, looks like it'll be a really fun app.
markww
 
Posts: 23
Joined: Mon Sep 05, 2011 4:36 pm

Previous

Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest