Physics 2d object random x position

Hi, I want to create a train math game somthing like this game in below link
https://www.education.com/game/three-digit-subtraction-train/

when i use a random position for train its quickly move and disappear in preview
2023-08-01_17-56-24

I used this events.


thanks for your help

Without any movement actions, does the train appear in the correct position? Let’s look at the setup first. Are the joints working?

If that part isn’t working then can you post a pic of the add wheel joint settings. The joint settings don’t appear in the editor. So, can you post a pic of the its settings popup.

Do all of the objects have the physics behavior? If the train isn’t working when stationary then a pic of the physics behavior settings for the train and at least 1 wheel would help.

Let’s make sure the physics is setup first then we can talk about movement.

Physics usually uses forces not positions. Since the wheels are jointed then you’d only need to apply force to the train and the wheels will follow. Forcing everything to an immediate position could mess with the physics and joints especially using the same X() position for everything. Imagine trying to move all of the wheels and the body of a real train into the same x position. The physics behavior is going to push back. The wheels could collide and the joints could act weird as they try to return to their previous spacing.

Normally the camera would follow the train but the camera action needs to run on each frame and it’s currently in an event with a trigger once It would only move once. If you move the camera position action to it’s own line then you might be able to see where the train goes.

Let’s make sure everything is setup before talking about moving it.

T U Keith for helping
The camera problem solved as u said by centering on train with no condition.
Train dissappearing solved by create it and wheels in runtime using events istead of placing them on scene.
Now i want to move the train in x axis by some pixels . Which type of force i should use. thank u again for your help.

Your welcome. You can use any of the physics forces. Just make sure it’s the physics with physics symbol. You can apply force by amount, toward a direction or angle. I usually use the center point for apply point but it depends on the shape and intentions. You can also use the physics mass center x,y for the apply point value.

The wiki is a good source for info as well as the examples.

https://wiki.gdevelop.io/gdevelop5/behaviors/physics2/#movement

https://wiki.gdevelop.io/gdevelop5/all-features/physics2/reference/

Example using direction

Thanks.
Now Iapply a force toward a position as you can see in below image.
I want the train stop at the destination. sometimes its act correct and othe times it doent stop in destination. Can you help me how to stop it in any custom position.

I’m not at my pc to test this. I’m assuming you need to stop all of the parts of the train. Is everything still moving or just the wheels? The wheels would have momentum and the joint is slightly loose. I think there’s probably a little wiggle room.

If you create a group and add all of the train parts then you can set the entire group to static and then back to dynamic with the 1 action.

I set entire moving parts to static but the problem not solved.
Below images shows wheel and train physics setting. Is it possible the broblem relate to this settings?
seeting for wheel are the same, only rhe radius of WheelB is set to 24


Does it stop in the wrong place or not at all? Depending on the speed the train moves it can move several pixels between frames and jump right past the range. You can try increasing the range between > and < first. If you need accuracy that’s another issue

If the track is always going to be flat then you could skip the physics and use an X position tween. It would be easier and more exact. You would have to adjust the duration based on distance and then manually rotate or animate the wheels if you want them to spin.

Thanks again.
by increasing the range between > and < the problem solved.
now my problem is to how calculate amount of force to reach different distnations. does exist any way to calculate this force in gdevelop?

1 Like

Not that I’m aware of. There’s probably a formula somewhere. There are a lot of properties to factor in. I’ll ask again. Is the train track flat? A position X tween would be easier and more accurate.

Thanks so much. By using tween and rotation my problem solved.
Another question that I have : is it possible to have moving paralex background when the train itself moves by tween. Using x offset according to this fact that every time the train appear in random x position, doesnt act.

You’re welcome. It’s good to hear. I’m sure you can add a parallax background. It’s done either by moving cameras or the offset of a tiled sprite. I don’t have any expertise with it. It seems to be a popular subject.

50+ results for Parallax
https://forum.gdevelop.io/search?q=Parallax%20

If that doesn’t help. It would probably be best to start a new post. Later.