Move one dynamic body relative to another dynamic body

I ran into some kind of problem that I cannot find a solution to. I have a swinging platform (body1) and a hero (body2) standing on it. The platform waves so that the hero moves to the right and left on the platform.

In the beginning, I want the hero to simply stand on the platform without changing its position relative to the platform. I achieved this by adding SKPhysicsJointFixed joining the platform and the hero.

Then at some point in the game I want the hero to run along the platform, which means that Y must remain the same relative to the platform, and X must grow. The only solution I found was to make the hero’s body static and change its position in didSimulatePhysics . The problem with this approach is that the situation does not change exactly. The hero’s position is behind one frame compared to the platform’s position.

After the hero reaches the right end of the platform, he must stop moving and stand on the platform again. Therefore, I make the hero motionless and again add a fixed joint. And since the position of the hero changes with a delay, the position of the hero relative to the platform is incorrect. Sometimes it stops some pixels below the surface of the platform, and sometimes some pixels above the surface.

So, is there any other approach to moving the hero along the platform, the body of the hero must remain dynamic all the time.

+1
source share
1 answer

I have a game with moving floating platforms. The simplest and most dynamic approach to achieving the correct relative speed is simply to make adjustments to the speed. Here is a simplified version of what I'm doing to achieve this.

In my game loop, I have the following:

 if heroOnBridge { hero.extraMotion = floatingBridge.physicsBody!.velocity } else { hero.extraMotion = CGVector(dx:0,dy:0) } 

Here, what I am doing is constantly looking to see if my hero is on a moving bridge, if then I set the hero’s “extra movement” property to the speed of the bridge I'm on. If I'm not on the bridge, then I don't add any extra speed. Please note that I am not showing how the value of heroOnBridge is calculated, because it really depends on how you determine if you are on the platform. In my case, I assume that you are on the platform if you cross the platform frame and are on the ground.

Then, in the area where I calculate the speed of my hero depending on his direction (left, right, jumps, etc.), I simply add extraMotion to the speed of the hero.

If you don't already know this, you have an area in the game loop to handle the movement of your characters. This will allow you to make changes to the speed of your hero depending on certain factors (in the air, jumping, moving left / right, on the platform, etc.).

If you need further help calculating part of the speed, let me know and I will send more of my code. You can also read my answer here for a smooth and dynamic speed reference.

Remember that for the most part you never want to directly set the position of something, always try to set the necessary speed to reach this position over a period of time. This prevents a very unstable and unpredictable movement.

+2
source

Source: https://habr.com/ru/post/1267164/


All Articles