Exemplo n.º 1
0
Arquivo: Shape.cpp Projeto: 4ian/SFML
void Shape::update()
{
    // Get the total number of points of the shape
    std::size_t count = getPointCount();
    if (count < 3)
    {
        m_vertices.resize(0);
        m_outlineVertices.resize(0);
        return;
    }

    m_vertices.resize(count + 2); // + 2 for center and repeated first point

    // Position
    for (std::size_t i = 0; i < count; ++i)
        m_vertices[i + 1].position = getPoint(i);
    m_vertices[count + 1].position = m_vertices[1].position;

    // Update the bounding rectangle
    m_vertices[0] = m_vertices[1]; // so that the result of getBounds() is correct
    m_insideBounds = m_vertices.getBounds();

    // Compute the center and make it the first vertex
    m_vertices[0].position.x = m_insideBounds.left + m_insideBounds.width / 2;
    m_vertices[0].position.y = m_insideBounds.top + m_insideBounds.height / 2;

    // Color
    updateFillColors();

    // Texture coordinates
    updateTexCoords();

    // Outline
    updateOutline();
}
Exemplo n.º 2
0
//Move each point by velocity * dt
void Convex::move(sf::Time* dt)
{
	for (int i = 0; i < getPointCount(); i++)
	{
		setPoint(i, getPoint(i) + _velocity * dt->asSeconds());
	}
}
Exemplo n.º 3
0
/*
	Checks the position of every point of the vertex, if
	any of them are outside of the window and still heading
	there changes their direction

	momentOfInertia = mass * r^2
*/
void Convex::borderCollision(sf::RenderWindow* window)
{
	float impulse;
	float Px, Py;

	for (int i = 0; i < getPointCount(); i++)
	{
		
		if (getPoint(i).x < 0 && _velocity.x < 0)
		{
			sf::Vector2f collisionNormal(1, 0);
			applyChanges(i, &collisionNormal);
		}

		if (getPoint(i).x > window->getSize().x && _velocity.x > 0)
		{
			sf::Vector2f collisionNormal(-1, 0);
			applyChanges(i, &collisionNormal);
		}

		if (getPoint(i).y < 0 && _velocity.y < 0)
		{
			sf::Vector2f collisionNormal(0, 1);
			applyChanges(i, &collisionNormal);
		}

		if (getPoint(i).y > window->getSize().y && _velocity.y > 0)
		{
			sf::Vector2f collisionNormal(0, -1);
			applyChanges(i, &collisionNormal);
		}

		_momentOfInertia = _mass;
	}
}
Exemplo n.º 4
0
bool MgBaseShape::_offset(const Vector2d& vec, int)
{
    for (int i = 0; i < getPointCount(); i++) {
        setPoint(i, getPoint(i) + vec);
    }
    update();
    return true;
}
Exemplo n.º 5
0
sf::Vector2f CenterCircle::getPoint(unsigned int index) const {
	static const float pi = 3.141592654f;
	float angle = index * 2 * pi / getPointCount();

	float x = std::cos(angle) * radius;
	float y = std::sin(angle) * radius;

	return sf::Vector2f(x, y);	
}
Exemplo n.º 6
0
 virtual sf::Vector2f getPoint(size_t index) const{
     static const float pi = 3.141592654f;
     
     float angle = index * 2 * pi / getPointCount() - pi / 2;
     float x = std::cos(angle) * m_radius.x;
     float y = std::sin(angle) * m_radius.y;
     
     return sf::Vector2f(m_radius.x + x, m_radius.y + y);
 }
Exemplo n.º 7
0
//-------------------------------------------------------------
void ConeGeometry::init(DolfinGui *ui)
{
    setPointCount(6);
    setRadiusCount(2);
    setCreated(false);
    setGuiWindow(ui);
    setPoints(new double[getPointCount()]);
    setPoints(new double[getRadiusCount()]);
    setMyType("Cone");
}
Exemplo n.º 8
0
/************************************
 * Function name: Histogram::getMean
 * Description:   Returns the arithmetic mean of the values in the
 *  histogram
 * Arguments:     int32_t & mean - the mean
 *                const bool onlyAboveZero - only count values above zero,
 *  assumes accumulation/increment value has never been negative
 * Return value:  bool
 */
