示例#1
0
//************************************************************************
void CNutDropScene::OnSNotify (LPSPRITE lpSprite, SPRITE_NOTIFY Notify)
//************************************************************************
{
	if (! lpSprite)
		return;

	if (Notify == SN_MOVED)
	{
		// Check for collision
		if (lpSprite != m_lpPlayerSprite)
			CheckObjectCollision (lpSprite);
	}
	else
	if (Notify == SN_MOVEDONE)
	{
		if (m_lpPlayerSprite && lpSprite == m_lpPlayerSprite)
			m_lpPlayerSprite->SetCellsPerSec (0);
	}
}
void RigidBodySimulator::Simulate(){
	//
	//check item number
	if (pObjectList.size() == 0 || pForceList.size() == 0){ //not enough items
		//cannot preceed
		std::cerr << "not enough object/force!" << std::endl;
	}

	else{               // able to simulate
		//traverse objects
		for (auto item : pObjectList){
			//traverse forces
			isCollide = false;
			//collision detection
			for (auto other_item : pObjectList){
				
				if (other_item == item) //same item
					continue;
				else{
					//check coll8ison 
					if (CheckObjectCollision(item, other_item)){
						//handle collision
						HandleCollision(item, other_item);
						isCollide = true;
					}
				}
			}//end travere other objects

			//sumarize all forces
			//if (item->getALLForces().size() == 0){ //check if no force apply
			//	continue;
			//}
			
			std::vector<Physical_Force*>forces = item->getALLForces();
			Physical_Force totalForce = *forces[0];
			if (forces.size() > 1){
				for (int i = 0; i < forces.size(); i++){
					if (i = 0){
						continue;
					}
					totalForce += *forces[i];
				}
			}
			
			//if (haveGravity){ //gravity applied
			//	totalForce += Physical_Force(Vec3f(0.0, 0.0, -9.8)*item->GetMass());
			
			//for (auto force : item->getALLForces()){
			//	//
			//	totalForce += *force;
			//}
			//update accleration ///////////////////////
			
			if (true/*!isCollide*/){  //applyforce only when no collision
				Acceleration acclerate = Acceleration(&totalForce, item->GetMass());

				//update velocity ////////////////////////////
				Vec3f inspedct = (acclerate*dt).getunifiedVector();
				Velocity vincre = acclerate*dt;
				//Velocity vincre = Velocity(inspedct);
				//Velocity i = item->GetVelovity() + vincre;
				item->SetVelocity(item->GetVelovity() + vincre);
			}
			

			//update transition //////////////////////////////////
			Vec3f dis = item->GetVelovity().getunifiedVector()*dt;
			//item->Translate(item->GetVelovity()*dt);
			item->Translate(dis);
			item->Rotation(0.5);
			
		}
	}
}