2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 /**> HEADER FILES <**/
25 #include "openal_wrapper.h"
26 #include "Animation.h"
28 extern float multiplier;
30 extern Skeleton testskeleton;
31 extern Terrain terrain;
32 extern int channels[100];
33 extern Objects objects;
34 extern int environment;
35 extern float terraindetail;
36 extern float camerashake;
39 extern XYZ envsound[30];
40 extern float envsoundvol[30];
41 extern int numenvsounds;
42 extern float envsoundlife[30];
43 extern int tutoriallevel;
45 extern int whichjointstartarray[26];
46 extern int whichjointendarray[26];
49 extern bool visibleloading;
51 void dealloc2(void* param){
56 void Muscle::DoConstraint(bool spinny)
60 static XYZ newpoint1,newpoint2;
62 static float oldlength;
63 static float relaxlength;
67 if(type!=boneconnect)relaxlength=findDistance(&parent1->position,&parent2->position);
69 if(type==boneconnect)strength=1;
70 if(type==constraint)strength=0;
72 if(strength<0)strength=0;
73 if(strength>1)strength=1;
75 length-=(length-relaxlength)*(1-strength)*multiplier*10000;
76 length-=(length-targetlength)*(strength)*multiplier*10000;
77 if(strength==0)length=relaxlength;
79 if((relaxlength-length>0&&relaxlength-oldlength<0)||(relaxlength-length<0&&relaxlength-oldlength>0))length=relaxlength;
82 if(length<minlength)length=minlength;
83 if(length>maxlength)length=maxlength;
87 if(length<minlength*.6)length=minlength*.6;
88 if(length>maxlength*1.4)length=maxlength*1.4;
91 if(length==relaxlength)return;
94 midp=(parent1->position*parent1->mass+parent2->position*parent2->mass)/(parent1->mass+parent2->mass);
95 //Find vector from midpoint to second vector
96 vel=parent2->position-midp;
97 //Change to unit vector
99 //Apply velocity change
100 newpoint1=midp-vel*length*(parent2->mass/(parent1->mass+parent2->mass));
101 newpoint2=midp+vel*length*(parent1->mass/(parent1->mass+parent2->mass));
103 parent1->velocity=parent1->velocity+(newpoint1-parent1->position)/multiplier/4;
104 parent2->velocity=parent2->velocity+(newpoint2-parent2->position)/multiplier/4;
108 parent1->velocity=parent1->velocity+(newpoint1-parent1->position);
109 parent2->velocity=parent2->velocity+(newpoint2-parent2->position);
111 //Move child point to within certain distance of parent point
112 parent1->position=newpoint1;
113 parent2->position=newpoint2;
116 void Skeleton::FindForwardsfirst()
118 //Find forward vectors
119 CrossProduct(joints[forwardjoints[1]].position-joints[forwardjoints[0]].position,joints[forwardjoints[2]].position-joints[forwardjoints[0]].position,&forward);
122 CrossProduct(joints[lowforwardjoints[1]].position-joints[lowforwardjoints[0]].position,joints[lowforwardjoints[2]].position-joints[lowforwardjoints[0]].position,&lowforward);
123 Normalise(&lowforward);
126 specialforward[0]=forward;
127 specialforward[1]=forward;
128 specialforward[2]=forward;
129 specialforward[3]=forward;
130 specialforward[4]=forward;
133 void Skeleton::FindForwards()
135 //Find forward vectors
136 CrossProduct(joints[forwardjoints[1]].position-joints[forwardjoints[0]].position,joints[forwardjoints[2]].position-joints[forwardjoints[0]].position,&forward);
139 CrossProduct(joints[lowforwardjoints[1]].position-joints[lowforwardjoints[0]].position,joints[lowforwardjoints[2]].position-joints[lowforwardjoints[0]].position,&lowforward);
140 Normalise(&lowforward);
143 specialforward[0]=forward;
145 specialforward[1]=joints[jointlabels[rightshoulder]].position+joints[jointlabels[rightwrist]].position;
146 specialforward[1]=joints[jointlabels[rightelbow]].position-specialforward[1]/2;
147 specialforward[1]+=forward*.4;
148 Normalise(&specialforward[1]);
149 specialforward[2]=joints[jointlabels[leftshoulder]].position+joints[jointlabels[leftwrist]].position;
150 specialforward[2]=joints[jointlabels[leftelbow]].position-specialforward[2]/2;
151 specialforward[2]+=forward*.4;
152 Normalise(&specialforward[2]);
154 specialforward[3]=joints[jointlabels[righthip]].position+joints[jointlabels[rightankle]].position;
155 specialforward[3]=specialforward[3]/2-joints[jointlabels[rightknee]].position;
156 specialforward[3]+=lowforward*.4;
157 Normalise(&specialforward[3]);
158 specialforward[4]=joints[jointlabels[lefthip]].position+joints[jointlabels[leftankle]].position;
159 specialforward[4]=specialforward[4]/2-joints[jointlabels[leftknee]].position;
160 specialforward[4]+=lowforward*.4;
161 Normalise(&specialforward[4]);
164 float Skeleton::DoConstraints(XYZ *coords,float *scale)
166 static float friction=1.5;
167 static float elasticity=.3;
168 static XYZ bounceness;
169 static XYZ oldpos[100];
170 static int numrepeats=3;
171 static float groundlevel=.15;
172 static float soundvolume;
173 static int i,j,k,l,m;
174 static XYZ temp,start,end;
175 static XYZ terrainnormal;
179 //static int whichjointstart,whichjointend;
180 static float distance;
181 static float frictionness;
182 static XYZ terrainlight;
183 static int whichpatchx;
184 static int whichpatchz;
187 static float tempmult;
188 static bool breaking;
194 freetime+=multiplier;
196 whichpatchx=coords->x/(terrain.size/subdivision*terrain.scale*terraindetail);
197 whichpatchz=coords->z/(terrain.size/subdivision*terrain.scale*terraindetail);
199 terrainlight=*coords;
200 objects.SphereCheckPossible(&terrainlight, 1);
202 for(i=0; i<num_joints; i++){
203 oldpos[i]=joints[i].position;
207 for(i=0; i<num_joints; i++){
208 //if(!isnormal(joints[i].velocity.x)||!isnormal(joints[i].velocity.y)||!isnormal(joints[i].velocity.z))joints[i].velocity=0;
209 joints[i].position=joints[i].position+joints[i].velocity*multiplier;
211 if(joints[i].label==head)groundlevel=.8;
212 if(joints[i].label==righthand||joints[i].label==rightwrist||joints[i].label==rightelbow)groundlevel=.2;
213 if(joints[i].label==lefthand||joints[i].label==leftwrist||joints[i].label==leftelbow)groundlevel=.2;
214 joints[i].position.y-=groundlevel;
215 //if(!joints[i].locked&&!broken)joints[i].velocity+=joints[i].velchange*multiplier*10*(500-longdead)/500;
216 joints[i].oldvelocity=joints[i].velocity;
219 //multiplier/=numrepeats;
220 for(j=0; j<numrepeats; j++){
221 if(!joints[jointlabels[rightknee]].locked&&!joints[jointlabels[righthip]].locked){
222 temp=joints[jointlabels[rightknee]].position-(joints[jointlabels[righthip]].position+joints[jointlabels[rightankle]].position)/2;
223 while(normaldotproduct(temp,lowforward)>-.1&&!sphere_line_intersection(&joints[jointlabels[righthip]].position,&joints[jointlabels[rightankle]].position,&joints[jointlabels[rightknee]].position,&r)){
224 joints[jointlabels[rightknee]].position-=lowforward*.05;
225 if(spinny)joints[jointlabels[rightknee]].velocity-=lowforward*.05/multiplier/4;
226 else joints[jointlabels[rightknee]].velocity-=lowforward*.05;
227 joints[jointlabels[rightankle]].position+=lowforward*.025;
228 if(spinny)joints[jointlabels[rightankle]].velocity+=lowforward*.025/multiplier/4;
229 else joints[jointlabels[rightankle]].velocity+=lowforward*.25;
230 joints[jointlabels[righthip]].position+=lowforward*.025;
231 if(spinny)joints[jointlabels[righthip]].velocity+=lowforward*.025/multiplier/4;
232 else joints[jointlabels[righthip]].velocity+=lowforward*.025;
233 temp=joints[jointlabels[rightknee]].position-(joints[jointlabels[righthip]].position+joints[jointlabels[rightankle]].position)/2;
236 if(!joints[jointlabels[leftknee]].locked&&!joints[jointlabels[righthip]].locked){
237 temp=joints[jointlabels[leftknee]].position-(joints[jointlabels[lefthip]].position+joints[jointlabels[leftankle]].position)/2;
238 while(normaldotproduct(temp,lowforward)>-.1&&!sphere_line_intersection(&joints[jointlabels[lefthip]].position,&joints[jointlabels[leftankle]].position,&joints[jointlabels[leftknee]].position,&r)){
239 joints[jointlabels[leftknee]].position-=lowforward*.05;
240 if(spinny)joints[jointlabels[leftknee]].velocity-=lowforward*.05/multiplier/4;
241 else joints[jointlabels[leftknee]].velocity-=lowforward*.05;
242 joints[jointlabels[leftankle]].position+=lowforward*.025;
243 if(spinny)joints[jointlabels[leftankle]].velocity+=lowforward*.025/multiplier/4;
244 else joints[jointlabels[leftankle]].velocity+=lowforward*.25;
245 joints[jointlabels[lefthip]].position+=lowforward*.025;
246 if(spinny)joints[jointlabels[lefthip]].velocity+=lowforward*.025/multiplier/4;
247 else joints[jointlabels[lefthip]].velocity+=lowforward*.025;
248 temp=joints[jointlabels[leftknee]].position-(joints[jointlabels[lefthip]].position+joints[jointlabels[leftankle]].position)/2;
252 if(terrain.patchobjectnum[whichpatchx][whichpatchz])
253 for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
254 k=terrain.patchobjects[whichpatchx][whichpatchz][m];
255 if(k<objects.numobjects&&k>=0)
256 if(objects.possible[k]){
257 temp=joints[jointlabels[head]].position*(*scale)+*coords;
258 if(objects.model[k].SphereCheck(&temp, 0.06, &start, &objects.position[k], &objects.rotation[k])!=-1){
259 //temp=(joints[jointlabels[head]].position*(*scale)+*coords)-start;
261 //joints[jointlabels[head]].position=((temp*.2+start)-*coords)/(*scale);
262 joints[jointlabels[head]].position=(temp-*coords)/(*scale);
270 startheadpos=joints[jointlabels[head]].position;
272 headpos=joints[jointlabels[head]].position+(joints[jointlabels[head]].position-joints[jointlabels[neck]].position);
273 if(terrain.patchobjectnum[whichpatchx][whichpatchz])
274 for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
275 k=terrain.patchobjects[whichpatchx][whichpatchz][m];
276 if(k<objects.numobjects&&k>=0)
277 if(objects.possible[k]){
278 friction=objects.friction[k];
279 start=joints[jointlabels[head]].position*(*scale)+*coords;
280 end=(headpos)*(*scale)+*coords;
281 whichhit=objects.model[k].LineCheckPossible(&start,&end,&temp,&objects.position[k],&objects.rotation[k]);
283 if(joints[jointlabels[head]].label==groin&&!joints[jointlabels[head]].locked&&joints[jointlabels[head]].delay<=0){
284 joints[jointlabels[head]].locked=1;
285 joints[jointlabels[head]].delay=1;
286 static float gLoc[3];
288 gLoc[0]=headpos.x*(*scale)+coords->x;
289 gLoc[1]=headpos.y*(*scale)+coords->y;
290 gLoc[2]=headpos.z*(*scale)+coords->z;
291 vel[0]=joints[jointlabels[head]].velocity.x;
292 vel[1]=joints[jointlabels[head]].velocity.y;
293 vel[2]=joints[jointlabels[head]].velocity.z;
294 PlaySoundEx( landsound1, samp[landsound1], NULL, true);
295 OPENAL_3D_SetAttributes(channels[landsound1], gLoc, vel);
296 OPENAL_SetVolume(channels[landsound1], 128);
297 OPENAL_SetPaused(channels[landsound1], false);
302 if(joints[jointlabels[head]].label==head&&!joints[jointlabels[head]].locked&&joints[jointlabels[head]].delay<=0){
303 joints[jointlabels[head]].locked=1;
304 joints[jointlabels[head]].delay=1;
305 static float gLoc[3];
307 gLoc[0]=headpos.x*(*scale)+coords->x;
308 gLoc[1]=headpos.y*(*scale)+coords->y;
309 gLoc[2]=headpos.z*(*scale)+coords->z;
310 vel[0]=joints[jointlabels[head]].velocity.x;
311 vel[1]=joints[jointlabels[head]].velocity.y;
312 vel[2]=joints[jointlabels[head]].velocity.z;
313 PlaySoundEx( landsound2, samp[landsound2], NULL, true);
314 OPENAL_3D_SetAttributes(channels[landsound2], gLoc, vel);
315 OPENAL_SetVolume(channels[landsound2], 128);
316 OPENAL_SetPaused(channels[landsound2], false);
319 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
320 if(terrainnormal.y>.8)freefall=0;
321 bounceness=terrainnormal*findLength(&joints[jointlabels[head]].velocity)*(abs(normaldotproduct(joints[jointlabels[head]].velocity,terrainnormal)));
322 if(findLengthfast(&joints[jointlabels[head]].velocity)>findLengthfast(&joints[jointlabels[head]].oldvelocity)){
324 joints[jointlabels[head]].velocity=joints[jointlabels[head]].oldvelocity;
326 if(findLengthfast(&bounceness)>4000&&breaking){
327 objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.rotation[k],0),.4,.5,Random()%360);
328 Sprite::MakeSprite(cloudsprite, headpos*(*scale)+*coords,joints[jointlabels[head]].velocity*.06, 1,1,1, 4, .2);
332 static float gLoc[3];
334 gLoc[0]=headpos.x*(*scale)+coords->x;
335 gLoc[1]=headpos.y*(*scale)+coords->y;
336 gLoc[2]=headpos.z*(*scale)+coords->z;
337 vel[0]=joints[jointlabels[head]].velocity.x;
338 vel[1]=joints[jointlabels[head]].velocity.y;
339 vel[2]=joints[jointlabels[head]].velocity.z;
340 PlaySoundEx( breaksound2, samp[breaksound2], NULL, true);
341 OPENAL_3D_SetAttributes(channels[breaksound2], gLoc, vel);
342 OPENAL_SetVolume(channels[breaksound2], 300);
343 OPENAL_SetPaused(channels[breaksound2], false);
345 envsound[numenvsounds]=*coords;
346 envsoundvol[numenvsounds]=64;
347 envsoundlife[numenvsounds]=.4;
350 if(objects.type[k]==treetrunktype){
351 objects.rotx[k]+=joints[jointlabels[head]].velocity.x*multiplier*.4;
352 objects.roty[k]+=joints[jointlabels[head]].velocity.z*multiplier*.4;
353 objects.rotx[k+1]+=joints[jointlabels[head]].velocity.x*multiplier*.4;
354 objects.roty[k+1]+=joints[jointlabels[head]].velocity.z*multiplier*.4;
356 if(!joints[jointlabels[head]].locked)damage+=findLengthfast(&bounceness)/2500;
357 ReflectVector(&joints[jointlabels[head]].velocity,&terrainnormal);
358 frictionness=abs(normaldotproduct(joints[jointlabels[head]].velocity,terrainnormal));//findLength(&bounceness)/findLength(&joints[jointlabels[head]].velocity);
359 joints[jointlabels[head]].velocity-=bounceness;
360 if(1-friction*frictionness>0)joints[jointlabels[head]].velocity*=1-friction*frictionness;
361 else joints[jointlabels[head]].velocity=0;
362 if(findLengthfast(&bounceness)>2500){
363 Normalise(&bounceness);
364 bounceness=bounceness*50;
366 joints[jointlabels[head]].velocity+=bounceness*elasticity;
369 if(!joints[jointlabels[head]].locked)
370 if(findLengthfast(&joints[jointlabels[head]].velocity)<1){
371 joints[jointlabels[head]].locked=1;
372 //joints[jointlabels[head]].velocity*=3;
374 if(findLengthfast(&bounceness)>500)Sprite::MakeSprite(cloudsprite, headpos*(*scale)+*coords,joints[jointlabels[head]].velocity*.06, 1,1,1, .5, .2);
375 joints[jointlabels[head]].position=(temp-*coords)/(*scale)+(startheadpos-headpos)+terrainnormal*.005;
376 if(longdead>100)broken=1;
382 for(i=0; i<num_joints; i++){
383 //joints[i].delay-=multiplier/1.5;
385 if(!spinny)if(findLengthfast(&joints[i].velocity)>320)joints[i].locked=0;
386 if(spinny)if(findLengthfast(&joints[i].velocity)>600)joints[i].locked=0;
387 if(joints[i].delay>0){
389 for(j=0;j<num_joints;j++){
390 if(joints[j].locked)freely=0;
392 if(freely)joints[i].delay-=multiplier*3;
394 //if(joints[i].delay>0)
395 //if(findLengthfast(&joints[i].velocity)>700&&joints[i].label!=head)joints[i].delay-=multiplier;
399 for(i=0; i<num_muscles; i++){
401 //muscles[i].DoConstraint(broken);
402 muscles[i].DoConstraint(spinny);
405 for(i=0; i<num_joints; i++){
406 //joints[i].delay-=multiplier/1.5;
410 if(joints[i].position.y*(*scale)+coords->y<terrain.getHeight(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z)+groundlevel){
413 if(joints[i].label==groin&&!joints[i].locked&&joints[i].delay<=0){
416 if(tutoriallevel!=1||id==0){
417 emit_sound_at(landsound1, joints[i].position*(*scale)+*coords, 128.);
422 if(joints[i].label==head&&!joints[i].locked&&joints[i].delay<=0){
425 if(tutoriallevel!=1||id==0){
426 emit_sound_at(landsound2, joints[i].position*(*scale)+*coords, 128.);
430 terrainnormal=terrain.getNormal(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z);
431 ReflectVector(&joints[i].velocity,&terrainnormal);
432 bounceness=terrainnormal*findLength(&joints[i].velocity)*(abs(normaldotproduct(joints[i].velocity,terrainnormal)));
433 if(!joints[i].locked)damage+=findLengthfast(&bounceness)/4000;
434 if(findLengthfast(&joints[i].velocity)<findLengthfast(&bounceness))bounceness=0;
435 frictionness=abs(normaldotproduct(joints[i].velocity,terrainnormal));//findLength(&bounceness)/findLength(&joints[i].velocity);
436 joints[i].velocity-=bounceness;
437 if(1-friction*frictionness>0)joints[i].velocity*=1-friction*frictionness;
438 else joints[i].velocity=0;
440 if(tutoriallevel!=1||id==0)
441 if(findLengthfast(&bounceness)>8000&&breaking){
442 objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.rotation[k],0),.4,.5,Random()%360);
443 Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, 4, .2);
444 //Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, 1, .2);
448 emit_sound_at(breaksound2, joints[i].position*(*scale)+*coords);
450 envsound[numenvsounds]=*coords;
451 envsoundvol[numenvsounds]=64;
452 envsoundlife[numenvsounds]=.4;
456 if(findLengthfast(&bounceness)>2500){
457 Normalise(&bounceness);
458 bounceness=bounceness*50;
461 joints[i].velocity+=bounceness*elasticity;
463 if(findLengthfast(&joints[i].velocity)>findLengthfast(&joints[i].oldvelocity)){
465 joints[i].velocity=joints[i].oldvelocity;
469 if(joints[i].locked==0)
470 if(findLengthfast(&joints[i].velocity)<1)joints[i].locked=1;
472 if(environment==snowyenvironment&&findLengthfast(&bounceness)>500&&terrain.getOpacity(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z)<.2){
473 terrainlight=terrain.getLighting(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z);
474 Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, terrainlight.x,terrainlight.y,terrainlight.z, .5, .7);
475 if(detail==2)terrain.MakeDecal(bodyprintdecal, joints[i].position*(*scale)+*coords,.4,.4,0);
477 else if(environment==desertenvironment&&findLengthfast(&bounceness)>500&&terrain.getOpacity(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z)<.2){
478 terrainlight=terrain.getLighting(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z);
479 Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, terrainlight.x*190/255,terrainlight.y*170/255,terrainlight.z*108/255, .5, .7);
482 else if(environment==grassyenvironment&&findLengthfast(&bounceness)>500&&terrain.getOpacity(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z)<.2){
483 terrainlight=terrain.getLighting(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z);
484 Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, terrainlight.x*90/255,terrainlight.y*70/255,terrainlight.z*8/255, .5, .5);
486 else if(findLengthfast(&bounceness)>500)Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, terrainlight.x,terrainlight.y,terrainlight.z, .5, .2);
489 joints[i].position.y=(terrain.getHeight(joints[i].position.x*(*scale)+coords->x,joints[i].position.z*(*scale)+coords->z)+groundlevel-coords->y)/(*scale);
490 if(longdead>100)broken=1;
492 if(terrain.patchobjectnum[whichpatchx][whichpatchz])
493 for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
494 k=terrain.patchobjects[whichpatchx][whichpatchz][m];
495 if(k<objects.numobjects&&k>=0)
496 if(objects.possible[k]){
497 friction=objects.friction[k];
498 start=joints[i].realoldposition;
499 end=joints[i].position*(*scale)+*coords;
500 whichhit=objects.model[k].LineCheckPossible(&start,&end,&temp,&objects.position[k],&objects.rotation[k]);
502 if(joints[i].label==groin&&!joints[i].locked&&joints[i].delay<=0){
505 if(tutoriallevel!=1||id==0){
506 emit_sound_at(landsound1, joints[i].position*(*scale)+*coords, 128.);
511 if(joints[i].label==head&&!joints[i].locked&&joints[i].delay<=0){
514 if(tutoriallevel!=1||id==0){
515 emit_sound_at(landsound2, joints[i].position*(*scale)+*coords, 128.);
519 terrainnormal=DoRotation(objects.model[k].facenormals[whichhit],0,objects.rotation[k],0)*-1;
520 if(terrainnormal.y>.8)freefall=0;
521 bounceness=terrainnormal*findLength(&joints[i].velocity)*(abs(normaldotproduct(joints[i].velocity,terrainnormal)));
522 if(findLengthfast(&joints[i].velocity)>findLengthfast(&joints[i].oldvelocity)){
524 joints[i].velocity=joints[i].oldvelocity;
526 if(tutoriallevel!=1||id==0)
527 if(findLengthfast(&bounceness)>4000&&breaking){
528 objects.model[k].MakeDecal(breakdecal,DoRotation(temp-objects.position[k],0,-objects.rotation[k],0),.4,.5,Random()%360);
529 Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, 4, .2);
533 emit_sound_at(breaksound2, joints[i].position*(*scale)+*coords);
535 envsound[numenvsounds]=*coords;
536 envsoundvol[numenvsounds]=64;
537 envsoundlife[numenvsounds]=.4;
540 if(objects.type[k]==treetrunktype){
541 //if(objects.rotx[k]==0||objects.roty[k]==0){
545 if(environment==grassyenvironment)howmany=findLength(&joints[i].velocity)*4/10;
546 if(environment==snowyenvironment)howmany=findLength(&joints[i].velocity)*1/10;
547 if(environment!=desertenvironment)
548 for(j=0;j<howmany;j++){
549 tempvel.x=float(abs(Random()%100)-50)/20;
550 tempvel.y=float(abs(Random()%100)-50)/20;
551 tempvel.z=float(abs(Random()%100)-50)/20;
552 pos=objects.position[k];
553 pos.y+=objects.scale[k]*15;
554 pos.x+=float(abs(Random()%100)-50)/100*objects.scale[k]*5;
555 pos.y+=float(abs(Random()%100)-50)/100*objects.scale[k]*15;
556 pos.z+=float(abs(Random()%100)-50)/100*objects.scale[k]*5;
557 Sprite::MakeSprite(splintersprite, pos,tempvel*.5, 165/255+float(abs(Random()%100)-50)/400,0,0, .2+float(abs(Random()%100)-50)/1300, 1);
558 Sprite::special[Sprite::numsprites-1]=1;
560 objects.rotx[k]+=joints[i].velocity.x*multiplier*.4;
561 objects.roty[k]+=joints[i].velocity.z*multiplier*.4;
562 objects.rotx[k+1]+=joints[i].velocity.x*multiplier*.4;
563 objects.roty[k+1]+=joints[i].velocity.z*multiplier*.4;
565 if(!joints[i].locked)damage+=findLengthfast(&bounceness)/2500;
566 ReflectVector(&joints[i].velocity,&terrainnormal);
567 frictionness=abs(normaldotproduct(joints[i].velocity,terrainnormal));//findLength(&bounceness)/findLength(&joints[i].velocity);
568 joints[i].velocity-=bounceness;
569 if(1-friction*frictionness>0)joints[i].velocity*=1-friction*frictionness;
570 else joints[i].velocity=0;
571 if(findLengthfast(&bounceness)>2500){
572 Normalise(&bounceness);
573 bounceness=bounceness*50;
575 joints[i].velocity+=bounceness*elasticity;
578 if(!joints[i].locked)
579 if(findLengthfast(&joints[i].velocity)<1){
581 //joints[i].velocity*=3;
583 if(findLengthfast(&bounceness)>500)Sprite::MakeSprite(cloudsprite, joints[i].position*(*scale)+*coords,joints[i].velocity*.06, 1,1,1, .5, .2);
584 joints[i].position=(temp-*coords)/(*scale)+terrainnormal*.005;
585 if(longdead>100)broken=1;
589 joints[i].realoldposition=joints[i].position*(*scale)+*coords;
595 if(terrain.patchobjectnum[whichpatchx][whichpatchz])
596 for(m=0;m<terrain.patchobjectnum[whichpatchx][whichpatchz];m++){
597 k=terrain.patchobjects[whichpatchx][whichpatchz][m];
598 if(objects.possible[k]){
600 //Make this less stupid
601 start=joints[jointlabels[whichjointstartarray[i]]].position*(*scale)+*coords;
602 end=joints[jointlabels[whichjointendarray[i]]].position*(*scale)+*coords;
603 whichhit=objects.model[k].LineCheckSlidePossible(&start,&end,&temp,&objects.position[k],&objects.rotation[k]);
605 joints[jointlabels[whichjointendarray[i]]].position=(end-*coords)/(*scale);
606 for(j=0; j<num_muscles; j++){
607 if((muscles[j].parent1->label==whichjointstartarray[i]&&muscles[j].parent2->label==whichjointendarray[i])||(muscles[j].parent2->label==whichjointstartarray[i]&&muscles[j].parent1->label==whichjointendarray[i]))
608 muscles[j].DoConstraint(spinny);
615 for(i=0; i<num_joints; i++){
617 if(joints[i].label==head)groundlevel=.8;
618 if(joints[i].label==righthand||joints[i].label==rightwrist||joints[i].label==rightelbow)groundlevel=.2;
619 if(joints[i].label==lefthand||joints[i].label==leftwrist||joints[i].label==leftelbow)groundlevel=.2;
620 joints[i].position.y+=groundlevel;
622 if(joints[i].label==lefthip||joints[i].label==leftknee||joints[i].label==leftankle||joints[i].label==righthip||joints[i].label==rightknee||joints[i].label==rightankle)joints[i].mass=2;
623 if(joints[i].locked){
631 for(i=0; i<num_muscles; i++){
632 if(muscles[i].type==boneconnect)
633 muscles[i].DoConstraint(0);
639 void Skeleton::DoGravity(float *scale)
642 for(i=0; i<num_joints; i++){
643 if(((joints[i].label!=leftknee&&joints[i].label!=rightknee)||lowforward.y>-.1||joints[i].mass<5)&&((joints[i].label!=rightelbow&&joints[i].label!=rightelbow)||forward.y<.3))joints[i].velocity.y+=gravity*multiplier/(*scale);
647 void Skeleton::Draw(int muscleview)
649 static float jointcolor[4];
664 //Calc motionblur-ness
665 for(int i=0; i<num_joints; i++){
666 joints[i].oldposition=joints[i].position;
667 joints[i].blurred=findDistance(&joints[i].position,&joints[i].oldposition)*100;
668 if(joints[i].blurred<1)joints[i].blurred=1;
674 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
676 for(int i=0; i<num_joints; i++){
677 if(joints[i].hasparent){
678 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].blurred);
679 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
680 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].parent->blurred);
681 glVertex3f(joints[i].parent->position.x,joints[i].parent->position.y,joints[i].parent->position.z);
682 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].parent->blurred);
683 glVertex3f(joints[i].parent->oldposition.x,joints[i].parent->oldposition.y,joints[i].parent->oldposition.z);
684 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].blurred);
685 glVertex3f(joints[i].oldposition.x,joints[i].oldposition.y,joints[i].oldposition.z);
688 for(int i=0; i<num_muscles; i++){
689 if(muscles[i].type==boneconnect){
690 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent2->blurred);
691 glVertex3f(muscles[i].parent1->position.x,muscles[i].parent1->position.y,muscles[i].parent1->position.z);
692 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent2->blurred);
693 glVertex3f(muscles[i].parent2->position.x,muscles[i].parent2->position.y,muscles[i].parent2->position.z);
694 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent2->blurred);
695 glVertex3f(muscles[i].parent2->oldposition.x,muscles[i].parent2->oldposition.y,muscles[i].parent2->oldposition.z);
696 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent1->blurred);
697 glVertex3f(muscles[i].parent1->oldposition.x,muscles[i].parent1->oldposition.y,muscles[i].parent1->oldposition.z);
703 for(int i=0; i<num_joints; i++){
704 if(joints[i].hasparent){
705 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].blurred);
706 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
707 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/joints[i].parent->blurred);
708 glVertex3f(joints[i].parent->position.x,joints[i].parent->position.y,joints[i].parent->position.z);
711 /*for(int i=0; i<num_joints; i++){
712 if(joints[i].hasparent){
713 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
714 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
715 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
716 glVertex3f(joints[i].position.x+forward.x,joints[i].position.y+forward.y,joints[i].position.z+forward.z);
719 for(int i=0; i<num_muscles; i++){
720 if(muscles[i].type==boneconnect){
721 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent1->blurred);
722 glVertex3f(muscles[i].parent1->position.x,muscles[i].parent1->position.y,muscles[i].parent1->position.z);
723 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],jointcolor[3]/muscles[i].parent2->blurred);
724 glVertex3f(muscles[i].parent2->position.x,muscles[i].parent2->position.y,muscles[i].parent2->position.z);
729 for(int i=0; i<num_muscles; i++){
730 if(muscles[i].type!=boneconnect){
731 glVertex3f(muscles[i].parent1->position.x,muscles[i].parent1->position.y,muscles[i].parent1->position.z);
732 glVertex3f(muscles[i].parent2->position.x,muscles[i].parent2->position.y,muscles[i].parent2->position.z);
740 for(int i=0; i<num_joints; i++){
741 if(i!=selected)glColor4f(0,0,.5,1);
742 if(i==selected)glColor4f(1,1,0,1);
743 if(joints[i].locked&&i!=selected)glColor4f(1,0,0,1);
744 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
749 //Set old position to current position
751 for(int i=0; i<num_joints; i++){
752 joints[i].oldposition=joints[i].position;
757 void Skeleton::AddJoint(float x, float y, float z, int which)
759 if(num_joints<max_joints-1){
760 joints[num_joints].velocity=0;
761 joints[num_joints].position.x=x;
762 joints[num_joints].position.y=y;
763 joints[num_joints].position.z=z;
764 joints[num_joints].mass=1;
765 joints[num_joints].locked=0;
767 /*if(which>=num_joints||which<0)*/joints[num_joints].hasparent=0;
768 /*if(which<num_joints&&which>=0){
769 joints[num_joints].parent=&joints[which];
770 joints[num_joints].hasparent=1;
771 joints[num_joints].length=findDistance(joints[num_joints].position,joints[num_joints].parent->position);
774 if(which<num_joints&&which>=0)AddMuscle(num_joints-1,which,0,10,boneconnect);
778 void Skeleton::DeleteJoint(int whichjoint)
780 if(whichjoint<num_joints&&whichjoint>=0){
781 joints[whichjoint].velocity=joints[num_joints-1].velocity;
782 joints[whichjoint].position=joints[num_joints-1].position;
783 joints[whichjoint].oldposition=joints[num_joints-1].oldposition;
784 joints[whichjoint].hasparent=joints[num_joints-1].hasparent;
785 joints[whichjoint].parent=joints[num_joints-1].parent;
786 joints[whichjoint].length=joints[num_joints-1].length;
787 joints[whichjoint].locked=joints[num_joints-1].locked;
788 joints[whichjoint].modelnum=joints[num_joints-1].modelnum;
789 joints[whichjoint].visible=joints[num_joints-1].visible;
791 for(int i=0;i<num_muscles;i++){
792 while(muscles[i].parent1==&joints[whichjoint]&&i<num_muscles)DeleteMuscle(i);
793 while(muscles[i].parent2==&joints[whichjoint]&&i<num_muscles)DeleteMuscle(i);
795 for(int i=0;i<num_muscles;i++){
796 while(muscles[i].parent1==&joints[num_joints-1]&&i<num_muscles)muscles[i].parent1=&joints[whichjoint];
797 while(muscles[i].parent2==&joints[num_joints-1]&&i<num_muscles)muscles[i].parent2=&joints[whichjoint];
799 for(int i=0;i<num_joints;i++){
800 if(joints[i].parent==&joints[whichjoint])joints[i].hasparent=0;
802 for(int i=0;i<num_joints;i++){
803 if(joints[i].parent==&joints[num_joints-1])joints[i].parent=&joints[whichjoint];
810 void Skeleton::DeleteMuscle(int whichmuscle)
812 if(whichmuscle<num_muscles){
813 muscles[whichmuscle].minlength=muscles[num_muscles-1].minlength;
814 muscles[whichmuscle].maxlength=muscles[num_muscles-1].maxlength;
815 muscles[whichmuscle].strength=muscles[num_muscles-1].strength;
816 muscles[whichmuscle].parent1=muscles[num_muscles-1].parent1;
817 muscles[whichmuscle].parent2=muscles[num_muscles-1].parent2;
818 muscles[whichmuscle].length=muscles[num_muscles-1].length;
819 muscles[whichmuscle].visible=muscles[num_muscles-1].visible;
820 muscles[whichmuscle].type=muscles[num_muscles-1].type;
821 muscles[whichmuscle].targetlength=muscles[num_muscles-1].targetlength;
827 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
829 if(whichjoint<num_joints){
830 joints[whichjoint].velocity=0;
831 joints[whichjoint].position.x=x;
832 joints[whichjoint].position.y=y;
833 joints[whichjoint].position.z=z;
835 if(which>=num_joints||which<0)joints[whichjoint].hasparent=0;
836 if(which<num_joints&&which>=0){
837 joints[whichjoint].parent=&joints[which];
838 joints[whichjoint].hasparent=1;
839 joints[whichjoint].length=findDistance(&joints[whichjoint].position,&joints[whichjoint].parent->position);
844 void Skeleton::AddMuscle(int attach1,int attach2,float minlength,float maxlength,int type)
846 if(num_muscles<max_muscles-1&&attach1<num_joints&&attach1>=0&&attach2<num_joints&&attach2>=0&&attach1!=attach2){
847 muscles[num_muscles].parent1=&joints[attach1];
848 muscles[num_muscles].parent2=&joints[attach2];
849 muscles[num_muscles].length=findDistance(&muscles[num_muscles].parent1->position,&muscles[num_muscles].parent2->position);
850 muscles[num_muscles].targetlength=findDistance(&muscles[num_muscles].parent1->position,&muscles[num_muscles].parent2->position);
851 muscles[num_muscles].strength=.7;
852 muscles[num_muscles].type=type;
853 muscles[num_muscles].minlength=minlength;
854 muscles[num_muscles].maxlength=maxlength;
860 void Skeleton::MusclesSet()
862 for(int i=0;i<num_muscles;i++){
863 muscles[i].length=findDistance(&muscles[i].parent1->position,&muscles[i].parent2->position);
867 void Skeleton::DoBalance()
870 newpoint=joints[0].position;
871 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
872 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
873 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
874 //Move child point to within certain distance of parent point
875 joints[0].position=newpoint;
880 void Skeleton::FindRotationMuscle(int which, int animation)
882 static XYZ temppoint1,temppoint2,tempforward;
883 static float distance;
885 temppoint1=muscles[which].parent1->position;
886 temppoint2=muscles[which].parent2->position;
887 distance=sqrt((temppoint1.x-temppoint2.x)*(temppoint1.x-temppoint2.x)+(temppoint1.y-temppoint2.y)*(temppoint1.y-temppoint2.y)+(temppoint1.z-temppoint2.z)*(temppoint1.z-temppoint2.z));
888 if((temppoint1.y-temppoint2.y)<=distance)muscles[which].rotate2=asin((temppoint1.y-temppoint2.y)/distance);
889 if((temppoint1.y-temppoint2.y)>distance)muscles[which].rotate2=asin(1.f);
890 muscles[which].rotate2*=360/6.28;
893 distance=sqrt((temppoint1.x-temppoint2.x)*(temppoint1.x-temppoint2.x)+(temppoint1.y-temppoint2.y)*(temppoint1.y-temppoint2.y)+(temppoint1.z-temppoint2.z)*(temppoint1.z-temppoint2.z));
894 if((temppoint1.z-temppoint2.z)<=distance)muscles[which].rotate1=acos((temppoint1.z-temppoint2.z)/distance);
895 if((temppoint1.z-temppoint2.z)>distance)muscles[which].rotate1=acos(1.f);
896 muscles[which].rotate1*=360/6.28;
897 if(temppoint1.x>temppoint2.x)muscles[which].rotate1=360-muscles[which].rotate1;
898 if(!isnormal(muscles[which].rotate1))muscles[which].rotate1=0;
899 if(!isnormal(muscles[which].rotate2))muscles[which].rotate2=0;
901 if(muscles[which].parent1->label==head)tempforward=specialforward[0];
902 else if(muscles[which].parent1->label==rightshoulder||muscles[which].parent1->label==rightelbow||muscles[which].parent1->label==rightwrist||muscles[which].parent1->label==righthand)tempforward=specialforward[1];
903 else if(muscles[which].parent1->label==leftshoulder||muscles[which].parent1->label==leftelbow||muscles[which].parent1->label==leftwrist||muscles[which].parent1->label==lefthand)tempforward=specialforward[2];
904 else if(muscles[which].parent1->label==righthip||muscles[which].parent1->label==rightknee||muscles[which].parent1->label==rightankle||muscles[which].parent1->label==rightfoot)tempforward=specialforward[3];
905 else if(muscles[which].parent1->label==lefthip||muscles[which].parent1->label==leftknee||muscles[which].parent1->label==leftankle||muscles[which].parent1->label==leftfoot)tempforward=specialforward[4];
906 else if(!muscles[which].parent1->lower)tempforward=forward;
907 else if(muscles[which].parent1->lower)tempforward=lowforward;
909 if(animation==hanganim){
910 if(muscles[which].parent1->label==righthand||muscles[which].parent2->label==righthand){
914 if(muscles[which].parent1->label==lefthand||muscles[which].parent2->label==lefthand){
921 if(muscles[which].parent1->label==rightfoot||muscles[which].parent2->label==rightfoot){
924 if(muscles[which].parent1->label==leftfoot||muscles[which].parent2->label==leftfoot){
930 tempforward=DoRotation(tempforward,0,muscles[which].rotate1-90,0);
931 tempforward=DoRotation(tempforward,0,0,muscles[which].rotate2-90);
933 tempforward/=sqrt(tempforward.x*tempforward.x+tempforward.y*tempforward.y+tempforward.z*tempforward.z);
934 if(tempforward.z<=1&&tempforward.z>=-1)muscles[which].rotate3=acos(0-tempforward.z);
935 else muscles[which].rotate3=acos(-1.f);
936 muscles[which].rotate3*=360/6.28;
937 if(0>tempforward.x)muscles[which].rotate3=360-muscles[which].rotate3;
938 if(!isnormal(muscles[which].rotate3))muscles[which].rotate3=0;
941 void Animation::Load(const char *filename, int aheight, int aattack)
945 static XYZ startoffset,endoffset;
948 static const char *anim_prefix = ":Data:Animations:";
953 int len = strlen(anim_prefix) + strlen(filename);
954 char *buf = new char[len + 1];
955 snprintf(buf, len + 1, "%s%s", anim_prefix, filename);
956 // Changing the filename into something the OS can understand
957 char *fixedFN = ConvertFileName(buf);
960 LOG(std::string("Loading animation...") + fixedFN);
967 if(visibleloading)pgame->LoadingScreen();
969 tfile=fopen( fixedFN, "rb" );
971 funpackf(tfile, "Bi Bi", &numframes, &joints);
973 for(i = 0; i < joints; i++){
974 if(position[i])dealloc2(position[i]);
975 if(twist[i])dealloc2(twist[i]);
976 if(twist2[i])dealloc2(twist2[i]);
977 if(onground[i])dealloc2(onground[i]);
980 if(position)dealloc2(position);
981 if(twist)dealloc2(twist);
982 if(twist2)dealloc2(twist2);
983 if(speed)dealloc2(speed);
984 if(onground)dealloc2(onground);
985 if(forward)dealloc2(forward);
986 if(weapontarget)dealloc2(weapontarget);
987 if(label)dealloc2(label);*/
989 position=(XYZ**)malloc(sizeof(XYZ*)*joints);
990 for(i = 0; i < joints; i++)
991 position[i] = (XYZ*)malloc(sizeof(XYZ)*numframes);
993 twist=(float**)malloc(sizeof(float*)*joints);
994 for(i = 0; i < joints; i++)
995 twist[i] = (float*)malloc(sizeof(float)*numframes);
997 twist2=(float**)malloc(sizeof(float*)*joints);
998 for(i = 0; i < joints; i++)
999 twist2[i] = (float*)malloc(sizeof(float)*numframes);
1001 speed = (float*)malloc(sizeof(float)*numframes);
1003 onground=(bool**)malloc(sizeof(bool*)*joints);
1004 for(i = 0; i < joints; i++)
1005 onground[i] =(bool*)malloc(sizeof(bool)*numframes);
1007 forward = (XYZ*)malloc(sizeof(XYZ)*numframes);
1008 weapontarget = (XYZ*)malloc(sizeof(XYZ)*numframes);
1009 label = (int*)malloc(sizeof(int)*numframes);
1011 /*position = new XYZ[joints][numframes];
1012 twist = new float[joints][numframes];
1013 twist2 = new float[joints][numframes];
1014 speed = new float[numframes];
1015 onground = new bool[joints][numframes];
1016 forward = new XYZ[numframes];
1017 label = new int[numframes];*/
1019 for(i=0;i<numframes;i++){
1020 for(j=0;j<joints;j++){
1021 funpackf(tfile, "Bf Bf Bf", &position[j][i].x,&position[j][i].y,&position[j][i].z);
1023 for(j=0;j<joints;j++){
1024 funpackf(tfile, "Bf", &twist[j][i]);
1026 for(j=0;j<joints;j++){
1028 funpackf(tfile, "Bb", &uch);
1029 onground[j][i] = (uch != 0);
1031 funpackf(tfile, "Bf", &speed[i]);
1033 for(i=0;i<numframes;i++){
1034 for(j=0;j<joints;j++){
1035 funpackf(tfile, "Bf", &twist2[j][i]);
1038 for(i=0;i<numframes;i++){
1039 funpackf(tfile, "Bf", &label[i]);
1041 funpackf(tfile, "Bi", &weapontargetnum);
1042 for(i=0;i<numframes;i++){
1043 funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x,&weapontarget[i].y,&weapontarget[i].z);
1052 for(j=0;j<joints;j++){
1053 if(position[j][0].y<1)
1054 startoffset+=position[j][0];
1055 if(position[j][numframes-1].y<1)
1056 endoffset+=position[j][numframes-1];
1059 startoffset/=howmany;
1066 void Animation::Move(XYZ how)
1068 static int i,j,joints;
1069 for(i=0;i<numframes;i++){
1070 for(j=0;j<joints;j++){
1076 void Skeleton::Load(const char *filename, const char *lowfilename, const char *clothesfilename,
1077 const char *modelfilename, const char *model2filename,
1078 const char *model3filename, const char *model4filename,
1079 const char *model5filename, const char *model6filename,
1080 const char *model7filename, const char *modellowfilename,
1081 const char *modelclothesfilename, bool aclothes)
1083 static GLfloat M[16];
1084 static int parentID;
1087 static int i,j,tempmuscle;
1100 for(i=0;i<num_models;i++){
1101 if(i==0)model[i].loadnotex(modelfilename);
1102 if(i==1)model[i].loadnotex(model2filename);
1103 if(i==2)model[i].loadnotex(model3filename);
1104 if(i==3)model[i].loadnotex(model4filename);
1105 if(i==4)model[i].loadnotex(model5filename);
1106 if(i==5)model[i].loadnotex(model6filename);
1107 if(i==6)model[i].loadnotex(model7filename);
1108 model[i].Rotate(180,0,0);
1109 model[i].Scale(.04,.04,.04);
1110 model[i].CalculateNormals(0);
1113 drawmodel.load(modelfilename,0);
1114 drawmodel.Rotate(180,0,0);
1115 drawmodel.Scale(.04,.04,.04);
1116 drawmodel.FlipTexCoords();
1117 if(tutoriallevel==1&&id!=0)drawmodel.UniformTexCoords();
1118 if(tutoriallevel==1&&id!=0)drawmodel.ScaleTexCoords(0.1);
1119 drawmodel.CalculateNormals(0);
1121 modellow.loadnotex(modellowfilename);
1122 modellow.Rotate(180,0,0);
1123 modellow.Scale(.04,.04,.04);
1124 modellow.CalculateNormals(0);
1126 drawmodellow.load(modellowfilename,0);
1127 drawmodellow.Rotate(180,0,0);
1128 drawmodellow.Scale(.04,.04,.04);
1129 drawmodellow.FlipTexCoords();
1130 if(tutoriallevel==1&&id!=0)drawmodellow.UniformTexCoords();
1131 if(tutoriallevel==1&&id!=0)drawmodellow.ScaleTexCoords(0.1);
1132 drawmodellow.CalculateNormals(0);
1135 modelclothes.loadnotex(modelclothesfilename);
1136 modelclothes.Rotate(180,0,0);
1137 modelclothes.Scale(.041,.04,.041);
1138 modelclothes.CalculateNormals(0);
1140 drawmodelclothes.load(modelclothesfilename,0);
1141 drawmodelclothes.Rotate(180,0,0);
1142 drawmodelclothes.Scale(.04,.04,.04);
1143 drawmodelclothes.FlipTexCoords();
1144 drawmodelclothes.CalculateNormals(0);
1147 tfile=fopen( ConvertFileName(filename), "rb" );
1149 funpackf(tfile, "Bi", &num_joints);
1150 //joints.resize(num_joints);
1151 if(joints) delete [] joints; //dealloc2(joints);
1152 joints=(Joint*)new Joint[num_joints]; //malloc(sizeof(Joint)*num_joints);
1154 for(i=0;i<num_joints;i++){
1155 funpackf(tfile, "Bf Bf Bf Bf Bf", &joints[i].position.x, &joints[i].position.y, &joints[i].position.z, &joints[i].length,&joints[i].mass);
1156 funpackf(tfile, "Bb Bb", &joints[i].hasparent,&joints[i].locked);
1157 funpackf(tfile, "Bi", &joints[i].modelnum);
1158 funpackf(tfile, "Bb Bb", &joints[i].visible,&joints[i].sametwist);
1159 funpackf(tfile, "Bi Bi", &joints[i].label,&joints[i].hasgun);
1160 funpackf(tfile, "Bb", &joints[i].lower);
1161 funpackf(tfile, "Bi", &parentID);
1162 if(joints[i].hasparent)joints[i].parent=&joints[parentID];
1163 joints[i].velocity=0;
1164 joints[i].oldposition=joints[i].position;
1166 tempmuscle=num_muscles;
1167 funpackf(tfile, "Bi", &num_muscles);
1169 if(muscles) delete [] muscles; //dealloc2(muscles);
1170 muscles=(Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
1172 for(i=0;i<num_muscles;i++){
1173 tempmuscle=muscles[i].numvertices;
1174 funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &muscles[i].length, &muscles[i].targetlength,&muscles[i].minlength, &muscles[i].maxlength,&muscles[i].strength,&muscles[i].type,&muscles[i].numvertices);
1175 //muscles[i].vertices.clear();
1176 //muscles[i].vertices.resize(muscles[i].numvertices);
1177 //if(muscles[i].vertices)dealloc2(muscles[i].vertices);
1178 muscles[i].vertices=(int*)malloc(sizeof(int)*muscles[i].numvertices);
1181 for(j=0;j<muscles[i].numvertices-edit;j++){
1182 funpackf(tfile, "Bi", &muscles[i].vertices[j+edit]);
1183 if(muscles[i].vertices[j+edit]>=model[0].vertexNum){
1184 muscles[i].numvertices--;
1188 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
1189 muscles[i].parent1=&joints[parentID];
1190 funpackf(tfile, "Bi", &parentID);
1191 muscles[i].parent2=&joints[parentID];
1194 funpackf(tfile, "Bi", &forwardjoints[j]);
1197 funpackf(tfile, "Bi", &lowforwardjoints[j]);
1199 for(j=0;j<num_muscles;j++){
1200 for(i=0;i<muscles[j].numvertices;i++){
1201 for(int k=0;k<num_models;k++){
1202 if(muscles[j].numvertices&&muscles[j].vertices[i]<model[k].vertexNum)model[k].owner[muscles[j].vertices[i]]=j;
1207 for(i=0;i<num_joints;i++){
1208 joints[i].startpos=joints[i].position;
1210 for(i=0;i<num_muscles;i++){
1211 FindRotationMuscle(i,-1);
1213 for(int k=0;k<num_models;k++){
1214 for(i=0;i<model[k].vertexNum;i++){
1215 model[k].vertex[i]=model[k].vertex[i]-(muscles[model[k].owner[i]].parent1->position+muscles[model[k].owner[i]].parent2->position)/2;
1216 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1219 glRotatef(muscles[model[k].owner[i]].rotate3,0,1,0);
1220 glRotatef(muscles[model[k].owner[i]].rotate2-90,0,0,1);
1221 glRotatef(muscles[model[k].owner[i]].rotate1-90,0,1,0);
1222 glTranslatef(model[k].vertex[i].x,model[k].vertex[i].y,model[k].vertex[i].z);
1223 glGetFloatv(GL_MODELVIEW_MATRIX,M);
1224 model[k].vertex[i].x=M[12]*1;
1225 model[k].vertex[i].y=M[13]*1;
1226 model[k].vertex[i].z=M[14]*1;
1229 model[k].CalculateNormals(0);
1234 tfile=fopen( ConvertFileName(lowfilename), "rb" );
1236 lSize=sizeof(num_joints);
1237 fseek ( tfile, lSize, SEEK_CUR);
1238 //joints = new Joint[num_joints];
1239 //jointlabels = new int[num_joints];
1240 for(i=0;i<num_joints;i++){
1242 fseek ( tfile, lSize, SEEK_CUR);
1243 lSize=sizeof(float);
1244 fseek ( tfile, lSize, SEEK_CUR);
1245 lSize=sizeof(float);
1246 fseek ( tfile, lSize, SEEK_CUR);
1247 lSize=1;//sizeof(bool);
1248 fseek ( tfile, lSize, SEEK_CUR);
1249 lSize=1;//sizeof(bool);
1250 fseek ( tfile, lSize, SEEK_CUR);
1252 fseek ( tfile, lSize, SEEK_CUR);
1253 lSize=1;//sizeof(bool);
1254 fseek ( tfile, lSize, SEEK_CUR);
1255 lSize=1;//sizeof(bool);
1256 fseek ( tfile, lSize, SEEK_CUR);
1258 fseek ( tfile, lSize, SEEK_CUR);
1260 fseek ( tfile, lSize, SEEK_CUR);
1261 lSize=1;//sizeof(bool);
1262 fseek ( tfile, lSize, SEEK_CUR);
1264 fseek ( tfile, lSize, SEEK_CUR);
1265 if(joints[i].hasparent)joints[i].parent=&joints[parentID];
1266 joints[i].velocity=0;
1267 joints[i].oldposition=joints[i].position;
1269 funpackf(tfile, "Bi", &num_muscles);
1270 //muscles = new Muscle[num_muscles];
1271 for(i=0;i<num_muscles;i++){
1272 lSize=sizeof(float);
1273 fseek ( tfile, lSize, SEEK_CUR);
1274 lSize=sizeof(float);
1275 fseek ( tfile, lSize, SEEK_CUR);
1276 lSize=sizeof(float);
1277 fseek ( tfile, lSize, SEEK_CUR);
1278 lSize=sizeof(float);
1279 fseek ( tfile, lSize, SEEK_CUR);
1280 lSize=sizeof(float);
1281 fseek ( tfile, lSize, SEEK_CUR);
1283 fseek ( tfile, lSize, SEEK_CUR);
1284 tempmuscle=muscles[i].numverticeslow;
1285 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1286 if(muscles[i].numverticeslow){
1287 //muscles[i].verticeslow.clear();
1288 //muscles[i].verticeslow.resize(muscles[i].numverticeslow);
1289 //if(muscles[i].verticeslow)dealloc2(muscles[i].verticeslow);
1290 muscles[i].verticeslow=(int*)malloc(sizeof(int)*muscles[i].numverticeslow);
1292 for(j=0;j<muscles[i].numverticeslow-edit;j++){
1293 funpackf(tfile, "Bi", &muscles[i].verticeslow[j+edit]);
1294 if(muscles[i].verticeslow[j+edit]>=modellow.vertexNum){
1295 muscles[i].numverticeslow--;
1302 lSize=1;//sizeof(bool);
1303 fseek ( tfile, lSize, SEEK_CUR);
1305 fseek ( tfile, lSize, SEEK_CUR);
1306 fseek ( tfile, lSize, SEEK_CUR);
1309 for(j=0;j<num_muscles;j++){
1310 for(i=0;i<muscles[j].numverticeslow;i++){
1311 if(muscles[j].numverticeslow&&muscles[j].verticeslow[i]<modellow.vertexNum)modellow.owner[muscles[j].verticeslow[i]]=j;
1315 for(i=0;i<num_joints;i++){
1316 joints[i].startpos=joints[i].position;
1318 for(i=0;i<num_muscles;i++){
1319 FindRotationMuscle(i,-1);
1321 for(i=0;i<modellow.vertexNum;i++){
1322 modellow.vertex[i]=modellow.vertex[i]-(muscles[modellow.owner[i]].parent1->position+muscles[modellow.owner[i]].parent2->position)/2;
1323 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1326 glRotatef(muscles[modellow.owner[i]].rotate3,0,1,0);
1327 glRotatef(muscles[modellow.owner[i]].rotate2-90,0,0,1);
1328 glRotatef(muscles[modellow.owner[i]].rotate1-90,0,1,0);
1329 glTranslatef(modellow.vertex[i].x,modellow.vertex[i].y,modellow.vertex[i].z);
1330 glGetFloatv(GL_MODELVIEW_MATRIX,M);
1331 modellow.vertex[i].x=M[12];
1332 modellow.vertex[i].y=M[13];
1333 modellow.vertex[i].z=M[14];
1336 modellow.CalculateNormals(0);
1340 tfile=fopen( ConvertFileName(clothesfilename), "rb" );
1341 lSize=sizeof(num_joints);
1342 fseek ( tfile, lSize, SEEK_CUR);
1343 //joints = new Joint[num_joints];
1344 //jointlabels = new int[num_joints];
1345 for(i=0;i<num_joints;i++){
1347 fseek ( tfile, lSize, SEEK_CUR);
1348 lSize=sizeof(float);
1349 fseek ( tfile, lSize, SEEK_CUR);
1350 lSize=sizeof(float);
1351 fseek ( tfile, lSize, SEEK_CUR);
1352 lSize=1;//sizeof(bool);
1353 fseek ( tfile, lSize, SEEK_CUR);
1354 lSize=1;//sizeof(bool);
1355 fseek ( tfile, lSize, SEEK_CUR);
1357 fseek ( tfile, lSize, SEEK_CUR);
1358 lSize=1;//sizeof(bool);
1359 fseek ( tfile, lSize, SEEK_CUR);
1360 lSize=1;//sizeof(bool);
1361 fseek ( tfile, lSize, SEEK_CUR);
1363 fseek ( tfile, lSize, SEEK_CUR);
1365 fseek ( tfile, lSize, SEEK_CUR);
1366 lSize=1;//sizeof(bool);
1367 fseek ( tfile, lSize, SEEK_CUR);
1369 fseek ( tfile, lSize, SEEK_CUR);
1370 if(joints[i].hasparent)joints[i].parent=&joints[parentID];
1371 joints[i].velocity=0;
1372 joints[i].oldposition=joints[i].position;
1374 funpackf(tfile, "Bi", &num_muscles);
1375 //muscles = new Muscle[num_muscles];
1376 for(i=0;i<num_muscles;i++){
1377 lSize=sizeof(float);
1378 fseek ( tfile, lSize, SEEK_CUR);
1379 lSize=sizeof(float);
1380 fseek ( tfile, lSize, SEEK_CUR);
1381 lSize=sizeof(float);
1382 fseek ( tfile, lSize, SEEK_CUR);
1383 lSize=sizeof(float);
1384 fseek ( tfile, lSize, SEEK_CUR);
1385 lSize=sizeof(float);
1386 fseek ( tfile, lSize, SEEK_CUR);
1388 fseek ( tfile, lSize, SEEK_CUR);
1389 tempmuscle=muscles[i].numverticesclothes;
1390 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1391 if(muscles[i].numverticesclothes){
1392 //muscles[i].verticesclothes.clear();
1393 //muscles[i].verticesclothes.resize(muscles[i].numverticesclothes);
1394 //if(muscles[i].verticesclothes)dealloc2(muscles[i].verticesclothes);
1395 muscles[i].verticesclothes=(int*)malloc(sizeof(int)*muscles[i].numverticesclothes);
1397 for(j=0;j<muscles[i].numverticesclothes-edit;j++){
1398 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j+edit]);
1399 if(muscles[i].verticesclothes[j+edit]>=modelclothes.vertexNum){
1400 muscles[i].numverticesclothes--;
1405 lSize=1;//sizeof(bool);
1406 fseek ( tfile, lSize, SEEK_CUR);
1408 fseek ( tfile, lSize, SEEK_CUR);
1409 fseek ( tfile, lSize, SEEK_CUR);
1412 for(j=0;j<num_muscles;j++){
1413 for(i=0;i<muscles[j].numverticesclothes;i++){
1414 if(muscles[j].numverticesclothes&&muscles[j].verticesclothes[i]<modelclothes.vertexNum)modelclothes.owner[muscles[j].verticesclothes[i]]=j;
1418 for(i=0;i<num_joints;i++){
1419 joints[i].startpos=joints[i].position;
1421 for(i=0;i<num_muscles;i++){
1422 FindRotationMuscle(i,-1);
1424 for(i=0;i<modelclothes.vertexNum;i++){
1425 modelclothes.vertex[i]=modelclothes.vertex[i]-(muscles[modelclothes.owner[i]].parent1->position+muscles[modelclothes.owner[i]].parent2->position)/2;
1426 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1429 glRotatef(muscles[modelclothes.owner[i]].rotate3,0,1,0);
1430 glRotatef(muscles[modelclothes.owner[i]].rotate2-90,0,0,1);
1431 glRotatef(muscles[modelclothes.owner[i]].rotate1-90,0,1,0);
1432 glTranslatef(modelclothes.vertex[i].x,modelclothes.vertex[i].y,modelclothes.vertex[i].z);
1433 glGetFloatv(GL_MODELVIEW_MATRIX,M);
1434 modelclothes.vertex[i].x=M[12];
1435 modelclothes.vertex[i].y=M[13];
1436 modelclothes.vertex[i].z=M[14];
1439 modelclothes.CalculateNormals(0);
1443 for(i=0;i<num_joints;i++){
1444 for(j=0;j<num_joints;j++){
1445 if(joints[i].label==j)jointlabels[j]=i;
1452 Animation::Animation()
1458 weapontargetnum = 0;
1470 Animation::~Animation()
1475 void Animation::deallocate()
1481 for(i = 0; i < joints; i++)
1482 dealloc2(position[i]);
1490 for(i = 0; i < joints; i++)
1499 for(i = 0; i < joints; i++)
1500 dealloc2(twist2[i]);
1508 for(i = 0; i < joints; i++)
1509 dealloc2(onground[i]);
1515 if(speed)dealloc2(speed);
1518 if(forward)dealloc2(forward);
1521 if(weapontarget)dealloc2(weapontarget);
1524 if(label)dealloc2(label);
1530 Skeleton::Skeleton()
1538 memset(forwardjoints, 0, sizeof(forwardjoints));
1543 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1546 // XYZ specialforward[5];
1547 memset(jointlabels, 0, sizeof(jointlabels));
1551 // Model modelclothes;
1555 // Model drawmodellow;
1556 // Model drawmodelclothes;
1561 memset(skinText, 0, sizeof(skinText));
1578 Skeleton::~Skeleton()
1601 numverticesclothes = 0;
1610 rotate1 = 0,rotate2 = 0,rotate3 = 0;
1611 lastrotate1 = 0,lastrotate2 = 0,lastrotate3 = 0;
1612 oldrotate1 = 0,oldrotate2 = 0,oldrotate3 = 0;
1613 newrotate1 = 0,newrotate2 = 0,newrotate3 = 0;
1621 dealloc2(verticeslow);
1622 dealloc2(verticesclothes);
1625 Animation & Animation::operator = (const Animation & ani)
1629 bool allocate = true;
1631 allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1633 if (allocate) deallocate();
1635 numframes = ani.numframes;
1636 height = ani.height;
1637 attack = ani.attack;
1638 joints = ani.joints;
1639 weapontargetnum = ani.weapontargetnum;
1641 if (allocate) position=(XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1642 for(i = 0; i < ani.joints; i++)
1644 if (allocate) position[i] = (XYZ*)malloc(sizeof(XYZ)*ani.numframes);
1645 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1648 if (allocate) twist=(float**)malloc(sizeof(float*)*ani.joints);
1649 for(i = 0; i < ani.joints; i++)
1651 if (allocate) twist[i] = (float*)malloc(sizeof(float)*ani.numframes);
1652 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1655 if (allocate) twist2=(float**)malloc(sizeof(float*)*ani.joints);
1656 for(i = 0; i < ani.joints; i++)
1658 if (allocate) twist2[i] = (float*)malloc(sizeof(float)*ani.numframes);
1659 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1662 if (allocate) speed = (float*)malloc(sizeof(float)*ani.numframes);
1663 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1665 if (allocate) onground=(bool**)malloc(sizeof(bool*)*ani.joints);
1666 for(i = 0; i < ani.joints; i++)
1668 if (allocate) onground[i] =(bool*)malloc(sizeof(bool)*ani.numframes);
1669 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1672 if (allocate) forward = (XYZ*)malloc(sizeof(XYZ)*ani.numframes);
1673 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1675 if (allocate) weapontarget = (XYZ*)malloc(sizeof(XYZ)*ani.numframes);
1676 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1678 if (allocate) label = (int*)malloc(sizeof(int)*ani.numframes);
1679 memcpy(label, ani.label, sizeof(int)*ani.numframes);