bool Histogram::getMean ( double & mean,
                          const bool onlyAboveZero ) {
  COUNTER
  ptCount;

  getPointCount ( ptCount, onlyAboveZero );

  if ( ptCount == 0 )
    mean = 0;
  else
    mean = (double)accumulation / (double)ptCount;

  return false;
}
Exemplo n.º 9
0
/*
	Rotates every point of the convex around its center of mass
*/
void Convex::rotateAroundMassCenter(sf::Time* dt)
{
	sf::Vector2f temp;

	for (int i = 0; i < getPointCount(); i++)
	{
		temp = getPoint(i);
		temp -= _massCenter;
		temp.x = (getPoint(i).x - _massCenter.x) * cos(_angularVelocity * dt->asSeconds()) - (getPoint(i).y - _massCenter.y) * sin(_angularVelocity * dt->asSeconds());
		temp.y = (getPoint(i).x - _massCenter.x) * sin(_angularVelocity * dt->asSeconds()) + (getPoint(i).y - _massCenter.y) * cos(_angularVelocity * dt->asSeconds());
		temp += _massCenter;
		setPoint(i, temp);
	}
}
Exemplo n.º 10
0
bool NCollisionComponent::contains(sf::Vector2f const& point)
{
    sf::Vector2f p = point - getFinalPosition();
    for (std::size_t i = 0; i < mPoints.size(); i++)
    {
        sf::Vector2f a = getPoint(i);
        sf::Vector2f b = (i == getPointCount() - 1) ? getPoint(0) - a : getPoint(i + 1) - a;
        if (b.x * (p.y - a.y) - b.y * (p.x - a.x) < 0)
        {
            return false;
        }
    }
    return true;
}
Exemplo n.º 11
0
unsigned int sfShape_getPointCount(const sfShape* shape)
{
    CSFML_CALL_RETURN(shape, getPointCount(), 0);
}
Exemplo n.º 12
0
	//-----------------------------------------------------------------------	
	const Vector3& FocusedShadowCameraSetup::PointListBody::getPoint(size_t cnt) const
	{
		OgreAssert(cnt >= 0 && cnt < getPointCount(), "Search position out of range");

		return mBodyPoints[ cnt ];
	}
Exemplo n.º 13
0
int MgBaseShape::_getHandleCount() const
{
    return getPointCount();
}
Exemplo n.º 14
0
/*
	Calculates the coordinates of the collision point
*/
void Convex::convexCollision(Convex* other, sf::RenderWindow* window)
{
	sf::Vector2f collisionNormal;
	float collisionX, collisionY;
	float x1, x2, x3, x4, y1, y2, y3, y4;
	

	if (hasCollided(other))
	{
		for (int i = 0; i < getPointCount(); i++)
		{
			//Retrieves the coordinates of the point i
			x1 = this->getPoint(i).x;
			y1 = this->getPoint(i).y;

			//If the index 'i' equals the amount of points
			//the other end of the line should be the starting point
			if (i == getPointCount() - 1)
			{
				x2 = this->getPoint(0).x;
				y2 = this->getPoint(0).y;
			}
			else
			{
				x2 = this->getPoint(i + 1).x;
				y2 = this->getPoint(i + 1).y;
			}


			for (int j = 0; j < other->getPointCount(); j++)
			{
				x3 = other->getPoint(j).x;
				y3 = other->getPoint(j).y;

				//If the index 'j' equals the amount of points
				//the other end of the line should be the starting point
				if (i == getPointCount() - 1)
				{
					x4 = other->getPoint(0).x;
					y4 = other->getPoint(0).y;
				}
				else
				{
					x4 = other->getPoint(i + 1).x;
					y4 = other->getPoint(i + 1).y;
				}

				collisionX = vectorProduct(vectorProduct(x1, y1, x2, y2),
							 vectorProduct(x1, 1, x2, 1),
							 vectorProduct(x3, y3, x4, y4),
							 vectorProduct(x3, 1, x4, 1))
							 /
							 vectorProduct(vectorProduct(x1, 1, x2, 1),
							 vectorProduct(y1, 1, y2, 1),
							 vectorProduct(x3, 1, x4, 1),
							 vectorProduct(y3, 1, y4, 1));

				collisionY = vectorProduct(vectorProduct(x1, y1, x2, y2),
							 vectorProduct(y1, 1, y2, 1),
							 vectorProduct(x3, y3, x4, y4),
							 vectorProduct(y3, 1, y4, 1))
							 /
							 vectorProduct(vectorProduct(x1, 1, x2, 1),
							 vectorProduct(y1, 1, y2, 1),
							 vectorProduct(x3, 1, x4, 1),
							 vectorProduct(y3, 1, y4, 1));
				
				//Checking that the collision point is actually between the corners
				//of the convexes
				if (isBetween(collisionX, x1, x2) && isBetween(collisionX, x3, x4) &&
					isBetween(collisionY, y1, y2) && isBetween(collisionY, y3, y4))
				{
					//If the collision point is between these points use them to calculate the
					//collisionNormal's components
					if (isBetween(collisionX, x1, x2) && isBetween(collisionY, y1, y2))
					{
						float x = x2 - x1;
						float y = y2 - y1;
						
						float length = sqrt(pow(x, 2) + pow(y, 2));
						float unitX = x / length;
						float unitY = y / length;

						collisionNormal.x = -unitY;
						collisionNormal.y = unitX;

						float impulse = getImpulse(&sf::Vector2f(collisionX, collisionY), &collisionNormal, other, this);
						applyChanges(&collisionNormal, impulse, &sf::Vector2f(collisionX, collisionY), other, this);
					}
					else
					{
						float x = x4 - x3;
						float y = y4 - y3;

						float length = sqrt(pow(x, 2) + pow(y, 2));
						float unitX = x / length;
						float unitY = y / length;

						collisionNormal.x = -unitY;
						collisionNormal.y = unitX;

						float impulse = getImpulse(&sf::Vector2f(collisionX, collisionY), &collisionNormal, this, other);
						applyChanges(&collisionNormal, impulse, &sf::Vector2f(collisionX, collisionY), this, other);
					}
					
					sf::CircleShape circle(5);
					circle.setFillColor(sf::Color::Red);
					circle.setPosition(collisionX, collisionY);
					window->draw(circle);
				}
			}
		}
	}
}
Exemplo n.º 15
0
unsigned int SonarPointCloud::getPreviewPointCount()
{
	return getPointCount() / m_iPreviewReductionFactor;
}
Exemplo n.º 16
0
	const DiVec3& DiFocusedShadowPolicy::PointListBody::getPoint(size_t cnt) const
	{
		DI_ASSERT_MESSAGE(cnt < getPointCount(), "Search position out of range");

		return mBodyPoints[ cnt ];
	}
