void SuperCell::SetBasis(const Vec v[3]) { int i; for (i = 0; i < 3; i++) vectors[i] = v[i]; double determinant = Cross(vectors[0], vectors[1]) * vectors[2]; for (i = 0; i < 3; i++) { inverse[i] = Cross(vectors[(i + 1) % 3], vectors[(i + 2) % 3]); heights[i] = fabs(determinant) / sqrt(Length2(inverse[i])); inverse[i] /= determinant; } int s = 0; int k[3]; for (k[2] = -1; k[2] <= 1; k[2]++) for (k[1] = -1; k[1] <= 1; k[1]++) for (k[0] = -1; k[0] <= 1; k[0]++) { Vec vec(0.0, 0.0, 0.0); for (i = 0; i < 3; i++) vec += vectors[i] * k[i]; translations[s++] = vec; } }
bool Frustum::CheckIfSphereInside(const Vector3<float>& aPosition, float aRadius) const { if (Length2(aPosition - myPosition) < aRadius * aRadius + myRadius* myRadius) { for (int i = 0; i < 6; i++) { if (myPlanes[i].Inside(aPosition, aRadius) == false) { return false; } } return true; } return false; }
Vector3D PointLight::LightIntensity(Scene& scene, IntersectResult intersect, const PerspectiveCamera& cam) { Vector3D delta = mPosition - intersect.mPosition; float rr = Length2(delta); float r = sqrt(rr); float r1 = 1.0 / r; Vector3D inverseDire = Normalize(delta); if (mIsShadow) { Ray shadowRay(intersect.mPosition, inverseDire); IntersectResult shadowResult = scene.Intersect(shadowRay); if (shadowResult.mIsHit && shadowResult.mDistance <= r) { return -(this->mColor * this->mShadowReducingFactor); } } float diffuseAttenuation = 1.0 / (mAttenuation.x + mAttenuation.y * r + mAttenuation.z * rr); float specularAttenuation = 1.0 / (mAttenuation.x + mAttenuation.y * r + mAttenuation.z * rr); Vector3D colorRes; // Diffuse Material float NdotL = Dot(intersect.mNormal, inverseDire); // std::cout << NdotL << std::endl; if (NdotL > 0) { colorRes = colorRes + intersect.mPrimitive->GetDiffuseColor() * this->mColor * NdotL * diffuseAttenuation; } // Specular Material Vector3D Vo = cam.GetPosition() - intersect.mPosition; Vector3D V = Normalize(Vo); Vector3D h = Normalize(V + inverseDire); float NdotH = Dot(intersect.mNormal, h); if (NdotH > 0) { colorRes = colorRes + intersect.mPrimitive->GetSpecularColor() * this->mColor * powf(NdotH, intersect.mPrimitive->GetShininess()) * specularAttenuation; } return colorRes; }
const CommonUtilities::Vector::Vector2<float> GetClosestPointOnLineSegment(const LineSegment& aLine, const CommonUtilities::Vector::Vector2<float>& aPoint) { CommonUtilities::Vector::Vector2<float> AP = aPoint - aLine.GetStartPoint(); //Vector from A to P CommonUtilities::Vector::Vector2<float> AB = aLine.GetEndPoint() - aLine.GetStartPoint(); //Vector from A to B float magnitudeAB = Length2(AB); //Magnitude of AB vector (it's length squared) float ABAPproduct = Dot(AP, AB); //The DOT product of a_to_p and a_to_b float distance = ABAPproduct / magnitudeAB; //The normalized "distance" from a to your closest point if (distance < 0) //Check if P projection is over vectorAB { return aLine.GetStartPoint(); } else if (distance > 1) { return aLine.GetEndPoint(); } else { return aLine.GetStartPoint() + AB * distance; } }
CFfloat CFpoint::Length( void ) const { return sqrt( Length2() ); }
void ColoredCubeApp::updateScene(float dt) { updateGameState(); if(GetAsyncKeyState(VK_ESCAPE) & 0x8000) PostQuitMessage(0); if(gamestate == title) { float rad = 0.0f; camera.update(mTheta,mPhi,rad,0,dt,player,mView,mEyePos,true); maze.update(dt); if(once) { maze.setCeilTex(mfxDiffuseMapVar,mfxSpecMapVar,L"Title Screen.jpg",L"brickwork-bump-map.jpg"); once = false; } ambientLight = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f); //set ceiling texture here } if(gamestate == controls) { float rad = 0.0f; camera.update(mTheta,mPhi,rad,0,dt,player,mView,mEyePos,true); maze.update(dt); if(onceAgain) { maze.setCeilTex(mfxDiffuseMapVar,mfxSpecMapVar,L"Rules Screen.jpg",L"brickwork-bump-map.jpg"); onceAgain = false; } ambientLight = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f); //set ceiling texture here } if(gamestate == level1 || gamestate == level2) { ambientLight = D3DXCOLOR(0.3f, 0.03f, 0.2f, 1.0f); if(onceAgainStart) { maze.setCeilTex(mfxDiffuseMapVar,mfxSpecMapVar,L"13.free-brick-textures.jpg",L"brickwork-bump-map.jpg"); onceAgainStart = false; } if(GetAsyncKeyState('Y') & 0x8000) perspective = true; else perspective = false; if(oldBLevel!=0 && flashLightObject.getPowerLevel()<=0) { audio->playCue(BATTERY_DIE); } oldBLevel = flashLightObject.getPowerLevel(); //check for win game conditions auto oldP = player.getPosition(); timer -= dt; std::wostringstream outs; //update the camera camera.update(mTheta,mPhi,mRadius,0,dt,player,mView,mEyePos,perspective); //move the player camera.movePlayer(player,30,camera.getTarget(),perspective); player.update(dt); Location playerLoc; playerLoc.x = player.getPosition().x; playerLoc.z = player.getPosition().z; //collision detection if(player.getPosition()!=oldP) { if(maze.collided(playerLoc)) { player.setPosition(oldP); player.setVelocity(Vector3(0,0,0)); player.update(dt); } } for(int i = 0; i < numLightObjects; i++) { lamps[i].update(dt); } for(int i = 0; i < numBatteries; i++) { batteries[i].update(dt); if(player.collided(&batteries[i])) { batteries[i].setInActive(); flashLightObject.getBattery(); audio->playCue(BATTERY_CHARGE); } } if(gamestate == level1) { for(int i = 0; i < totalKeys; i++) { keyObject[i].update(dt); if(player.collided(&keyObject[i])) { currentKeys++; keyObject[i].setInActive(); audio->playCue(ITEM); } } } if(gamestate==level2) { for(int i = 0; i < ghosts.getNumEnemies(); i++) { if(flashLightObject.hitTarget(&ghosts.getEnemies()[i])) { ghosts.getEnemies()[i].decreaseHealth(); audio->playCue(G_HIT); } } } if(gamestate==level2) { if(player.collided(&endCube)) { gamestate = win; maze.setCeilTex(mfxDiffuseMapVar,mfxSpecMapVar,L"You Win.jpg",L"brickwork-bump-map.jpg"); maze.update(dt); } } if(player.getHealth()<=0) { gamestate = gameover; maze.setCeilTex(mfxDiffuseMapVar,mfxSpecMapVar,L"You Lose.jpg",L"brickwork-bump-map.jpg"); maze.update(dt); } endCube.update(dt); lights[1].ambient = ambientLight; if(gamestate == level2) { for(int i = 0; i < ghosts.getNumEnemies(); i++) { //player gets hit by a ghost if(player.collided(&ghosts.getEnemies()[i])) { player.setHealth(player.getHealth()-1); lights[1].ambient = hurtLight; //make flash take longer ghosts.getEnemies()[i].setInActive(); audio->playCue(P_HIT); } } } maze.update(dt); if(flashLightObject.getPosition()!=(player.getPosition()+(camera.getTarget()*5))) { flashLightObject.setPosition(player.getPosition() + camera.getTarget()*5); int i = 0; } //orientating the flashlight if(flashLightObject.lightSource.dir!=camera.getTarget()) { //vectors for caluclating z-rotation Vector2 cameraXY = Vector2(camera.getTarget().x,camera.getTarget().y); Vector2 startXY = Vector2(flashLightObject.lightSource.dir.x,flashLightObject.lightSource.dir.y); //vectors for calculating y-rotation Vector2 cameraXZ = Vector2(camera.getTarget().x,camera.getTarget().z); Vector2 startXZ = Vector2(flashLightObject.lightSource.dir.x,flashLightObject.lightSource.dir.z); //vectors for calculating x-rotation Vector2 cameraYZ = Vector2(camera.getTarget().y,camera.getTarget().z); Vector2 startYZ = Vector2(flashLightObject.lightSource.dir.y,flashLightObject.lightSource.dir.z); float xAngle = flashLightObject.getRotation().x; float yAngle = flashLightObject.getRotation().y; float zAngle = flashLightObject.getRotation().z; float topEquation; float bottomEquation; topEquation = Dot2(&cameraXY,&startXY); bottomEquation = Length2(&cameraXY)*Length2(&startXY); if(bottomEquation>0) { zAngle+=acos((topEquation/bottomEquation)); } topEquation = Dot2(&cameraXZ,&startXZ); bottomEquation = Length2(&cameraXZ)*Length2(&startXZ); if(bottomEquation>0) { yAngle+=acos((topEquation/bottomEquation)); } topEquation = Dot2(&cameraYZ,&startYZ); bottomEquation = Length2(&cameraYZ)*Length2(&startYZ); if(bottomEquation>0) { xAngle+=acos((topEquation/bottomEquation)); } flashLightObject.setRotation(Vector3(xAngle,yAngle,zAngle)); flashLightObject.lightSource.dir = camera.getTarget(); } flashLightObject.update(dt); //batteryObject.update(dt); ghosts.update(dt,&player); //lightObject1.update(dt); /*floor.update(dt); wall1.update(dt); wall2.update(dt); wall3.update(dt); wall4.update(dt);*/ //flashLightObject.setRotation( /*if(player.collided(&batteryObject)) { flashLightObject.getBattery(); }*/ //mParallelLight.pos = testCube.getPosition(); //set up the flashlight light direction based on the direction the geometry is pointing //D3DXVec3Normalize(&mParallelLight.dir, &(playerCamera.getTarget()-testCube.getPosition())); outs.precision(2); outs << L"Health: " << player.getHealth() << L"\n"; outs.precision(3); outs << "Battery: " << flashLightObject.getPowerLevel(); mTimer = outs.str(); } if(gamestate == gameover) { float rad = 0.0f; camera.update(mTheta,mPhi,rad,0,dt,player,mView,mEyePos,true); //set ceiling texture here if(onceAgainEnd) { maze.setCeilTex(mfxDiffuseMapVar,mfxSpecMapVar,L"Rules Screen.jpg",L"brickwork-bump-map.jpg"); onceAgainEnd = false; onceAgainStart = true; } } }