coeng
diff src/co_phys.cc @ 6:2f872a179914
first component test:
- prs, xform, physics components with dependencies
- topological sort of components to update them in the correct order
- debug visualization component
todo: remove draw() from components, doesn't make sense
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 14 Feb 2015 07:27:12 +0200 |
parents | 0e5da17d589c |
children | 8cce82794f90 |
line diff
1.1 --- a/src/co_phys.cc Sat Feb 14 01:35:42 2015 +0200 1.2 +++ b/src/co_phys.cc Sat Feb 14 07:27:12 2015 +0200 1.3 @@ -1,4 +1,6 @@ 1.4 +#include <assert.h> 1.5 #include "co_phys.h" 1.6 +#include "sim.h" 1.7 1.8 static CoRigid reg_co_rigid; 1.9 1.10 @@ -15,6 +17,42 @@ 1.11 register_component(name, cons_rigid); 1.12 } 1.13 1.14 -void CoRigid::update() 1.15 +const char **CoRigid::update_before() const 1.16 { 1.17 + static const char *before[] = { "prs", 0 }; 1.18 + return before; 1.19 } 1.20 + 1.21 +void CoRigid::add_impulse(const Vector3 &v) 1.22 +{ 1.23 + impulse += v; 1.24 +} 1.25 + 1.26 +void CoRigid::update(float dt) 1.27 +{ 1.28 + if(!gobj) return; 1.29 + 1.30 + if(!co_prs) { 1.31 + if(!(co_prs = COCAST(CoPRS, gobj->get_component("prs")))) { 1.32 + assert(co_prs); 1.33 + return; 1.34 + } 1.35 + } 1.36 + 1.37 + float damping = world ? world->get_damping() : 0.005; 1.38 + 1.39 + Vector3 newpos = co_prs->pos + vel * dt; 1.40 + 1.41 + Vector3 accel = impulse; 1.42 + impulse.x = impulse.y = impulse.z = 0.0f; // reset impulse 1.43 + 1.44 + if(world) { 1.45 + accel += world->get_gravity(); 1.46 + } 1.47 + 1.48 + vel = (vel - vel * damping * dt) + accel * dt; 1.49 + 1.50 + // TODO collisions 1.51 + 1.52 + co_prs->pos = newpos; 1.53 +}