Automatically moving an object

Hello,

I am writing a game where in Single Player mode, I need to move an object to simulate the computer's movement.
I have tried to move the Sprite directly (I am using CoCos2d Game Engine) but that loses the connectivity to the world (ie: it doesn't collide with anything in the world once I directly move the Object).

What is the best way to implement my Artificial Intelligence Logic? Is it in the Tick() method and by accessing the object's MouseJoint (I tried this but it crashes on Launch).

THank you all
Not sure about cocos, but in standard Box2D there are four primary ways to move a body:

  • Manually set its position (not generally a good idea)
  • Apply a Force
  • Apply an Impulse
  • Set the body's linear velocity (my preferred choice)

Manually setting the body's position can cause problems with collision detection (there would be nothing stopping you from positioning it inside another object for example)

Applying a force or impulse is like "pushing" the object. It will move in a direction for a short period, then will be subject to normal damping effects etc. If you continue to apply the force each step it will also accumulate. So your body will move faster and faster. This is good if you want to model something like a thruster on a space ship (eg Asteroids).

Probably the most common is to set linear velocity. This becomes and absolute value so your object will move at exactly this speed. Obviously it will still be affected by damping effects and gravity unless you cancel these by compensating with the linear velocity.

In my game I have only left or right movement, so I just set the linear velocity to the same value either right or left (x,0) or (-x,0), but any direction should work
Be aware that setting the velocity directly (instead of using force and drag to achieve same effect) could cause artifacts if two very bouncy objects are resting on each other and one begins to push the other.

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