0
\$\begingroup\$

A mathematician will probably think this was a silly question, but I'm not one and I'm in need of help :D

I'm working in Godot and with its RigidBody2Ds (objects that have automated physics behaviour).
I want to create a spring of type RigidBody2D (the 'springiness' is not the only function of this object).

I'm at the point in code where this spring and another RigidBody2D collide so I have all information about the two bodies at the moment of collision.

My goal is to make any body that touches the spring reach a certain height (from the spring), and move by certain speed horizontally, as you can see in the picture

So, the information about both bodies I have is: position, velocity, gravity scale, mass, weight.
What I want to achieve is constant line of movement of any object (that may have different properties I mentioned above) by applying a certain force to the colliding object.
I can stop the body before applying new force to it, that is, at the time of collision.

Rotation, friction, absorption and similar properties don't need to be counted on as the game I'm making is a simple one. With this I'm trying to make a predictable movement for the player to count on. I also plan to disable collision briefly at the time of collision so that the colliding object can touch the spring from any angle and the collision result would be the same.

EDIT: Here's some of my testing

Stones have random mass from 1 to 50, in this case 45.748413, 41.445225, 24.259401 and 37.189552 and all have same gravity scale (GS), that is: 2. The slime has GS of 5 and mass of 1.

I bothered to make a seamless gif, so you can only see the two jumps here. Because the spring has circular shape they fall apart after i think 10 bounces.

From this test I see that mass doesn't matter when I use @Sergio's method, so that's a step foreward. Thanks @Sergio!

func _on_body_entered(body):
    if body is RigidBody2D:
        body.set_linear_velocity(Vector2(0, -50 * body.gravity_scale))

Now, even though they don't reach the same height, they have same timing - from first touching the spring (yeah, i know, it doesn't look like a spring), to falling back. So that's also a step foreward, but I still don't have an idea how to get them all to get to the same height.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Try setting velocity of the object with set_linear_velocity(vel) on rigidbody, in that case object mass doesn't matter (unless you have strangely configured engine settings).

\$\endgroup\$
5
  • \$\begingroup\$ Hi Sergio. I tried that but it seems that the gravity scale still matters \$\endgroup\$
    – Monset
    Commented Jan 11, 2021 at 18:01
  • \$\begingroup\$ Do you mean that objects of different mass behave differently after calling set_linear_velocity? \$\endgroup\$
    – Sergio
    Commented Jan 11, 2021 at 18:34
  • \$\begingroup\$ Actually, they don't. You are half-correct with your answer. set_linear_velocity(vel) doesn't seem to care about mass. Tho, when I tried changing the gravity scale property, the object didn't bounce high enough as before. Thing is that I need both properties changed on some objects to get the right behaviour from them. \$\endgroup\$
    – Monset
    Commented Jan 11, 2021 at 18:37
  • \$\begingroup\$ Looking at your update I think you should set same gravity scale for all objects (this is how real world behaves), then just write body.set_linear_velocity(Vector2(0, -50)) and that should be enough. \$\endgroup\$
    – Sergio
    Commented Jan 12, 2021 at 10:55
  • \$\begingroup\$ Unfortunately I cannot do that, nor is that the question. It's easy to say 'just don't do that'. For my objects to behave more 'realistic', or maybe 'desireable for the game' is better, I have to add more downforce, and just adding mass isn't helping. Let's take the stone you can see in the gif above or example: I set it's gravity scale (GS) back to 1, and now it behaves more like a baloon, so I add more mass, and now I cannot use my player, slime, to push it as I could before, because of too mutch mass difference. So the balance between GS and mass is the key for desirable behaviour in game. \$\endgroup\$
    – Monset
    Commented Jan 12, 2021 at 11:46

You must log in to answer this question.

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