aboutsummaryrefslogtreecommitdiff
path: root/src/Person.cpp
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-10-01 16:13:41 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-10-01 16:13:41 +0700
commit7e530bdcc6ef88a1174f3408a7b2fed4eff43222 (patch)
tree39070c9566730db6f06e153e9e5bc76eade16d94 /src/Person.cpp
parentf076e700c738a9aea4fbac936f3b281cd35ee475 (diff)
downloadblackshades-7e530bdcc6ef88a1174f3408a7b2fed4eff43222.tar.gz
Remove Skeleton.num_joints
Diffstat (limited to 'src/Person.cpp')
-rw-r--r--src/Person.cpp167
1 files changed, 73 insertions, 94 deletions
diff --git a/src/Person.cpp b/src/Person.cpp
index 7630000..bd1e5cb 100644
--- a/src/Person.cpp
+++ b/src/Person.cpp
@@ -51,28 +51,21 @@ HitStruct Person::BulletCollideWithPlayer(int who, XYZ start, XYZ end){
XYZ collisionpoint;
GLfloat M[16];
int collide;
- XYZ average;
- int howmany;
- float distancemax;
HitStruct hitstruct;
hitstruct.collision=0;
//Make bounding sphere
- average=0;
- howmany=0;
- for(int j=0;j<skeleton.num_joints;j++){
- average.x=average.x+skeleton.joints[j].position.x;
- average.y=average.y+skeleton.joints[j].position.y;
- average.z=average.z+skeleton.joints[j].position.z;
- howmany++;
- }
- average=average/howmany;
- distancemax=0;
- for(int j=0;j<skeleton.num_joints;j++){
- if(findDistancefast(average,skeleton.joints[j].position)>distancemax){
- distancemax=findDistancefast(average,skeleton.joints[j].position);
- }
- }
- distancemax=sqrt(distancemax);
+
+ XYZ average {};
+ for (auto& joint : skeleton.joints)
+ average += joint.position;
+ average /= max_joints;
+
+ float distancemax = 0.0f;
+ for (auto& joint : skeleton.joints)
+ distancemax = max(distancemax,
+ findDistancefast(average, joint.position));
+ distancemax = sqrt(distancemax);
+
//Collide with player
if(skeleton.free<1){
start=start-playercoords;
@@ -85,18 +78,18 @@ HitStruct Person::BulletCollideWithPlayer(int who, XYZ start, XYZ end){
if(sphere_line_intersection(tempbulletloc[0].x,tempbulletloc[0].y,tempbulletloc[0].z,
tempbulletloc[1].x,tempbulletloc[1].y,tempbulletloc[1].z,
average.x, average.y, average.z, distancemax)){
- for(int j=0;j<skeleton.num_joints;j++){
- if(skeleton.joints[j].hasparent&&skeleton.joints[j].visible){
- tempbulletloc[0]=start;
- tempbulletloc[1]=end;
+ for (auto& joint : skeleton.joints) {
+ if (joint.hasparent && joint.visible) {
+ tempbulletloc[0] = start;
+ tempbulletloc[1] = end;
glPushMatrix();
glLoadIdentity();
- glScalef(1,1/skeleton.joints[j].length,1);
- glRotatef(skeleton.joints[j].rotate2-90,0,0,1);
- glRotatef(skeleton.joints[j].rotate1-90,0,1,0);
- glTranslatef( (-(skeleton.joints[j].position.x+skeleton.joints[j].parent->position.x)/2),
- (-(skeleton.joints[j].position.y+skeleton.joints[j].parent->position.y)/2),
- (-(skeleton.joints[j].position.z+skeleton.joints[j].parent->position.z)/2));
+ glScalef(1, 1 / joint.length, 1);
+ glRotatef(joint.rotate2 - 90, 0, 0, 1);
+ glRotatef(joint.rotate1 - 90, 0, 1, 0);
+ glTranslatef(-(joint.position.x + joint.parent->position.x) / 2,
+ -(joint.position.y + joint.parent->position.y) / 2,
+ -(joint.position.z + joint.parent->position.z) / 2);
glTranslatef(tempbulletloc[0].x,tempbulletloc[0].y,tempbulletloc[0].z);
glGetFloatv(GL_MODELVIEW_MATRIX,M);
tempbulletloc[0].x=M[12];
@@ -105,29 +98,29 @@ HitStruct Person::BulletCollideWithPlayer(int who, XYZ start, XYZ end){
glPopMatrix();
glPushMatrix();
glLoadIdentity();
- glScalef(1,1/skeleton.joints[j].length,1);
- glRotatef(skeleton.joints[j].rotate2-90,0,0,1);
- glRotatef(skeleton.joints[j].rotate1-90,0,1,0);
- glTranslatef( (-(skeleton.joints[j].position.x+skeleton.joints[j].parent->position.x)/2),
- (-(skeleton.joints[j].position.y+skeleton.joints[j].parent->position.y)/2),
- (-(skeleton.joints[j].position.z+skeleton.joints[j].parent->position.z)/2));
+ glScalef(1, 1 / joint.length, 1);
+ glRotatef(joint.rotate2 - 90, 0, 0, 1);
+ glRotatef(joint.rotate1 - 90, 0, 1, 0);
+ glTranslatef(-(joint.position.x + joint.parent->position.x) / 2,
+ -(joint.position.y + joint.parent->position.y) / 2,
+ -(joint.position.z + joint.parent->position.z) / 2);
glTranslatef(tempbulletloc[1].x,tempbulletloc[1].y,tempbulletloc[1].z);
glGetFloatv(GL_MODELVIEW_MATRIX,M);
tempbulletloc[1].x=M[12];
tempbulletloc[1].y=M[13];
tempbulletloc[1].z=M[14];
glPopMatrix();
- collide=skeletonmodels[skeleton.joints[j].modelnum].LineCheck(tempbulletloc[0],tempbulletloc[1],&collisionpoint);
+ collide=skeletonmodels[joint.modelnum].LineCheck(tempbulletloc[0],tempbulletloc[1],&collisionpoint);
if(collide!=-1)
{
glPushMatrix();
glLoadIdentity();
- glTranslatef( (skeleton.joints[j].position.x+skeleton.joints[j].parent->position.x)/2,
- (skeleton.joints[j].position.y+skeleton.joints[j].parent->position.y)/2,
- (skeleton.joints[j].position.z+skeleton.joints[j].parent->position.z)/2);
- glRotatef(-skeleton.joints[j].rotate1+90,0,1,0);
- glRotatef(-skeleton.joints[j].rotate2+90,0,0,1);
- glScalef(1,skeleton.joints[j].length,1);
+ glTranslatef((joint.position.x + joint.parent->position.x) / 2,
+ (joint.position.y + joint.parent->position.y) / 2,
+ (joint.position.z + joint.parent->position.z) / 2);
+ glRotatef(90 - joint.rotate1, 0, 1, 0);
+ glRotatef(90 - joint.rotate2, 0, 0, 1);
+ glScalef(1, joint.length, 1);
glTranslatef(collisionpoint.x,collisionpoint.y,collisionpoint.z);
glGetFloatv(GL_MODELVIEW_MATRIX,M);
collisionpoint.x=M[12];
@@ -136,8 +129,8 @@ HitStruct Person::BulletCollideWithPlayer(int who, XYZ start, XYZ end){
glPopMatrix();
hitstruct.collision=1;
hitstruct.hitlocation=collisionpoint;
- hitstruct.joint1=&skeleton.joints[j];
- hitstruct.joint2=skeleton.joints[j].parent;
+ hitstruct.joint1 = &joint;
+ hitstruct.joint2 = joint.parent;
}
}
}
@@ -302,7 +295,7 @@ void Person::DoAnimations(int who)
if(target>1)currentframe=targetframe;
- for(int i=0;i<skeleton.num_joints;i++){
+ for (int i = 0; i < max_joints; ++i) {
if(currentanimation!=lyinganim){
skeleton.joints[i].velocity=((animation[currentanimation].position[i][currentframe]*(1-target)+animation[targetanimation].position[i][targetframe]*(target))-(skeleton.joints[i].position))/multiplier;
skeleton.joints[i].position=animation[currentanimation].position[i][currentframe]*(1-target)+animation[targetanimation].position[i][targetframe]*(target);
@@ -357,33 +350,26 @@ void Person::DoAnimations(int who)
}
XYZ rotatearound;
XYZ oldpos;
- if(whichgun==sniperrifle){
- for(int i=0;i<skeleton.num_joints;i++){
- if(skeleton.joints[i].label!=righthand&&skeleton.joints[i].label!=rightelbow&&skeleton.joints[i].label!=rightwrist&&skeleton.joints[i].label!=lefthand&&skeleton.joints[i].label!=leftelbow&&skeleton.joints[i].label!=leftwrist){
- }else{
- skeleton.joints[i].position=animation[rifleholdanim].position[i][0];
- if(currentanimation==crouchanim||targetanimation==crouchanim)skeleton.joints[i].position-=(animation[idleanim].position[skeleton.jointlabels[neck]][0]-skeleton.joints[skeleton.jointlabels[neck]].position);
- }
- }
- }
- if(whichgun==shotgun){
- for(int i=0;i<skeleton.num_joints;i++){
- if(skeleton.joints[i].label!=righthand&&skeleton.joints[i].label!=rightelbow&&skeleton.joints[i].label!=rightwrist&&skeleton.joints[i].label!=lefthand&&skeleton.joints[i].label!=leftelbow&&skeleton.joints[i].label!=leftwrist){
- }else{
- skeleton.joints[i].position=animation[rifleholdanim].position[i][0];
- if(currentanimation==crouchanim||targetanimation==crouchanim)skeleton.joints[i].position-=(animation[idleanim].position[skeleton.jointlabels[neck]][0]-skeleton.joints[skeleton.jointlabels[neck]].position);
+ switch (whichgun) {
+ case sniperrifle:
+ case shotgun:
+ case assaultrifle:
+ for (int i = 0; i < max_joints; ++i)
+ switch (skeleton.joints[i].label) {
+ case leftelbow:
+ case leftwrist:
+ case lefthand:
+ case rightelbow:
+ case rightwrist:
+ case righthand:
+ skeleton.joints[i].position = animation[rifleholdanim].position[i][0];
+ if (currentanimation == crouchanim
+ || targetanimation == crouchanim)
+ skeleton.joints[i].position += skeleton.joints[skeleton.jointlabels[neck]].position
+ - animation[idleanim].position[skeleton.jointlabels[neck]][0];
}
- }
- }
- if(whichgun==assaultrifle){
- for(int i=0;i<skeleton.num_joints;i++){
- if(skeleton.joints[i].label!=righthand&&skeleton.joints[i].label!=rightelbow&&skeleton.joints[i].label!=rightwrist&&skeleton.joints[i].label!=lefthand&&skeleton.joints[i].label!=leftelbow&&skeleton.joints[i].label!=leftwrist){
- }else{
- skeleton.joints[i].position=animation[rifleholdanim].position[i][0];
- if(currentanimation==crouchanim||targetanimation==crouchanim)skeleton.joints[i].position-=(animation[idleanim].position[skeleton.jointlabels[neck]][0]-skeleton.joints[skeleton.jointlabels[neck]].position);
- }
- }
}
+
if((aiming||aimamount>0||whichgun==grenade)&&whichgun!=nogun){
if(aiming&&targetanimation!=joganim){
if(aimamount<1)aimamount+=multiplier*4;
@@ -401,7 +387,7 @@ void Person::DoAnimations(int who)
if(grenamount<0)grenamount=0;
}
rotatearound=skeleton.joints[skeleton.jointlabels[neck]].position;
- for(int i=0;i<skeleton.num_joints;i++){
+ for (int i = 0; i < max_joints; ++i){
if(skeleton.joints[i].label!=righthand&&skeleton.joints[i].label!=rightelbow&&skeleton.joints[i].label!=rightwrist&&skeleton.joints[i].label!=lefthand&&skeleton.joints[i].label!=leftelbow&&skeleton.joints[i].label!=leftwrist){
}else{
if(whichgun==sniperrifle){
@@ -473,7 +459,7 @@ void Person::DoAnimations(int who)
}
//Whack
if(attackframe>-1&&whichgun!=grenade){
- for(int i=0;i<skeleton.num_joints;i++){
+ for (int i = 0; i < max_joints; ++i) {
if(!skeleton.joints[i].lower){
if(attackframe==animation[riflehitanim].numframes)skeleton.joints[i].position=skeleton.joints[i].position*(attacktarget)+animation[riflehitanim].position[i][attackframe-1]*(1-attacktarget);
if(attackframe>0&&attackframe<animation[riflehitanim].numframes)skeleton.joints[i].position=animation[riflehitanim].position[i][attackframe-1]*(1-attacktarget)+animation[riflehitanim].position[i][attackframe]*(attacktarget);
@@ -490,7 +476,7 @@ void Person::DoAnimations(int who)
}
//Throw grenade
if(attackframe>-1&&whichgun==grenade&&ammo>0){
- for(int i=0;i<skeleton.num_joints;i++){
+ for (int i = 0; i < max_joints; ++i) {
if(!skeleton.joints[i].lower){
if(attackframe==animation[grenadethrowanim].numframes)skeleton.joints[i].position=skeleton.joints[i].position*(attacktarget)+animation[grenadethrowanim].position[i][attackframe-1]*(1-attacktarget);
if(attackframe>0&&attackframe<animation[grenadethrowanim].numframes)skeleton.joints[i].position=animation[grenadethrowanim].position[i][attackframe-1]*(1-attacktarget)+animation[grenadethrowanim].position[i][attackframe]*(attacktarget);
@@ -521,20 +507,16 @@ void Person::DoAnimations(int who)
}
rotatearound=skeleton.joints[skeleton.jointlabels[abdomen]].position;
- if(who==0)
- for(int i=0;i<skeleton.num_joints;i++){
- if(skeleton.joints[i].lower==0)
- skeleton.joints[i].position=rotatearound+DoRotation(skeleton.joints[i].position-rotatearound,playerrotation2/2,0,0);
- }
- if(who==0)
- for(int i=0;i<skeleton.num_joints;i++){
- if(skeleton.joints[i].lower==1&&skeleton.joints[i].label!=groin)
- skeleton.joints[i].position=DoRotation(skeleton.joints[i].position,0,playerlowrotation-playerrotation,0);
- }
- //head facing
- if(who==0){
- rotatearound=skeleton.joints[skeleton.jointlabels[neck]].position;
- skeleton.joints[skeleton.jointlabels[head]].position=rotatearound+DoRotation(skeleton.joints[skeleton.jointlabels[head]].position-rotatearound,playerrotation2/2,0,0);
+ if (who == 0) {
+ for (auto& joint : skeleton.joints)
+ if (!joint.lower)
+ joint.position = rotatearound
+ + DoRotation(joint.position - rotatearound, playerrotation2 / 2, 0, 0);
+ else if (joint.label != groin)
+ joint.position = DoRotation(joint.position, 0, playerlowrotation - playerrotation, 0);
+ rotatearound = skeleton.joints[skeleton.jointlabels[neck]].position;
+ skeleton.joints[skeleton.jointlabels[head]].position = rotatearound
+ + DoRotation(skeleton.joints[skeleton.jointlabels[head]].position - rotatearound, playerrotation2 / 2, 0, 0);
}
skeleton.DoConstraints();
@@ -1023,8 +1005,7 @@ int Person::DrawSkeleton(int who)
if (skeleton.offset && skeleton.free < 1) {
XYZ normal;
skeleton.offset = 0;
- for (int i = 0; i < skeleton.num_joints; i++) {
- auto& joint = skeleton.joints[i];
+ for (auto& joint : skeleton.joints) {
joint.oldposition = joint.position;
joint.position += joint.offset;
if (findLengthfast(joint.offset) >= multiplier * multiplier * 25) {
@@ -1105,7 +1086,7 @@ int Person::DrawSkeleton(int who)
+ skeleton.lowforward * 0.2;
Normalise(specialfwd++);
- for(int i = 0; i < skeleton.num_joints; i++)
+ for (int i = 0; i < max_joints; i++)
if (skeleton.joints[i].hasparent
&& skeleton.joints[i].visible)
skeleton.FindRotationJoint(i);
@@ -1116,16 +1097,14 @@ int Person::DrawSkeleton(int who)
}
}
- for (int i = 0; i < skeleton.num_joints; i++)
+ for (int i = 0; i < max_joints; i++)
draw_joint(skeleton.joints[i], who, whichcostume);
for (int i = 0; i < skeleton.num_muscles; i++)
draw_muscle(skeleton.muscles[i], who, whichcostume);
if (skeleton.offset && skeleton.free < 1)
- for (int i = 0; i < skeleton.num_joints; i++) {
- auto& joint = skeleton.joints[i];
+ for (auto& joint : skeleton.joints)
joint.position = joint.oldposition;
- }
glDisable(GL_LIGHT1);
return 0;