1 module ws.physics.bullet.object;
2 
3 import
4 	std.conv,
5 	std.string,
6 	ws.io,
7 	ws.math.vector,
8 	ws.math.quaternion,
9 	ws.file.obj,
10 	ws.thread.loader,
11 	ws.physics.bullet.cbullet,
12 	ws.physics.bullet.shape;
13 
14 
15 class BulletObject: Loadable {
16 	
17 	this(BulletWorld* world, Shape shape){
18 		assert(world);
19 		this.shape = shape;
20 		this.path = path;
21 		this.world = getWorld(world);
22 	}
23 
24 	string path;
25 
26 	protected {
27 		btDynamicsWorld* world;
28 		Shape shape;
29 		btRigidBody* rigid;
30 	}
31 
32 
33 	void setVel(float[3] vel){
34 		onLoaded ~= {
35 			setLinearVel(rigid, vel[0], vel[1], vel[2]);
36 		};
37 	}
38 
39 	float[3] getVel(){
40 		if(loaded){
41 			btScalar[3] vel;
42 			getLinearVel(rigid, vel.ptr);
43 			return [cast(float)vel[0], cast(float)vel[1], cast(float)vel[2]];
44 		}
45 		return [0,0,0];
46 	}
47 
48 	void setPos(float[3] pos){
49 		onLoaded ~= {
50 			ws.physics.bullet.cbullet.translate(rigid, pos[0]*2, pos[1]*2, pos[2]*2);
51 		};
52 	}
53 
54 	float[3] getPos(){
55 		if(loaded){
56 			assert(rigid);
57 			btScalar[3] pos;
58 			getTranslate(rigid, pos.ptr);
59 			return [pos[0]/2, pos[1]/2, pos[2]/2];
60 		}
61 		return [0,0,0];
62 	}
63 	
64 	void setAngle(Quaternion a){
65 		onLoaded ~= {
66 			btScalar[4] data = [a.z, -a.y, a.x, a.w];
67 			rotate(rigid, data.ptr);
68 		};
69 	}
70 	
71 	Quaternion getAngle(){
72 		if(!loaded)
73 			return Quaternion();
74 		btScalar[4] data;
75 		getRotate(rigid, data.ptr);
76 		return Quaternion(data[2], -data[1], data[0], data[3])*Quaternion.euler(0,-180,0);
77 	}
78 
79 	void applyForce(float[3] f){
80 		ws.physics.bullet.cbullet.applyForce(rigid, f[0], f[1], f[2]);
81 	}
82 
83 	void destroy(){
84 		onLoaded ~= {
85 			assert(rigid, "Object already destroyed");
86 			removeRigid(world, rigid);
87 		};
88 	}
89 
90 
91 	void setMass(double m){
92 		ws.physics.bullet.cbullet.setMass(rigid, m);
93 	}
94 
95 	/+
96 	double getMass(){
97 		return ws.physics.bullet.cbullet.getMass(rigid);
98 	}
99 	+/
100 
101 	override void finish(){
102 		//shape = loadObjCompound(("models/"~path).toStringz());
103 		assert(shape);
104 		rigid = addRigid(shape.shape, 100, world);
105 		assert(rigid);
106 		setUserPointer(rigid, cast(void*)this);
107 		loadState = Loaded;
108 	}
109 
110 }
111