MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / PhysicsApplyConstantForce

Function PhysicsApplyConstantForce

physics/physics.cpp:258–291  ·  view source on GitHub ↗

Applies a linear force over time. --Like gravity or thrust

Source from the content-addressed store, hash-verified

256
257// Applies a linear force over time. --Like gravity or thrust
258void PhysicsApplyConstantForce(const object &objp, vector &newPos, vector &newVel, vector &movementVec,
259 const vector &force, float deltaTime) {
260 const vector &pos = objp.pos;
261 const vector &vel = objp.mtype.phys_info.velocity;
262 const double drag = static_cast<double>(objp.mtype.phys_info.drag);
263 const double mass = static_cast<double>(objp.mtype.phys_info.mass);
264
265 if (mass < std::numeric_limits<double>::epsilon() || drag < std::numeric_limits<double>::epsilon()) {
266 // No Mass/Drag
267 movementVec = (vel * deltaTime) + (force * (0.5f * deltaTime * deltaTime));
268 newPos = pos + movementVec;
269 newVel = vel + force * deltaTime;
270 return;
271 }
272
273 // Standard motion with a linear air drag (drag is proportional to velocity)
274 const double dt = static_cast<double>(deltaTime);
275 const double oneOverDrag = 1.0 / drag;
276 const double massOverDrag = mass / drag;
277 const double forceOverDrag[3] = {force.x * oneOverDrag, force.y * oneOverDrag, force.z * oneOverDrag};
278 const double objVel[3] = {static_cast<double>(vel.x), static_cast<double>(vel.y), static_cast<double>(vel.z)};
279 const double expDoMDt = exp((-1.0 / massOverDrag) * dt);
280
281 newPos.x = static_cast<float>(static_cast<double>(pos.x) + forceOverDrag[0] * dt +
282 massOverDrag * (objVel[0] - forceOverDrag[0]) * (1.0 - expDoMDt));
283 newPos.y = static_cast<float>(static_cast<double>(pos.y) + forceOverDrag[1] * dt +
284 massOverDrag * (objVel[1] - forceOverDrag[1]) * (1.0 - expDoMDt));
285 newPos.z = static_cast<float>(static_cast<double>(pos.z) + forceOverDrag[2] * dt +
286 massOverDrag * (objVel[2] - forceOverDrag[2]) * (1.0 - expDoMDt));
287 movementVec = newPos - pos;
288 newVel.x = static_cast<float>((objVel[0] - forceOverDrag[0]) * expDoMDt + forceOverDrag[0]);
289 newVel.y = static_cast<float>((objVel[1] - forceOverDrag[1]) * expDoMDt + forceOverDrag[1]);
290 newVel.z = static_cast<float>((objVel[2] - forceOverDrag[2]) * expDoMDt + forceOverDrag[2]);
291}
292
293// Banks an object as it turns (we counteract this and then reapply it when we
294// actually do the turn -- cool)

Callers 1

PhysicsDoSimLinearFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected