Moving bodies/platforms

This has probably been covered a number of times already, but I can't seem to find much info relating to it through the search bar, so I'm just gonna go ahead and ask. What is the general practice for moving/floating platforms and the like?

I've been trying to create a solution just by taking a rectangle and moving it back and forth, but finding a solution that allows objects on top of the platform, such as the player dude, to move along with and not tunnel through is eluding me.

Right now I've just got the platform setting it's linear velocity every frame,

Code:
body.SetLinearVelocity(new b2Vec2(2, 0));


but the platform continues to fall a tiny bit each frame due to gravity and when my player dude jumps on it he'll often just slip through like it's made of jello.

Any suggestions on how to make a moving/floating platform more stable, keeping it's y position static and holding things up like it should?
Simple answer: Mouse Joints.
i would like to see a example, both in running swf, and in code form.
if anyone has the time.

very interesting.
So, is your suggestion to create a mouse joint, attach the platform body to it and just drag the joint around? Would I have to lock the rotation of the platform to keep it stable and such?
Yes, that's exactly it. To lock rotation you can set the body not to rotate. To animate rotation you could use a second mouse joint.

If someone gets a working example setting the position and velocity directly, I would be VERY interested. My own attempts have failed miserably.
Well, let me weigh in with a couple of suggestions.

1. I'm not sure I'd use a mouse joint. I'd use a prismatic joint, which is probably better suited to your task -- it basically restricts the motion of an object to move only along a particular line. You can set the maximum ranges of your joint, and then you can attach a motor to it to get your platform to move back-and-forth along the axis of your joint.

2. I've had success using the SetVelocity technique, but you can't just set the Y velocity to zero, which I know makes the most sense, but actually causes the "slowly falling" behavior you've described. I believe this is because sometime after you've set your velocity, box2d is applying gravity, which slightly alters the velocity you've just set. Instead, you kinda have to take your body's position, figure out where you want it to go in the next frame, and then apply a velocity based on the difference between where you are and where you want to be. In practice, this ends up giving you a velocity that's something like (2, -0.01824).

3. I'm actually not sure about the "Going through it like it's jello" phenomenon. That might be related to some other weirdness. You're not also setting the position in addition to setting the velocity or anything like that, are you?
i believe i have seen the jello effect when i was drawing the points in the wrong order for making a convex polygon

in my case i was using rectangles, and the SetAsBox functionality.
but the rectangle's width was inverse to the height, as shown below (not sure if "inverse" is the right word)
+height, +width = good
-height, +width = bad
+height, - width = bad
-height, -width = good

as soon as i suspected it i logically just forced all widths and heights to be positive.

and realize when i draw my own custom polygons, i will have to pay close attention to draw them in the right order.
I use following code and everything works perfectly 😎

Code:
private var movePlatformBody:b2Body;
private var movePlatformBodyDirection:int; //direction of moving

public function Level_1() {
    ...
    movePlatformBody = CreateBody("movePlatform","Polygon",defaultDensity,defaultFriction,defaultRestitution,[[[40, -5], [40, 5], [-40, 5], [-40, -5]]]); //my own creation function
    var prismaticJointDef:b2PrismaticJointDef=new b2PrismaticJointDef();
    prismaticJointDef.Initialize(movePlatformBody, m_ground, movePlatformBody.GetWorldCenter(), new b2Vec2(1,0));
    prismaticJointDef.enableLimit = false;
    prismaticJointDef.enableMotor = false;
    movePlatformBodyDirection=1;
    m_world.CreateJoint(prismaticJointDef); //I fix one body axe. In this case it is horizontal platform. For vertical platforms use new b2Vec2(0,1) in prismaticJointDef.Initialize
...
}

public override function Update(e:Event):void {
    super.Update(e);

    if(movePlatform.x<390) movePlatformBodyDirection=1;
    if(movePlatform.x>550) movePlatformBodyDirection=-1; //the platform will move form x=390 to x=550
         
    movePlatformBody.SetLinearVelocity(new b2Vec2(2*movePlatformBodyDirection,0));
}


I tried to use MouseJoints, but my variant is more simply and quick.
Thanks maklaus, worked 100% perfect, that´s exactly what I was looking for 🙂
Obviously old posts are still being looked at, so for anyone else who arrives here... 😉

In the years between the two preceding posts in this thread, kinematic bodies were added which help to cater to cases like this, you might wanna check them out too.

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