Skip to main content
added markdown to code in text
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

Have a look at semi-implicit Euler integration - there's an example at the end for an 1D spring. This is what you're basically doing. Also, you have no need for working with magnitudes and normalized directions. Just add the applied forces times the delta time to the velocity. That's all you need to do.

Now, to answer your question, you need a force accumulator. See this course for more details. It could be m_forceAppliedm_forceApplied in your case. And every time you call AddForceAddForce you just add force to it. And in CalculateVelocityCalculateVelocity you just do m_velocity += m_forceApplied * deltaTime / m_massm_velocity += m_forceApplied * deltaTime / m_mass. And you can add gravity using AddForceAddForce and the m * g formulam * g formula as Sascha was saying. Or you can hardcode it directly: m_velocity += (m_forceApplied / m_mass + gravity) * deltaTimem_velocity += (m_forceApplied / m_mass + gravity) * deltaTime.

Update: I corrected myself, I realized I used the mass wrong in the integration, it was not adding up.

Have a look at semi-implicit Euler integration - there's an example at the end for an 1D spring. This is what you're basically doing. Also, you have no need for working with magnitudes and normalized directions. Just add the applied forces times the delta time to the velocity. That's all you need to do.

Now, to answer your question, you need a force accumulator. See this course for more details. It could be m_forceApplied in your case. And every time you call AddForce you just add force to it. And in CalculateVelocity you just do m_velocity += m_forceApplied * deltaTime / m_mass. And you can add gravity using AddForce and the m * g formula as Sascha was saying. Or you can hardcode it directly: m_velocity += (m_forceApplied / m_mass + gravity) * deltaTime.

Update: I corrected myself, I realized I used the mass wrong in the integration, it was not adding up.

Have a look at semi-implicit Euler integration - there's an example at the end for an 1D spring. This is what you're basically doing. Also, you have no need for working with magnitudes and normalized directions. Just add the applied forces times the delta time to the velocity. That's all you need to do.

Now, to answer your question, you need a force accumulator. See this course for more details. It could be m_forceApplied in your case. And every time you call AddForce you just add force to it. And in CalculateVelocity you just do m_velocity += m_forceApplied * deltaTime / m_mass. And you can add gravity using AddForce and the m * g formula as Sascha was saying. Or you can hardcode it directly: m_velocity += (m_forceApplied / m_mass + gravity) * deltaTime.

Update: I corrected myself, I realized I used the mass wrong in the integration, it was not adding up.

added 115 characters in body
Source Link

Have a look at semi-implicit Euler integration - there's an example at the end for an 1D spring. This is what you're basically doing. Also, you have no need for working with magnitudes and normalized directions. Just add the applied forces times the delta time to the velocity. That's all you need to do.

Now, to answer your question, you need a force accumulator. See this course for more details. It could be m_forceApplied in your case. And every time you call AddForce you just add force to it. And in CalculateVelocity you just do m_velocity += m_forceApplied * deltaTime / m_mass. And you can add gravity using AddForce and the m * g formula as Sascha was saying. Or you can hardcode it directly: m_velocity += (m_forceApplied +/ m_mass *+ gravity) * deltaTime.

Update: I corrected myself, I realized I used the mass wrong in the integration, it was not adding up.

Have a look at semi-implicit Euler integration - there's an example at the end for an 1D spring. This is what you're basically doing. Also, you have no need for working with magnitudes and normalized directions. Just add the applied forces times the delta time to the velocity. That's all you need to do.

Now, to answer your question, you need a force accumulator. See this course for more details. It could be m_forceApplied in your case. And every time you call AddForce you just add force to it. And in CalculateVelocity you just do m_velocity += m_forceApplied * deltaTime. And you can add gravity using AddForce and the m * g formula as Sascha was saying. Or you can hardcode it directly: m_velocity += (m_forceApplied + m_mass * gravity) * deltaTime.

Have a look at semi-implicit Euler integration - there's an example at the end for an 1D spring. This is what you're basically doing. Also, you have no need for working with magnitudes and normalized directions. Just add the applied forces times the delta time to the velocity. That's all you need to do.

Now, to answer your question, you need a force accumulator. See this course for more details. It could be m_forceApplied in your case. And every time you call AddForce you just add force to it. And in CalculateVelocity you just do m_velocity += m_forceApplied * deltaTime / m_mass. And you can add gravity using AddForce and the m * g formula as Sascha was saying. Or you can hardcode it directly: m_velocity += (m_forceApplied / m_mass + gravity) * deltaTime.

Update: I corrected myself, I realized I used the mass wrong in the integration, it was not adding up.

Source Link

Have a look at semi-implicit Euler integration - there's an example at the end for an 1D spring. This is what you're basically doing. Also, you have no need for working with magnitudes and normalized directions. Just add the applied forces times the delta time to the velocity. That's all you need to do.

Now, to answer your question, you need a force accumulator. See this course for more details. It could be m_forceApplied in your case. And every time you call AddForce you just add force to it. And in CalculateVelocity you just do m_velocity += m_forceApplied * deltaTime. And you can add gravity using AddForce and the m * g formula as Sascha was saying. Or you can hardcode it directly: m_velocity += (m_forceApplied + m_mass * gravity) * deltaTime.