coeng

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