Example #1
0
//sets the new positon to the given values, informs all other objects about the collision if needed, updates the grid
void Field::updatePosition(const float &xNew, const float &yNew)
{
	if (x != xNew || y != yNew) {
		updateMetrics(metrics, affectedGrids, (int32_t)xNew, (int32_t)yNew);

		applyCollision();

		int32_t newGridX = (int32_t)floor(xNew / GRID_SIZE);
		int32_t newGridY = (int32_t)floor(yNew / GRID_SIZE);

		if (xGridded != newGridX || yGridded != newGridY)
		{
			removeFromMap();

			x = xNew;
			y = yNew;
	
			xGridded = newGridX;
			yGridded = newGridY;

			insertIntoMap();
		}
		else
		{
			x = xNew;
			y = yNew;
	
			xGridded = newGridX;
			yGridded = newGridY;
		}
	}
}
Example #2
0
void AnimatedSprite::checkCollision(float seconds, sf::Vector2f oldPos)
{
    sf::Vector2f newPos = getPosition();


    // Pour chaque objets
    // Collisions avec les objets fixes
    std::vector <FixedSprite*> fss = Moteur2D::getInstance()->getFixedSprites();
    for (unsigned int i = 0; i<fss.size(); i++)
    {
        FixedSprite* fs = fss.at(i);
        if ((*fs)!=(*this) && collideWith(fs))
        {
            // On traite la collision
            applyCollision(fs, oldPos);
            return ;
        }

    }
    // Collisions avec les objets mouvants.
    std::vector <AnimatedSprite*> ass = Moteur2D::getInstance()->getAnimatedSprites();
    for (unsigned int i = 0; i<ass.size(); i++)
    {
        AnimatedSprite* as = ass.at(i);
        if ((*as)!=(*this) && collideWith(as))
        {
            // On traite la collision
            applyCollision(as, oldPos);
            return;
        }
    }
    // Collisions avec les arrière plans concrets.
    std::map <int, ConcreteBackground*> cbs = Moteur2D::getInstance()->getConcreteBackgrounds();
    for (std::map<int,ConcreteBackground*>::iterator it=cbs.begin(); it!=cbs.end(); ++it)
    {
        if ((it->second)->collidedBy(this, newPos))
        {
            // On traite la collision
            m_vit.x=0;
            m_vit.y=0;
            setPosition( oldPos);
            return;
        }
    }
}
Example #3
0
void Game::checkCollisions()
{
    if(Collision::BoundingBoxTest(theCar.getCurrentSprite(), car.getCurrentSprite()))
    {
        std::cout << "Collided!\n";

        applyCollision(theCar, car);
        //double vel1 = theCar.getVelocity();
        //double vel2 = car.getVelocity();

        //int angle1 = theCar.getTravelAngle();
        //int angle2 = car.getTravelAngle();

        //theCar.setTravelAngle(90);
        //theCar.setDriveState(4);
        //theCar.setVelocity(2);
        //car.setVelocity(0);
        //car.setTravelAngle(270);
    }
}