Exemplo n.º 17
0
void RenderSystem::update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt)
{
    m_renderingQueue.clear();

    if(m_debugHitboxDraw)
    {
        es.each<PositionComponent, PlatformerHitboxComponent>([&](
            entityx::Entity entity,
            PositionComponent& position,
            PlatformerHitboxComponent& hitbox) {
            auto shape = std::make_shared<sf::ConvexShape>(hitbox.getHitbox().getPointsCount());
            shape->setOutlineThickness(1.f);
            shape->setFillColor(sf::Color::Transparent);
            shape->setOutlineColor(sf::Color::Black);
            for(std::size_t i = 0; i < shape->getPointCount(); ++i)
            {
                shape->setPoint(i, sf::Vector2f(
                    hitbox.getHitbox().getPoint(i).x,
                    hitbox.getHitbox().getPoint(i).y
                ));
            }

            addToRenderingQueue(shape, sf::RenderStates(position.getPositionTransform()), 100000.f);
        });
    }

    auto drawFunc = [&](entityx::Entity entity, PositionComponent& position, RenderComponent& render) {
        //Update the animated sprite and put it in the render queue
        auto animatedSprite = getAnimatedSprite(entity);

        animatedSprite->update(dt);
        animatedSprite->setOrigin(sf::Vector2f(0.5f, 0.5f));
        animatedSprite->setPosition(position.x + position.width/2.f, position.y + position.height/2.f);
        animatedSprite->setScale((render.flipped ? (-1) : (1)) * position.width, position.height);

        //Call the lua callback if the animation has just been restarted
        if(animatedSprite->hadRestartedAnimation() && render.onAnimationEndFunc.valid())
            render.onAnimationEndFunc.call(EntityHandle(entity), render.currentAnimation);

        addToRenderingQueue(animatedSprite, sf::RenderStates::Default, position.z); //TODO: Get z position from RenderComponent
    };

    sf::FloatRect viewAABB(
        m_renderingView.getCenter().x - 1024.f/2.f,
        m_renderingView.getCenter().y - 768.f/2.f,
        1024.f,
        768.f
    );

    std::set<entityx::Entity> entitiesToDraw = m_grid.getEntitiesIntersectingAABB(viewAABB);
    for(entityx::Entity entity : entitiesToDraw)
    {
        if(entity.has_component<PositionComponent>() && entity.has_component<RenderComponent>())
        {
            drawFunc(entity, *(entity.component<PositionComponent>().get()), *(entity.component<RenderComponent>().get()));
        }
    }

    if(m_cameraFollowPlayers)
    {
        //Update the camera
        es.each<PlayerComponent, PlatformerComponent, PositionComponent>([&](entityx::Entity entity, PlayerComponent& player, PlatformerComponent& platformer, PositionComponent& position) {
            if(player.playerNumber != m_centerOnPlayer)
                return; //Only center on the selected player

            float newX = position.x + position.width / 2.f;
            float newY = m_renderingView.getCenter().y;

            if(platformer.groundEntity && platformer.groundEntity.has_component<PositionComponent>())
            {
                m_lastGroundEntity = platformer.groundEntity;
            }

            if(m_lastGroundEntity)
            {
                auto groundPosition = m_lastGroundEntity.component<PositionComponent>();

                //If the player is on the ground, update to Y position.
                if(std::abs(newY - groundPosition->y) > 10.f)
                {
                    newY += (newY > groundPosition->y ? -1 : 1) * 150.f * dt;
                }

                //If the player goes beyond some limits, remove the last ground entity and try to center on player
                if(position.y + position.height > newY + 200.f || position.y + position.height < newY - 200.f)
                {
                    m_lastGroundEntity = entityx::Entity();
                }
            }
            else
            {
                newY += (newY > position.y ? -1 : 1) * std::abs(newY - position.y) * 2 * dt + (platformer.fallingSpeed - platformer.jumpingSpeed) * dt;
            }

            m_renderingView.setCenter(sf::Vector2f(newX, newY));
        });
    }
}