----------------------------------------------------------------------------------------------------------- Simulate a physics object for this frame
| 2423 | // ----------------------------------------------------------------------------------------------------------- |
| 2424 | // Simulate a physics object for this frame |
| 2425 | void do_vis_physics_sim(vis_effect *vis) { |
| 2426 | if (Frametime <= 0.0f) |
| 2427 | return; |
| 2428 | |
| 2429 | vector total_force; |
| 2430 | vector old_pos = vis->pos; |
| 2431 | |
| 2432 | #ifdef _DEBUG |
| 2433 | if (!Game_do_vis_sim) { |
| 2434 | return; |
| 2435 | } |
| 2436 | #endif |
| 2437 | |
| 2438 | if (vis->flags & VF_DEAD) { |
| 2439 | return; |
| 2440 | } |
| 2441 | |
| 2442 | if (fabsf(vis->velocity.x) < 0.000001f && fabsf(vis->velocity.y) < 0.000001f && fabsf(vis->velocity.z) < 0.000001f && |
| 2443 | !(vis->phys_flags & PF_GRAVITY)) { |
| 2444 | return; |
| 2445 | } |
| 2446 | |
| 2447 | // Determine velocity |
| 2448 | if (vis->phys_flags & PF_FIXED_VELOCITY) { |
| 2449 | vis->pos += vis->velocity * Frametime; |
| 2450 | } else { |
| 2451 | if (vis->mass > 0.0f && vis->drag > 0.0f) { |
| 2452 | // If the object is effected by gravity (normal, lighter than air, and anti-gravity) |
| 2453 | if (vis->phys_flags & PF_GRAVITY) { |
| 2454 | total_force.x = total_force.z = 0.0f; |
| 2455 | total_force.y = Gravity_strength * vis->mass; |
| 2456 | } else if (vis->phys_flags & PF_REVERSE_GRAVITY) { |
| 2457 | total_force.x = total_force.z = 0.0f; |
| 2458 | total_force.y = -Gravity_strength * vis->mass; |
| 2459 | } else { |
| 2460 | total_force.x = total_force.y = total_force.z = 0.0f; |
| 2461 | } |
| 2462 | |
| 2463 | // Note: The math here is done in 64bit floats because we will run into precision problems with 32bit |
| 2464 | const double forceOverDrag[3] = {total_force.x / vis->drag, total_force.y / vis->drag, total_force.z / vis->drag}; |
| 2465 | const double frameTime = Frametime; |
| 2466 | const double massOverDrag = vis->mass / vis->drag; |
| 2467 | const double dragOverMass = vis->drag / vis->mass; |
| 2468 | const double expDoMFt = exp(-dragOverMass * frameTime); |
| 2469 | |
| 2470 | double visNewPos[3]; |
| 2471 | visNewPos[0] = double(vis->pos.x) + forceOverDrag[0] * frameTime + |
| 2472 | massOverDrag * (double(vis->velocity.x) - forceOverDrag[0]) * (1.0 - expDoMFt); |
| 2473 | visNewPos[1] = double(vis->pos.y) + forceOverDrag[1] * frameTime + |
| 2474 | massOverDrag * (double(vis->velocity.y) - forceOverDrag[1]) * (1.0 - expDoMFt); |
| 2475 | visNewPos[2] = double(vis->pos.z) + forceOverDrag[2] * frameTime + |
| 2476 | massOverDrag * (double(vis->velocity.z) - forceOverDrag[2]) * (1.0 - expDoMFt); |
| 2477 | |
| 2478 | double visNewVel[3]; |
| 2479 | visNewVel[0] = (double(vis->velocity.x) - forceOverDrag[0]) * expDoMFt + forceOverDrag[0]; |
| 2480 | visNewVel[1] = (double(vis->velocity.y) - forceOverDrag[1]) * expDoMFt + forceOverDrag[1]; |
| 2481 | visNewVel[2] = (double(vis->velocity.z) - forceOverDrag[2]) * expDoMFt + forceOverDrag[2]; |
| 2482 |
no test coverage detected