0
\$\begingroup\$

I working on a game project using C++ with SMFL and Box2d. I'm basically recreating the game "Jump and Hover", a simple platform game.

In the game ther is an platform thats "hovering" in the air and the player can move it left and right by collideing with it. The player can stand on the platform without make it going down and the player can hit in from below without makeing it going up. So basicly its fixed on the y axes and can only move left/right.

My first idea of implementing this was to set the velocity.y to 0 every round in my general update function that being called every step of the world.

Unfortunately it didnt work. its made the movment up/down very slow but didnt prevent that.

My second idea was to set the gravity scale of the player to zero when he hit the platform from above = stands on it. That worked almost perfectly but when im jumpging on the platform after couple jumps its goes a little bit down. Plus it also still goes up when hitting it from below.

Im out of ideas so Im asking for help.

\$\endgroup\$
1
  • \$\begingroup\$ Have you tried setting the position.y to 0 (or whatever y you want) each frame? Each engine iteration might start moving it a bit as well as setting its speed. \$\endgroup\$
    – Sacha
    Commented Jun 4, 2022 at 18:03

1 Answer 1

0
\$\begingroup\$

Set the body type of the platform to kinematic, and this is also a general introduction.

Bodies have position and velocity. You can apply forces, torques, and impulses to bodies. Bodies can be static, kinematic, or dynamic. Here are the body type definitions:
...
b2_kinematicBody
A kinematic body moves under simulation according to its velocity. Kinematic bodies do not respond to forces. They can be moved manually by the user, but normally a kinematic body is moved by setting its velocity. A kinematic body behaves as if it has infinite mass, however, Box2D stores zero for the mass and the inverse mass. Kinematic bodies do not collide with other kinematic or static bodies.
...

In simple terms, this will make your rigidbody similar to have infinite mass, other than the speed set by the code, other physics interactions will not affect its speed. Here is a similar example.

\$\endgroup\$
5
  • \$\begingroup\$ Ye I read about that, but becuas its says that Kinematic bodies do not respond to forces I thought it would not work for me, becaus I need it (the platform) to responde to a force applied by the players body. Edit: \$\endgroup\$ Commented Jun 4, 2022 at 18:49
  • \$\begingroup\$ I tried that now and it doesnt work - the platform doesnt move at all. \$\endgroup\$ Commented Jun 4, 2022 at 18:56
  • \$\begingroup\$ Plus kinematic bodys doest collide with static bodys = go trow my map's walls. \$\endgroup\$ Commented Jun 4, 2022 at 19:24
  • \$\begingroup\$ Maybe the platforms can respond to the player's behavior in a non-physical way. You can add triggers in four directions to change the movement behavior of the platform. \$\endgroup\$
    – Mangata
    Commented Jun 4, 2022 at 20:11
  • \$\begingroup\$ Plan B: you can use prismatic joint to set a horizontal track. Maybe this is more suitable for your needs. joints-prismatic \$\endgroup\$
    – Mangata
    Commented Jun 4, 2022 at 21:39

You must log in to answer this question.

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