0
\$\begingroup\$

I am trying to implement a space rocket in 2D. Whenever the player presses a button, a constant force (Both vertical and horizontal) is being added to that rocket. What I want to add is a maximum speed, the rocket cant exceed as the rocket will continue accelerating to infinity. The player can also choose not to accelerate, which can lead to the rocket starting to fall down due to gravity. Thus, I cant just Clamp the rigidbody.velocity of the rocket whenever the magnitude of it exceeds max speed as it will also limit the fall velocity which is not as limited. Thus, I am trying to predict whether the player will exceed the maximum velocity when trying to accelerate, in order not to add the force.

So far, I have tried doing this but the velocity vector does not seem to change immediately, so vel is equal to postVel:

Vector2 vel = rigidbody.velocity;
rigidbody.AddRelativeForce(Force); // Add force
Vector2 postVel = rigidbody.velocity;
rigidbody.AddRelativeForce(-Force); // Undo force
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Note that you might get more helpful answers if you would tell us what you want to accomplish by doing this. \$\endgroup\$ – Philipp Feb 3 '20 at 17:00
0
\$\begingroup\$

Remmber: F=MA

When you add force you are modifying the acceleration, which will in turn change velocity as time progresses.

You have added and subtracted force in the same instant, without allowing time for the newly changed rate of acceleration to modify the velocity. Add some brief sleeps in your code and you will see the change in velocity.

\$\endgroup\$
4
  • \$\begingroup\$ The brief sleeps won't work because the prediction has to be instant \$\endgroup\$ – Panagiotis Iatrou Feb 3 '20 at 19:15
  • \$\begingroup\$ I think you are fundamentally misunderstanding the basis of real world physics which the game engine is attempting to simulate. Regardless of the magnitude of the change in force, there will be no change in velocity until there has been a change in time. \$\endgroup\$ – disc_code22 Feb 3 '20 at 19:17
  • 1
    \$\begingroup\$ @PanagiotisIatrou I think this may be an X/Y problem. If you provide some more specifics about your situation and what you are trying to accomplish I think someone will be able to provide a more specific answer. It is hard to understand what exactly the inputs and outputs you are looking for are, and what your current implementation is. \$\endgroup\$ – disc_code22 Feb 3 '20 at 19:22
  • \$\begingroup\$ I edited the question body \$\endgroup\$ – Panagiotis Iatrou Feb 3 '20 at 19:35
0
\$\begingroup\$

Adding forces to a rigidbody won't change its velocity until the phyiscs engine runs another update, which happens betweeen two calls to a FixedUpdate method in your own code.

But if you want to predict the velocity of a body after applying a force, you can do so using Newton's formula force = mass * acceleration, or acceleration = force / mass.

Vector3 newVelocity = rigidbody.velocity + Force / rigidbody.mass;

But keep in mind that drag, other forces and collisions with other bodies might cause the velocity on the next frame to be different than you predicted.

\$\endgroup\$
1
  • \$\begingroup\$ if force / rigidbody.mass is equal to acceleration then why do we add that to the new velocity? Shouldn't we add a velocity, instead of acceleration? This piece of code is on FixedUpdate(): Vector2 vel = rb.velocity; Vector2 newVel = vel + (force / rb.mass); Debug.Log(vel.magnitude + " vs " + newVel.magnitude); rb.AddRelativeForce(force); And it prints this which is not the result wanted. What I want is to predict the next "vel" \$\endgroup\$ – Panagiotis Iatrou Feb 3 '20 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.