coeng
annotate 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 |
rev | line source |
---|---|
nuclear@6 | 1 #include <assert.h> |
nuclear@5 | 2 #include "co_phys.h" |
nuclear@6 | 3 #include "sim.h" |
nuclear@3 | 4 |
nuclear@5 | 5 static CoRigid reg_co_rigid; |
nuclear@3 | 6 |
nuclear@5 | 7 static Component *cons_rigid() { return new CoRigid; } |
nuclear@3 | 8 |
nuclear@5 | 9 CoRigid::CoRigid() |
nuclear@3 | 10 { |
nuclear@3 | 11 mass = 1.0; |
nuclear@3 | 12 elast = 0.5; |
nuclear@3 | 13 friction = 0.0; |
nuclear@3 | 14 |
nuclear@3 | 15 name = "rigid"; |
nuclear@3 | 16 |
nuclear@3 | 17 register_component(name, cons_rigid); |
nuclear@3 | 18 } |
nuclear@3 | 19 |
nuclear@6 | 20 const char **CoRigid::update_before() const |
nuclear@3 | 21 { |
nuclear@6 | 22 static const char *before[] = { "prs", 0 }; |
nuclear@6 | 23 return before; |
nuclear@3 | 24 } |
nuclear@6 | 25 |
nuclear@6 | 26 void CoRigid::add_impulse(const Vector3 &v) |
nuclear@6 | 27 { |
nuclear@6 | 28 impulse += v; |
nuclear@6 | 29 } |
nuclear@6 | 30 |
nuclear@6 | 31 void CoRigid::update(float dt) |
nuclear@6 | 32 { |
nuclear@6 | 33 if(!gobj) return; |
nuclear@6 | 34 |
nuclear@6 | 35 if(!co_prs) { |
nuclear@6 | 36 if(!(co_prs = COCAST(CoPRS, gobj->get_component("prs")))) { |
nuclear@6 | 37 assert(co_prs); |
nuclear@6 | 38 return; |
nuclear@6 | 39 } |
nuclear@6 | 40 } |
nuclear@6 | 41 |
nuclear@6 | 42 float damping = world ? world->get_damping() : 0.005; |
nuclear@6 | 43 |
nuclear@6 | 44 Vector3 newpos = co_prs->pos + vel * dt; |
nuclear@6 | 45 |
nuclear@6 | 46 Vector3 accel = impulse; |
nuclear@6 | 47 impulse.x = impulse.y = impulse.z = 0.0f; // reset impulse |
nuclear@6 | 48 |
nuclear@6 | 49 if(world) { |
nuclear@6 | 50 accel += world->get_gravity(); |
nuclear@6 | 51 } |
nuclear@6 | 52 |
nuclear@6 | 53 vel = (vel - vel * damping * dt) + accel * dt; |
nuclear@6 | 54 |
nuclear@6 | 55 // TODO collisions |
nuclear@6 | 56 |
nuclear@6 | 57 co_prs->pos = newpos; |
nuclear@6 | 58 } |