Phys2D question: mobile robot simulation

Hi,

I’ve got a question about Phys2D (or maybe more physics in general). I’m planning to use it to simulate movement of a mobile two-wheeled robot around a world with obstacles. The wheels are differential, i.e. the robot turns because of the different angular velocity of the wheels. On each timestep the robot determines what force/velocity to apply to each wheel.

The question is: I know how to calculate the robot’s next position and rotation from the velocities. However I imagine I can’t set the position and rotation directly since that will screw with the logic of the physics engine. So I imagine I need to do it by applying velocities/forces to the robot. Thing is…I don’t know how to since the robot is describing a circular path most of the time, and from what I saw about velocities the best I can do i s to send it in a straight line spinning around its own axis.

Anyone knows how to go about this, or if there perhaps is a way in which I can set the position and rotation directly and still have all the functionality of the engine?

Thanks

So I actually just tried to set the position and rotation directly (maybe I should do that before asking questions) and it seems it works: collisions are still being detected etc.

Now I have another question though: if the “robot” (just a circle) moves a distance equal to or greater than its own radius per timestep, it tunnels through other objects. I know this can be an issue for small and fast moving objects and that the solution is usually having either smaller timesteps or applying continuous collision detection. However I thought this happened only when the object moves either completely through the obstacle within one timestep or a distance that exceeds a certain threshold. I think in Phys2D this threshold is represented by the variable “allowedPenetration” of Arbiter, but even when I set this to 0 the problem keeps occurring. The object is actually penetrating the obstacle during 5 timesteps or so (seen by rendering a frame after each step). Shouldn’t a collision be detected in this case?

For wheels simulation, you can do as follow :

  • create a circle body to simulate the robot.
  • create two circle body to simulate the wheels (no mass, size doesn’t matter)
  • remove collision for wheels (collision mask to 0 other object must have a none 0 mask)
  • create fixed link to stick wheels to the body.

Now you have only to apply forces on the wheels.

For the collission problem, it may come from that you force the position.

Hm, I’ve never seen Phys2D have collision-related problems, so it might be because you are doing some forceful position changes and the like. In general, it’s a much better idea to adjust only the velocity of an object, rather than directly changing the velocity or rotation. If you want it to seem like an instantaneous change, you want to apply a lot of damping to the velocity, rather than just ignoring it.

And yeah looking up your problem is almost certainly that you’re simply setting the position. Phys2D can’t magically know what the object’s next position is going to be if you’re ignoring velocity.

Thanks for the suggestion. I played around a bit with it and have the following observations:

  1. when the wheels have zero mass, in fact when any part of the robot has zero mass, the rotation (and position as well I believe) values become NaN -> bad.
  2. setting the collision mask to anything (0, 1, another value) didn’t have any effect for me, how should I set this so that a body is ignored for collision?
  3. although your solution works (I adjusted the velocities of the wheels instead of the forces), it technically doesn’t make any difference with setting the position and rotation of the robot body directly. The robot will still go through obstacles just as happily, if it achieves a velocity such that the displacement of the body >= its radius within one timestep. This makes sense because actually at the end of a world step, Phys2D sets the position / rotation of bodies directly as well (based on calculation of forces etc, but the idea is the same). As far as I can tell collision detection is done purely based on the current position of a body: Phys2D doesn’t have continuous collision detection. So, setting the position directly shouldn’t make a difference for collision detection.

Coming back to my original question, it seems then that if a body has already penetrated “far enough” into another body within one timestep, Phys2D doesn’t regard it as a collision.

Huh, well that’s interesting. I thought for sure that I observed continuous collision detection within Phys2D.