From b474447d20876f5ce9e83902c78deb4e56b7aaa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Fri, 2 Jul 2021 11:30:47 +0700 Subject: Fix array out of bound in tackling Also Debian updated OpenAL (-; --- src/Skeleton.cpp | 187 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 98 insertions(+), 89 deletions(-) (limited to 'src/Skeleton.cpp') diff --git a/src/Skeleton.cpp b/src/Skeleton.cpp index a5516c6..8ac7c4f 100644 --- a/src/Skeleton.cpp +++ b/src/Skeleton.cpp @@ -96,106 +96,115 @@ void Skeleton::DoConstraints() void Skeleton::DoConstraints(Model *collide, XYZ *move, float rotation) { - friction=20; - numrepeats=2; - groundlevel=0; - - move->y+=.35; - - for(int j=0; jy += 0.35; + groundlevel = 0; + numrepeats = 2; + for (int j = 0; j < numrepeats; j++) { + for (int i = 0; i < num_joints; i++) { + if (!joints[i].existing + && i != jointlabels[lefthand] + && i != jointlabels[righthand]) + continue; + + // Length constraints joints[i].DoConstraint(); - //Ground constraint - offset=0; - overpoint=joints[i].position; - overpoint.y+=10; - underpoint=joints[i].position; - underpoint.y-=offset; - whichtri=collide->LineCheck2(overpoint,underpoint,&impact,*move,rotation); - if(collide->normals[whichtri].y<=.8)whichtri=collide->LineCheck2(joints[i].realoldposition,joints[i].position,&impact,*move,rotation); - if(joints[i].position.y<=groundlevel+offset||whichtri!=-1){ - if(whichtri==-1||(whichtri!=-1&&collide->normals[whichtri].y>.8)){ - if(whichtri==-1)joints[i].position.y=groundlevel+offset; - if(whichtri!=-1){ - joints[i].position=impact; - joints[i].position.y+=offset; + // Ground constraint + auto& pos = joints[i].position; + overpoint = pos; + overpoint.y += 10; + underpoint = pos; + underpoint.y -= offset = 0; + whichtri = collide->LineCheck2(overpoint, underpoint, + &impact, *move, rotation); + if (whichtri == -1 + || collide->normals[whichtri].y <= 0.8) + whichtri = collide->LineCheck2( + joints[i].realoldposition, pos, + &impact, *move, rotation); + + if (pos.y <= groundlevel + offset || whichtri != -1) { + if (whichtri == -1 + || collide->normals[whichtri].y > 0.8) { + if (whichtri == -1) { + pos.y = groundlevel + offset; + } else { + pos = impact; + pos.y += offset; } - joints[i].velocity.y*=-.3; - joints[i].velocity.x*=.3; - joints[i].velocity.z*=.3; + joints[i].velocity.y *= -0.3; + joints[i].velocity.x *= 0.3; + joints[i].velocity.z *= 0.3; } - offset=.2; - if(whichtri!=-1&&collide->normals[whichtri].y<=.8){ - normalrotated=DoRotation(collide->normals[whichtri],0,rotation,0); - joints[i].position=impact+normalrotated*offset; - ReflectVector(&joints[i].velocity,&normalrotated); - joints[i].velocity*=.3; - } - if(broken<=1){ - XYZ avgvelocity; - avgvelocity=0; - float gLoc[3]; - ALint tempint; - for(int k=0; k2||findLengthfast(avgvelocity)>2)){ - avgvelocity+=joints[i].velocity; - gLoc[0]=joints[i].position.x/soundscalefactor; - gLoc[1]=joints[i].position.y/soundscalefactor; - gLoc[2]=joints[i].position.z/soundscalefactor; -#ifdef DEBIAN_NEEDS_TO_UPDATE_THEIR_OPENAL - alGetSourceiv(gSourceID[headlandsound], AL_SOURCE_STATE, &tempint); -#else - alGetSourcei(gSourceID[headlandsound], AL_SOURCE_STATE, &tempint); -#endif - if (tempint != AL_PLAYING){ - alSourcef(gSourceID[headlandsound], AL_MIN_GAIN, ALfloat(findLengthfast(avgvelocity)*2/findDistancefast(joints[i].position,camera.position)*soundscalefactor*2)); - alSourcef(gSourceID[headlandsound], AL_MAX_GAIN, ALfloat(findLengthfast(avgvelocity)*2/findDistancefast(joints[i].position,camera.position)*soundscalefactor*2)); - alSourcefv(gSourceID[headlandsound], AL_POSITION, gLoc); - alSourcePlay(gSourceID[headlandsound]);} - } - avgvelocity=0; - for(int k=0; knormals[whichtri].y <= 0.8) { + normalrotated = DoRotation(collide->normals[whichtri], 0, rotation, 0); + pos = impact + normalrotated * offset; + ReflectVector(&joints[i].velocity, &normalrotated); + joints[i].velocity *= 0.3; } - avgvelocity/=num_joints; - if((joints[i].label==abdomen)&&(findLengthfast(joints[i].velocity)>2||findLengthfast(avgvelocity)>2)){ - avgvelocity+=joints[i].velocity; - gLoc[0]=joints[i].position.x/soundscalefactor; - gLoc[1]=joints[i].position.y/soundscalefactor; - gLoc[2]=joints[i].position.z/soundscalefactor; -#ifdef DEBIAN_NEEDS_TO_UPDATE_THEIR_OPENAL - alGetSourceiv(gSourceID[bodylandsound], AL_SOURCE_STATE, &tempint); -#else - alGetSourcei(gSourceID[bodylandsound], AL_SOURCE_STATE, &tempint); -#endif - if (tempint != AL_PLAYING){ - alSourcef(gSourceID[bodylandsound], AL_MIN_GAIN, ALfloat(findLengthfast(joints[i].velocity)*1/findDistancefast(joints[i].position,camera.position)*soundscalefactor*2)); - alSourcef(gSourceID[bodylandsound], AL_MAX_GAIN, ALfloat(findLengthfast(joints[i].velocity)*1/findDistancefast(joints[i].position,camera.position)*soundscalefactor*2)); - alSourcefv(gSourceID[bodylandsound], AL_POSITION, gLoc); - alSourcePlay(gSourceID[bodylandsound]);} + + if (broken > 1) + continue; + XYZ avgvelocity {0}; + for (int k = 0; k < num_joints; k++) + avgvelocity += joints[k].velocity; + avgvelocity /= num_joints; + + int landsound = -1; + switch (joints[i].label) { + case head: + landsound = headlandsound; + break; + case abdomen: + landsound = bodylandsound; + break; } + ALint playing = AL_PLAYING; + if (landsound >= 0) + alGetSourcei(gSourceID[landsound], + AL_SOURCE_STATE, &playing); + + if (playing != AL_PLAYING + && (findLengthfast(joints[i].velocity) > 2 + || findLengthfast(avgvelocity) > 2)) { + ALfloat gain = + findLengthfast(avgvelocity + + joints[i].velocity) + / findDistancefast(pos, + camera.position) + * soundscalefactor * 4; + alSourcef(gSourceID[landsound], + AL_MIN_GAIN, gain); + alSourcef(gSourceID[landsound], + AL_MAX_GAIN, gain); + + float gLoc[3] { + pos.x / soundscalefactor, + pos.y / soundscalefactor, + pos.z / soundscalefactor, + }; + alSourcefv(gSourceID[landsound], + AL_POSITION, gLoc); + alSourcePlay(gSourceID[landsound]); } - }} + } } - if(num_muscles) - for(int i=0; i