File size: 1,498 Bytes
4559903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "WorldOracle.h"

WorldOracle::WorldOracle(void){
}

WorldOracle::~WorldOracle(void){
}


double WorldOracle::getWorldHeightAt(Point3d worldLoc){
	double height = 0;
	worldLoc.y = 0;
	for (uint i=0;i<spheres.size();i++){
		//see if the given point falls within it...
		Vector3d v(spheres[i].pos, worldLoc);
		double r = spheres[i].radius;
		double dist = v.length();
		if (dist < r){
			double tmpHeight = sqrt(r*r - v.x*v.x - v.z*v.z) + spheres[i].pos.y;
			if (height < tmpHeight)
				height = tmpHeight;
		}
	}

	return height;
}

void createSpheresRBFile(DynamicArray<Sphere> spheres, char* fName){
	FILE* fp = fopen(fName, "w");
	//we'll create an RB with the correct CDP, mesh and position
	for (uint i=0;i<spheres.size();i++){
		fprintf(fp, "RigidBody\n");
		fprintf(fp, "\tstatic\n");
		fprintf(fp, "\tCDP_Sphere %2.3lf %2.3lf %2.3lf %2.3lf\n", spheres[i].pos.x,spheres[i].pos.y,spheres[i].pos.z, spheres[i].radius);
		fprintf(fp, "/End\n\n\n");
	}
	fclose(fp);
}


void WorldOracle::initializeWorld(World *physicalWorld){
//	spheres.push_back(Sphere(Point3d(0,-0.2,0), 0.3));
//	spheres.push_back(Sphere(Point3d(5, -1.0, 6), 1.1));

	if (spheres.size() > 0){
		createSpheresRBFile(spheres, "../data/objects/tmpSpheres.rbs");
		physicalWorld->loadRBsFromFile("../data/objects/tmpSpheres.rbs");
	}
}

void WorldOracle::draw(){
	for (uint i=0;i<spheres.size();i++)
		GLUtils::drawSphere(spheres[i].pos, spheres[i].radius, 9);
}