Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SM_Scene.cpp « src « Fuzzics « Sumo « Physics « gameengine « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e3eb7e5c5eab0f964b9811f7c21e9c1d3ac5aaf2 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
 * $Id$
 * Copyright (C) 2001 NaN Technologies B.V.
 * The physics scene.
 */

#ifdef WIN32
#pragma warning(disable : 4786)  // shut off 255 char limit debug template warning
#endif

#include "SM_Scene.h"
#include "SM_Object.h"
#include "SM_FhObject.h"

#include <algorithm>

void SM_Scene::add(SM_Object& object) {
	object.calcXform();
	m_objectList.push_back(&object);
	DT_AddObject(m_scene, object.getObjectHandle());
	if (object.isDynamic()) {
		DT_SetObjectResponse(m_respTable, object.getObjectHandle(),
							 SM_Object::boing, DT_SIMPLE_RESPONSE, this);
	}

	if (object.getDynamicParent()) {
		DT_SetPairResponse(m_respTable, object.getObjectHandle(),
						   object.getDynamicParent()->getObjectHandle(),
						   0, DT_NO_RESPONSE, 0);
	}
	SM_FhObject *fh_object = object.getFhObject();

	if (fh_object) {
		DT_AddObject(m_scene, fh_object->getObjectHandle());
		DT_SetObjectResponse(m_respTable, fh_object->getObjectHandle(),
							 SM_FhObject::ray_hit, DT_SIMPLE_RESPONSE, this);
	}
}	

void SM_Scene::remove(SM_Object& object) { 	
	T_ObjectList::iterator i =
		std::find(m_objectList.begin(), m_objectList.end(), &object);
	if (!(i == m_objectList.end()))
	{
		std::swap(*i, m_objectList.back());
		m_objectList.pop_back();
		DT_RemoveObject(m_scene, object.getObjectHandle());
		if (object.isDynamic()) {
			DT_ClearObjectResponse(m_respTable, object.getObjectHandle());
		}
		
		if (object.getDynamicParent()) {
			DT_ClearPairResponse(m_respTable, object.getObjectHandle(),
								 object.getDynamicParent()->getObjectHandle());
		}

		SM_FhObject *fh_object = object.getFhObject();
		
		if (fh_object) {
			DT_RemoveObject(m_scene, fh_object->getObjectHandle());
			DT_ClearObjectResponse(m_respTable,
								   fh_object->getObjectHandle());
		}
	} 
	else {
		// tried to remove an object that is not in the scene
		//assert(false);
	}
}	

void SM_Scene::proceed(MT_Scalar timeStep, MT_Scalar subSampling) {
	// Don't waste time...but it's OK to spill a little.
	if (timeStep < 0.001)
		return;

	// Divide the timeStep into a number of subsamples of size roughly 
	// equal to subSampling (might be a little smaller).
	int num_samples = (int)ceil(timeStep / subSampling);


	MT_Scalar subStep = timeStep / num_samples;
	T_ObjectList::iterator i;

	// Apply a forcefield (such as gravity)
	for (i = m_objectList.begin(); i != m_objectList.end(); ++i) {
		(*i)->applyForceField(m_forceField);
	}
	
	// Do the integration steps per object.
	int step;
	for (step = 0; step != num_samples; ++step) {

		for (i = m_objectList.begin(); i != m_objectList.end(); ++i) {
			(*i)->integrateForces(subStep);
		}

		// And second we update the object positions by performing
		// an integration step for each object
		for (i = m_objectList.begin(); i != m_objectList.end(); ++i) {
			(*i)->integrateMomentum(subStep);
		}
#if 0
		// I changed the order of the next 2 statements.
		// Originally objects were first integrated with a call
		// to proceed(). However if external objects were 
		// directly manipulating the velocities etc of physics 
		// objects then the physics environment would not be able 
		// to react before object positions were updated. --- Laurence.

		// So now first we let the physics scene respond to 
		// new forces, velocities set externally. 
#endif
		// The collsion and friction impulses are computed here. 
		DT_Test(m_scene, m_respTable);

	}


	// clear the user set velocities.
#if 0
	clearObjectCombinedVelocities();
#endif
	// Finish this timestep by saving al state information for the next
	// timestep and clearing the accumulated forces. 

	for (i = m_objectList.begin(); i != m_objectList.end(); ++i) {
		(*i)->proceedKinematic(timeStep);
		(*i)->saveReactionForce(timeStep);
		(*i)->clearForce();
	}
	
	// For each pair of object that collided, call the corresponding callback.
	// Additional collisions of a pair within the same time step are ignored.

	if (m_secondaryRespTable) {
		T_PairList::iterator p;
		for (p = m_pairList.begin(); p != m_pairList.end(); ++p) {
			DT_CallResponse(m_secondaryRespTable, 
							(*p).first->getObjectHandle(), 
							(*p).second->getObjectHandle(), 
							0);
		}
	}
	
	clearPairs();
}

SM_Object *SM_Scene::rayTest(void *ignore_client, 
							 const MT_Point3& from, const MT_Point3& to, 
							 MT_Point3& result, MT_Vector3& normal) const {
	MT_Point3 local; 
 
	SM_Object *hit_object = (SM_Object *) 
		DT_RayTest(m_scene, ignore_client, from.getValue(), to.getValue(),
				   local.getValue(), normal.getValue());

	if (hit_object) {
		result = hit_object->getWorldCoord(local);
	}

	return hit_object;
}

void SM_Scene::clearObjectCombinedVelocities() {

	T_ObjectList::iterator i;

	for (i = m_objectList.begin(); i != m_objectList.end(); ++i) {

		(*i)->clearCombinedVelocities();

	}

}