Problem with circles and character moves

Hello,

I'm developing a game with Jbox2d.

In this game, i can have a character, which is moving with keyboard.

My character's shape is just a circle.

The problem is that, when my character collides with an other shape (on an other circle, i think this bug doesn't appears on polygons), and if this second circle is already moving, my characters "enters" on the other circle. I'm not sure of the word in english (superimposed ?), but there is the screenshot :

Image

To move my character, i use setLinearVelocity, maybe the problem is here ?
I tried with applyImpulse, but it doesn't moves my character.

Here is the code :

Code:
   /**
    * Moves left
    */
   public void goLeft() {
      this.moving = true;
      this.setLinearVelocityX(-USER_SPEED);
      this.sprite.startMoving(true);
   }

   /**
    * Moves right
    */
   public void goRight() {
      this.moving = false;
      this.setLinearVelocityX(USER_SPEED);
      this.sprite.startMoving(false);
   }

   /**
    * @param value the new linear velocity X
    */
   private synchronized void setLinearVelocityX(float value) {
      if(this.body.isSleeping())
         this.body.wakeUp();
      this.body.setLinearVelocity(new Vec2(value, this.body.m_linearVelocity.y));
      //this.body.m_linearVelocity.x = value;
   }


Can you help me resolving my problem ?

Thanks.

EDIT : Sorry for big image 😳
up
try applying forces instead of setting the linear velocity. The engine modifies the linear velocity when it collides objects, so by changing that you mess up collision. Let me know if you need help getting the forces correct.
Thank you for answering.

I tried to replace my code in setLinearVelocityX function by this code :

Code:
if(Math.abs(this.body.m_linearVelocity.x) < Math.abs(value)) {
   this.body.applyForce(new Vec2(value * 1000, 0), new Vec2(0, 0));
}


and by this :

Code:
while(Math.abs(this.body.m_linearVelocity.x) < Math.abs(value))
   this.body.applyForce(new Vec2(value * 1000, 0), new Vec2(0, 0));


On the first code my character doesn't starts with the required speed. There is a very long acceleration.

On the second code my character has the good start speed, but when he starts to move, he seems to be "teleported".

With these codes my character can "fly" when he jumps. If i press right-left-right-left... after a jump, the gravity seems to be broken, and my character doesn't falls.
Alrighty, this might give you smoother behavior (this is my static steer class, take what you want from it). Based off of OpenSteer (C++ ai steering test engine).

http://pastebin.com/dF2yDJME

I think the part you're looking for is the last method, steerForTargetSpeed. Let me know if you want the Obstacle or Pathway sources.
Thank you, but I don't know how to adapt this method for jbox2D.

I don't have any IPhysicsObject class, and what to to with the Vec2 that this function returns ?
sorry, it's from my engine, and it's a bit coupled. Here is what you might use:
Code:
/**
    * returns a force that's constrained to the object's max force to reach the target speed
    * @param argMe
    * @param targetSpeed
    * @return
    */
   public static Vec2 steerForTargetSpeed(Vec2 argDirection, Vec2 argCurrLinearVel, float argMaxForce, final float targetSpeed){
      final float speedError = targetSpeed - argCurrLinearVel.length();
      return argDirection.mul( MathHelper.constrain(speedError, -mf, mf));
   }
mf doesn't exists and argMaxForce is unused. Maybe it's the same thing ?

What are argMaxForce and argDirection ?

And what must i do with the returned Vec2 ?

And I don't have MathHelper. Must I Download it ?
up
up !

Original thread: https://box2d.org/forum/viewtopic.php?t=5385