Пример #1
0
void BallController::updateBalls(std::vector <Ball>& balls, Grid* grid, float deltaTime, int maxX, int maxY) {
    const float FRICTION = 0.01f;
    // Update our grabbed balls velocity
    if (m_grabbedBall != -1) {
        balls[m_grabbedBall].velocity = balls[m_grabbedBall].position - m_prevPos;
    }

    glm::vec2 gravity = getGravityAccel();

    for (size_t i = 0; i < balls.size(); i++) {
        // get handle for less typing
        Ball& ball = balls[i];
        // Update motion if its not grabbed
        if (i != m_grabbedBall) {
            ball.position += ball.velocity * deltaTime;
            // Apply friction
            glm::vec2 momentumVec = ball.velocity * ball.mass;
            if (momentumVec.x != 0 || momentumVec.y != 0) {
                if (FRICTION < glm::length(momentumVec)) {
                    ball.velocity -= deltaTime * FRICTION * glm::normalize(momentumVec) / ball.mass;
                } else {
                    ball.velocity = glm::vec2(0.0f);
                }
            }
            // Apply gravity
            ball.velocity += gravity * deltaTime;
        }
        // Check wall collision
        if (ball.position.x < ball.radius) {
            ball.position.x = ball.radius;
            if (ball.velocity.x < 0) ball.velocity.x *= -1;
        } else if (ball.position.x + ball.radius >= maxX) {
            ball.position.x = maxX - ball.radius - 1;
            if (ball.velocity.x > 0) ball.velocity.x *= -1;
        }
        if (ball.position.y < ball.radius) {
            ball.position.y = ball.radius;
            if (ball.velocity.y < 0) ball.velocity.y *= -1;
        } else if (ball.position.y + ball.radius >= maxY) {
            ball.position.y = maxY - ball.radius - 1;
            if (ball.velocity.y > 0) ball.velocity.y *= -1;
        }

        // Check to see if the ball moved
        Cell* newCell = grid->getCell(ball.position);
        if (newCell != ball.ownerCell) {
            grid->removeBallFromCell(&balls[i]);
            grid->addBall(&balls[i], newCell);
        }
    }
    // Updates all collisions using the spatial partitioning
    updateCollision(grid);

    //// Update our grabbed ball
    if (m_grabbedBall != -1) {
        // Update the velocity again, in case it got changed by collision
        balls[m_grabbedBall].velocity = balls[m_grabbedBall].position - m_prevPos;
        m_prevPos = balls[m_grabbedBall].position;
    }
}
Пример #2
0
//===================================================================
// Function to undo a delete of a company.  Does not allow to undo
// delete of a company that is already in the BST
//===================================================================
void undoDelete(DATA_HEAD *data)
{
    COMPANY* companyNode;
    int isDuplicate;

    //If there is something in the stack
    if(!emptyStack(data->pStack))
    {
        companyNode = (COMPANY*)popStack(data->pStack);

        //Check to see if there is duplicate
        isDuplicate = searchHash(data, companyNode->companyName, companyNode);

        //If there is, print and error, else, insert into the hashed array
        if(isDuplicate == 1)
        {
            printf("ERROR: DUPLICATE DATA\n");
            printf("%s has already been entered into the system\n", companyNode->companyName);
            free(companyNode);
        }
        else
        {
            printf("%s reinserted into the system\n", companyNode->companyName);
            insertManager(data, companyNode);
            updateCollision(data);
            (data->count)++;
            printf("\nNumber Of Data Records: %d\n", data->count);
        }
    }
    else
        printf("Nothing to undo.\n"); //nothing in the stack

    printf("\n");
}
void ForestEditorCtrl::onUndoAction()
{
   if ( mTool )
      mTool->onUndoAction();

   updateCollision();
}
Пример #4
0
bool Forest::onAdd()
{
   if (!Parent::onAdd())
      return false;

   const char *name = getName();
   if(name && name[0] && getClassRep())
   {
      Namespace *parent = getClassRep()->getNameSpace();
      Con::linkNamespaces(parent->mName, name);
      mNameSpace = Con::lookupNamespace(name);
   }

   setGlobalBounds();
   resetWorldBox();

   // TODO: Make sure this calls the script "onAdd" which will
   // populate the object with forest entries before creation.
   addToScene();

   // If we don't have a file name and the editor is 
   // enabled then create an empty forest data file.
   if ( isServerObject() && ( !mDataFileName || !mDataFileName[0] ) )
      createNewFile();
   else
   {
      // Try to load the forest file.
      mData = ResourceManager::get().load( mDataFileName );
      if ( !mData )
      {
         if ( isClientObject() )
            NetConnection::setLastError( "You are missing a file needed to play this mission: %s", mDataFileName );

         return false;
      }
   }

   updateCollision();

   smCreatedSignal.trigger( this );

   if ( isClientObject() )
   {
      mZoningDirty = true;
      SceneZoneSpaceManager::getZoningChangedSignal().notify( this, &Forest::_onZoningChanged );

      ForestWindMgr::getAdvanceSignal().notify( this, &Forest::getLocalWindTrees );
   }

   return true;
}
//takes in vector of all objects
void CollisionManager::update(const BasicObjectStorage* bo_store)
{
    //all obj goes into quadtree
    
    //clears quadtree
    Quadtree::getInstance().clear();
    
    for(auto it : *bo_store)
    {
        //inserts elements
        Quadtree::getInstance().insert(*it);
    }
    
    //gets vector of all obj and possible collisions
    std::map<BasicObject*, BasicObjectStorage> _results;
    
    for(auto it : *bo_store)
    {
        //for all objects gets all possible collisions
        
        //temp storage
        BasicObjectStorage _t_store;
        Quadtree::getInstance().retrieve(_t_store, *it);
        
        //copies to map
        _results[it] = _t_store;
    }
    
    //iterate over objects
    for(auto it : _results)
    {
        //iterate over object collisions
        for(auto itt : it.second)
        {
            if(it.first->getIsMove() && itt->getCollides() )
            {
                updateCollision(it.first, itt);
            }
        }
    }
}
void ForestEditorCtrl::deleteMeshSafe( ForestItemData *mesh )
{
   UndoManager *undoMan = NULL;
   if ( !Sim::findObject( "EUndoManager", undoMan ) )
   {
      Con::errorf( "ForestEditorCtrl::deleteMeshSafe() - EUndoManager not found." );
      return;     
   }

   // CompoundUndoAction which will delete the ForestItemData, ForestItem(s), and ForestBrushElement(s).
   CompoundUndoAction *compoundAction = new CompoundUndoAction( "Delete Forest Mesh" );
    
   // Find ForestItem(s) referencing this datablock and add their deletion
   // to the undo action.
   if ( mForest )
   {      
      Vector<ForestItem> foundItems;
      mForest->getData()->getItems( mesh, &foundItems );

      ForestDeleteUndoAction *itemAction = new ForestDeleteUndoAction( mForest->getData(), this );
      itemAction->removeItem( foundItems );
      compoundAction->addAction( itemAction );
   }

   // Find ForestBrushElement(s) referencing this datablock.
   SimGroup *brushGroup = ForestBrush::getGroup();
   sKey = mesh;
   Vector<SimObject*> foundElements;   
   brushGroup->findObjectByCallback( &findMeshReferences, foundElements );   

   // Add UndoAction to delete the ForestBrushElement(s) and the ForestItemData.
   MEDeleteUndoAction *elementAction = new MEDeleteUndoAction();
   elementAction->deleteObject( foundElements );
   elementAction->deleteObject( mesh );
   
   // Add compound action to the UndoManager. Done.
   undoMan->addAction( compoundAction );

   updateCollision();
}
Пример #7
0
void		Enemy::update(float dt)
{
	updateMovement(dt);
	updateCollision();
}
Пример #8
0
void BallController::updateBalls(std::vector<Ball>& balls, Grid* grid, float deltaTime, int maxX, int maxY)
{
	const float FRICTION = 0.001f;
	if (_grabbedBall != -1)
	{
		balls[_grabbedBall].velocity = balls[_grabbedBall].position - _prePos;
	}

	glm::vec2 gravity = getGravityAccel();

	for (int i = 0; i < balls.size(); i++)
	{
		Ball& ball = balls[i];
		if (i != _grabbedBall)
		{
			ball.position += ball.velocity * deltaTime;

			glm::vec2 momentumVec = ball.velocity * ball.mass;
			if (momentumVec.x != 0 || momentumVec.y != 0)
			{
				if (FRICTION < glm::length(momentumVec))
				{
					ball.velocity -= deltaTime * FRICTION * glm::normalize(momentumVec) / ball.mass;
				}
				else
				{
					ball.velocity = glm::vec2(0.0f);
				}
			}

			ball.velocity += gravity * deltaTime;
		}

		if (ball.position.x < ball.radius)
		{
			ball.position.x = ball.radius;
			if (ball.velocity.x < 0)
			{
				ball.velocity.x *= -1;
			}
		}
		else if (ball.position.x + ball.radius>= maxX)
		{
			ball.position.x = maxX - ball.radius - 1;
			if (ball.velocity.x > 0)
			{
				ball.velocity.x *= -1;
			}
		}

		if (ball.position.y < ball.radius)
		{
			ball.position.y = ball.radius;
			if (ball.velocity.y < 0)
			{
				ball.velocity.y *= -1;
			}
		}
		else if (ball.position.y + ball.radius >= maxY)
		{
			ball.position.y = maxY - ball.radius - 1;
			if (ball.velocity.y > 0)
			{
				ball.velocity.y *= -1;
			}
		}

		Cell* newCell = grid->getCell(ball.position);
		if (newCell != ball.ownerCell)
		{
			grid->removeBallFromCell(&balls[i]);
			grid->addBall(&balls[i], newCell);
		}
	}

	updateCollision(grid);

	if (_grabbedBall != -1)
	{
		balls[_grabbedBall].velocity = balls[_grabbedBall].position - _prePos;
		_prePos = balls[_grabbedBall].position;
	}
}