1
\$\begingroup\$

I am creating a game using Box2D (Javascript implementation) - and I added the ability to have a static platform, that is moved along an axis as a function of a sine.

My problem is when the player lands on the platform, as the platform moves along the X axis - the player is not moved along with it, as you visually would expect.

The player can land on the object, and if it hits the side of the object, it does colide with it and is pushed.

This image might explain better than I did:
After jumping on to the red platform the player character will fall off as the platform moves to the right

example

UPDATE:
Here is a live demo showing the problem:
http://onedayitwillmake.com/ChuClone/slideexample.php

\$\endgroup\$
10
  • 3
    \$\begingroup\$ Do both surfaces have some amount of friction? \$\endgroup\$ Commented Jul 22, 2011 at 3:07
  • \$\begingroup\$ How do you move the platform? Is there any chance to use a joint? \$\endgroup\$
    – Den
    Commented Jul 22, 2011 at 8:13
  • \$\begingroup\$ Well that is kind of the normal physical behavior. When the player jumps, give him also an impulse into the direction, where the platform moves to. Don't let him just fall onto the platform. \$\endgroup\$ Commented Jul 22, 2011 at 9:20
  • \$\begingroup\$ If you jump from a bridge and land on a moving truck, the same will happen. Don't try it at home :p \$\endgroup\$ Commented Jul 22, 2011 at 9:22
  • 1
    \$\begingroup\$ @Joe: Thanks for the clarification! I do still worry about whether being designated as 'static' is what's causing its movement not to be working as expected. \$\endgroup\$ Commented Jul 22, 2011 at 11:03

2 Answers 2

5
\$\begingroup\$

I move the platform by calling SetPosition on the body

I think this is the problem. Box2D is not designed to be used this way. Is there any way you can simulate sine function movement by applying impulses/forces?

\$\endgroup\$
4
  • \$\begingroup\$ I've also tried using b2_KinematicBody instead, which as I understand it are made to be used that way, but the results were the same :( \$\endgroup\$ Commented Jul 22, 2011 at 16:14
  • \$\begingroup\$ Is creating a joint dynamically (every time you land on platform) an option? \$\endgroup\$
    – Den
    Commented Jul 22, 2011 at 16:17
  • 2
    \$\begingroup\$ Reason why this doesn't work: SetPosition is like teleporting something. It's not going to bring anything with it: it won't move anything along with it because it's not moving, it won't apply any force on anything because it has no force being exerted on it to transfer - it's just going to flick out of existence here and back into existence there. \$\endgroup\$ Commented Jul 22, 2011 at 23:31
  • \$\begingroup\$ @Jonathan - Yeah it's one of those things that makes sense / sounds obvious now that I know about it. Thanks for the good info :) \$\endgroup\$ Commented Jul 22, 2011 at 23:51
3
\$\begingroup\$

Solved!

For future people: The solution was to use b2_Kinematic bodies. Do not move it by calling SetPosition on the platform.

Call SetLinearVelocity, and then any body's "on top" will interact as you would expect.

Giving the answer credit to Den since he provided information that led me in the right direction.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .