/** * CreateCSphere * * Creates a new collision sphere. */ iCollisionGeometry* CreateCSphere(iPhysics* rb, float radius){ CSphere* sphere = new CSphere(); sphere->_cinfo.radius = radius; sphere->_boundingRadius = radius; sphere->attach(rb); return sphere; }
bool CCollision::SPHEREtoCAPSULE (CSphere* const sphere, CCapsule* const capsule) { XMFLOAT3 closest; if(sphere->GetPos().x < capsule->GetTop().x) closest.x = capsule->GetTop().x; else if(sphere->GetPos().x > capsule->GetBottom().x) closest.x = capsule->GetBottom().x; else closest.x = sphere->GetPos().x; if(sphere->GetPos().y > capsule->GetTop().y) closest.y = capsule->GetTop().y; else if(sphere->GetPos().y < capsule->GetBottom().y) closest.y = capsule->GetBottom().y; else closest.y = sphere->GetPos().y; if(sphere->GetPos().z < capsule->GetTop().z) closest.z = capsule->GetTop().z; else if(sphere->GetPos().z > capsule->GetBottom().z) closest.z = capsule->GetBottom().z; else closest.z = sphere->GetPos().z; CSphere temp; temp.SetPos(closest); temp.SetRad(capsule->GetRad()); return SPHEREtoSPHERE(&temp, sphere); }
bool CCollision::AABBtoCapsule( CAABB* const aabb, CCapsule* const capsule ) { XMFLOAT3 abCenter; abCenter.x = (aabb->GetMin().x + aabb->GetMax().x) * 0.5f; abCenter.y = (aabb->GetMin().y + aabb->GetMax().y) * 0.5f; abCenter.z = (aabb->GetMin().z + aabb->GetMax().z) * 0.5f; XMFLOAT3 L; L.x = capsule->GetTop().x - capsule->GetBottom().x; L.y = capsule->GetTop().y - capsule->GetBottom().y; L.z = capsule->GetTop().z - capsule->GetBottom().z; XMFLOAT3 N; XMStoreFloat3(&N, XMVector3Normalize(XMLoadFloat3(&L))); XMFLOAT3 vec; vec.x = abCenter.x - capsule->GetBottom().x; vec.y = abCenter.y - capsule->GetBottom().y; vec.z = abCenter.z - capsule->GetBottom().z; float d = XMVector3Dot(XMLoadFloat3(&N), XMLoadFloat3(&vec)).m128_f32[0]; XMFLOAT3 closest; if(d < 0) { closest = capsule->GetBottom(); } else if(d > XMVector3Length(XMLoadFloat3(&L)).m128_f32[0]) { closest = capsule->GetTop(); } else { XMFLOAT3 dis = N; dis.x *= d; dis.y *= d; dis.z *= d; closest.x = capsule->GetBottom().x + dis.x; closest.y = capsule->GetBottom().y + dis.y; closest.z = capsule->GetBottom().z + dis.z; } CSphere temp; temp.SetPos(closest); temp.SetRad(capsule->GetRad()); return AABBtoSPHERE(aabb, &temp); }
bool CEnvSphere::bCreateWorld() { m_pCamera = 0; m_hSphere = 0; m_hLight = 0; // Create and setup camera ------------------------------------------------ m_pCamera = new CMyCamera( pGetGraphics() ); if( !m_pCamera->bCreateRenderCamera( iGetWindowWidth(), iGetWindowHeight() ) ) return false; m_pCamera->CalculateProjection( M3D_PI * 0.5f, 10.0f, 0.1f ); m_pCamera->SetPosition( vector3( 0, 0, -2 ) ); m_pCamera->SetLookAt( vector3( 0, 0, 0 ), vector3( 0, 1, 0 ) ); m_pCamera->CalculateView(); // Register triangle-entity and create an instance ------------------------ pGetScene()->RegisterEntityType( "sphere", CSphere::pCreate ); m_hSphere = pGetScene()->hCreateEntity( "sphere" ); if( !m_hSphere ) return false; CSphere *pSphere = (CSphere *)pGetScene()->pGetEntity( m_hSphere ); if( !pSphere->bInitialize( 1.0f, 16, 16, "majestic.cube" ) ) return false; // Create the light ------------------------------------------------------- m_hLight = pGetScene()->hCreateLight(); if( !m_hLight ) return false; CLight *pLight = pGetScene()->pGetLight( m_hLight ); pLight->SetPosition( vector3( 1.5f, 0.25f, 0 ) ); pLight->SetColor( vector4( 1, 1, 0.75f, 1 ) ); // Demonstrating smooth-subdivision to get a round sphere // disable this and increase sphere-tesselation for better preformance (memory-usage will be higher!) pGetGraphics()->SetRenderState( m3drs_subdivisionmode, m3dsubdiv_smooth ); pGetGraphics()->SetRenderState( m3drs_subdivisionlevels, 1 ); pGetGraphics()->SetRenderState( m3drs_subdivisionpositionregister, 0 ); pGetGraphics()->SetRenderState( m3drs_subdivisionnormalregister, 0 ); // using vertex-positions as normals (unit-sphere at origin) return true; }
bool CRayTracer::loadSnowflake (const char *filename) { FILE *f = fopen (filename, "r"); if (!f) return false; // Add the camera looking at the origin camera.setView (VECTOR(2.1, 1.7, 1.3), VECTOR (0,0,0)); camera.setRenderParameters (1024,1024,45); // Define background color background_color = COLOR( 0.078, 0.361, 0.753 ); // Add a two material materials["txt001"]=new CSolidMaterial (COLOR(0.8, 0.6, 0.264),1,0); materials["txt002"]=new CSolidMaterial (COLOR (0.5, 0.45, 0.35), 0.5,0.5); // // Add the ground CPlane *plane = new CPlane (VECTOR(0,1,0), 0.0); plane->setMaterial (materials["txt001"]); //objects.push_back (plane); // This is a very simply parser!! while (!feof(f)) { char buf[512]; fgets (buf, 511, f); if (strncmp (buf, "sphere", 6) == 0) { char material[64]; double x,y,z, rad; sscanf (buf, "sphere %s %lf %lf %lf %lf\n", material, &rad, &x,&y,&z); CSphere *sph = new CSphere(rad); sph->setLocation (VECTOR(x,z,y)); sph->setMaterial (materials["txt002"]); objects.push_back (sph); } } // Add 3 white lights // lights.push_back (new CLight(VECTOR ( 4, 2, 3), COLOR (1,1,1))); lights.push_back (new CLight(VECTOR ( 1, 4,-4), COLOR (1,1,1))); lights.push_back (new CLight(VECTOR (-3, 5, 1), COLOR (1,1,1))); fclose (f); return true; }
//Determines whether the ball has intersected the wall bool CWall::hasIntersected(CSphere& ball){ // There are two ways to determine intersection // |z - z'| <= r | |x - x'| <= r // |z + vzt - z'| <= r | |x + vxt - x'| <= r // After Collision // z = z' - r | x = x' - r D3DXVECTOR3 cord = ball.getCenter(); if (cord.x >= (4.5 - M_RADIUS)) return true; //cord.x = 4.5 - M_RADIUS; if (cord.x <= (-4.5 + M_RADIUS)) return true; //cord.x = -4.5 + M_RADIUS; if (cord.z <= (-3 + M_RADIUS)) return true; //cord.z = -3 + M_RADIUS; if (cord.z >= (3 - M_RADIUS)) return true; //cord.z = 3 - M_RADIUS; return false; }
//----------------------------------------------------------------------------- // initialize the Al object //----------------------------------------------------------------------------- bool CModelBomberman::Init() { //aux vars CSphere *poSphere; float afSpherePos[3]; m_poObject = new C3DObject(); if (!m_poObject->Load("../resources/models/player.obj")) { printf("Problem loading model!"); return false; } m_nTotalAl++; // heade afSpherePos[0] = 0.0f; afSpherePos[1] = 0.65f; afSpherePos[2] = 0.0f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.35f); m_vpoSpheres.push_back(poSphere); // chest afSpherePos[0] = 0.0f; afSpherePos[1] = 0.1f; afSpherePos[2] = 0.0f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.5f); m_vpoSpheres.push_back(poSphere); // right arm afSpherePos[0] = -0.5f; afSpherePos[1] = 0.25f; afSpherePos[2] = -0.1f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.2f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = -0.65f; afSpherePos[1] = 0.12f; afSpherePos[2] = -0.1f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.17f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = -0.72f; afSpherePos[1] = 0.0f; afSpherePos[2] = 0.0f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.13f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = -0.8f; afSpherePos[1] = -0.15f; afSpherePos[2] = 0.1f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.15f); m_vpoSpheres.push_back(poSphere); // left arm afSpherePos[0] = 0.5f; afSpherePos[1] = 0.25f; afSpherePos[2] = -0.1f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.2f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = 0.65f; afSpherePos[1] = 0.12f; afSpherePos[2] = -0.1f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.17f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = 0.72f; afSpherePos[1] = 0.0f; afSpherePos[2] = 0.0f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.13f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = 0.8f; afSpherePos[1] = -0.15f; afSpherePos[2] = 0.1f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.15f); m_vpoSpheres.push_back(poSphere); //right leg afSpherePos[0] = -0.2f; afSpherePos[1] = -0.45f; afSpherePos[2] = 0.025f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.18f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = -0.25f; afSpherePos[1] = -0.6f; afSpherePos[2] = 0.05f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.15f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = -0.25f; afSpherePos[1] = -0.8f; afSpherePos[2] = 0.05f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.13f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = -0.26f; afSpherePos[1] = -0.95f; afSpherePos[2] = 0.17f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.13f); m_vpoSpheres.push_back(poSphere); //right leg afSpherePos[0] = 0.2f; afSpherePos[1] = -0.45f; afSpherePos[2] = 0.025f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.18f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = 0.25f; afSpherePos[1] = -0.6f; afSpherePos[2] = 0.05f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.15f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = 0.25f; afSpherePos[1] = -0.8f; afSpherePos[2] = 0.05f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.13f); m_vpoSpheres.push_back(poSphere); afSpherePos[0] = 0.26f; afSpherePos[1] = -0.95f; afSpherePos[2] = 0.17f; poSphere = new CSphere(); poSphere->Init(afSpherePos, 0.13f); m_vpoSpheres.push_back(poSphere); return true; }
/*-<==>----------------------------------------------------------------- / Defines the scene /----------------------------------------------------------------------*/ void CRayTracer::load () { // Add the camera looking at the origin camera.setView (VECTOR(0, 50, 400), VECTOR (0,0,0)); camera.setRenderParameters (640,480,60); // Define some materials materials["orange"] = new CSolidMaterial (COLOR (1, 0.301, 0.074), 0.1); materials["blue"] = new CSolidMaterial (COLOR (0.0, 0.2, 0.8), 0.0); // materials["red"] = new CSolidMaterial (COLOR (0.8, 0.2, 0.0), 0.5); materials["green"] = new CSolidMaterial (COLOR (0.1, 0.8, 0.0), 0.7); materials["grey"] = new CSolidMaterial (COLOR (0.5, 0.5, 0.5), 0.5); materials["escacs"] = new CCheckerMaterial(materials["red"], materials["green"],100); materials["glass"] = new CTransparentMaterial(COLOR(0.1,0.1,0.2),1.0); // Add a sphere CSphere *sph = new CSphere(50); sph->setLocation (VECTOR(0,-100,0)); sph->setMaterial (materials["grey"]); objects.push_back (sph); // Add a sphere sph = new CSphere(50); sph->setLocation (VECTOR(0,100,0)); sph->setMaterial (materials["blue"]); objects.push_back (sph); // Add a sphere sph = new CSphere(50); sph->setLocation (VECTOR(0,210,0)); sph->setMaterial (materials["red"]); objects.push_back (sph); // Add a sphere sph = new CSphere(50); sph->setLocation (VECTOR(200,50,100)); sph->setMaterial (materials["glass"]); //sph->setMaterial (materials["green"]); objects.push_back (sph); // Add a sphere sph = new CSphere(50); sph->setLocation (VECTOR(200,50,0)); sph->setMaterial (materials["orange"]); objects.push_back (sph); // And now for something completely different // CCylinder* cyl = new CCylinder(100,50); // cyl->setLocation (VECTOR(85,0,120)); // cyl->setMaterial (materials["grey"]); // objects.push_back(cyl); /*CCylinder* cyl = new CCylinder(100,50); cyl->setLocation (VECTOR(150,0,100)); cyl->setMaterial (materials["green"]); objects.push_back(cyl);*/ // Add the ground CPlane *plane = new CPlane (VECTOR(0,1,0), 0); plane->setMaterial (materials["escacs"]); objects.push_back (plane); // Add a single white light CLight *light = new CLight(VECTOR (400,200,50), COLOR (1,1,1)); lights.push_back (light); }
bool CFrustum::SphereInFrustum( const CSphere & sphere ) const { return SphereInFrustum(sphere.GetPosition().x, sphere.GetPosition().y, sphere.GetPosition().z, sphere.GetRadius()); }
//changes x-velocity and z-velocity of the ball after collision void CWall::hitBy(CSphere& ball){ if (!this->hasIntersected(ball)) return; D3DXVECTOR3 cord = ball.getCenter(); if (cord.x >= (4.5 - M_RADIUS)) // 공의 X값이 우측으로 치우친 경우 ( 우측 벽에 부딪힌 경우 ) { ball.setCenter(4.5 - M_RADIUS, cord.y, cord.z); ball.setPower(ball.getVelocity().x*(-0.5), ball.getVelocity().z); // 방향 전환 } if (cord.x <= (-4.5 + M_RADIUS)) // 공의 X값이 좌측으로 치우친 경우 ( 좌측 벽에 부딪힌 경우 ) { ball.setCenter(-4.5 + M_RADIUS, cord.y, cord.z); ball.setPower(ball.getVelocity().x*(-0.5), ball.getVelocity().z); // 방향 전환 } if (cord.z <= (-3 + M_RADIUS)) // 공의 Z값이 아래로 치우친 경우 ( 하측 벽에 부딪힌 경우 ) { ball.setCenter(cord.x, cord.y, -3 + M_RADIUS); ball.setPower(ball.getVelocity().x, ball.getVelocity().z*(-0.5)); // 방향 전환 } if (cord.z >= (3 - M_RADIUS)) // 공의 Z값이 위로 치우친 경우 ( 상측 벽에 부딪힌 경우 ) { ball.setCenter(cord.x, cord.y, 3 - M_RADIUS); ball.setPower(ball.getVelocity().x, ball.getVelocity().z*(-0.5)); // 방향 전환 } }