Beispiel #1
0
void QChain::createChain() {
  std::vector<Vector2d> tmp(m_vertices.begin(), m_vertices.end());
  tmp = Geometry::simplifyPolygon(tmp, 0.2);

  m_vertices = std::vector<QPointF>(tmp.begin(), tmp.end());
  std::vector<QPointF> pts = vertices();

  size_t it = pts.size() - 1, prev = it - 1, next = 1;
  for (size_t i = 0; i < pts.size(); i++) {
    QPointF vec = pts[i] - pts[it];
    QPointF vecp = pts[it] - pts[prev], vecn = pts[next] - pts[i];

    auto fixture = std::make_unique<Box2DEdge>(this);
    fixture->setVisible(false);
    fixture->setShadowCaster(false);

    b2EdgeShape &edge = fixture->edgeShape();
    edge.Set(tob2Vec2(pts[it]), tob2Vec2(pts[i]));

    if (QPointF::dotProduct(vec, vecp) > 0) {
      edge.m_hasVertex0 = true;
      edge.m_vertex0 = tob2Vec2(pts[prev]);
    }
    if (QPointF::dotProduct(vec, vecn) > 0) {
      edge.m_hasVertex3 = true;
      edge.m_vertex3 = tob2Vec2(pts[next]);
    }
    addFixture(std::move(fixture));

    prev = it, it = i;
    next = next + 1 < pts.size() ? next + 1 : 0;
  }

  qreal minx = INF, miny = INF, maxx = -INF, maxy = -INF;
  for (QPointF p : vertices()) {
    minx = std::min(minx, p.x());
    miny = std::min(miny, p.y());
    maxx = std::max(maxx, p.x());
    maxy = std::max(maxy, p.y());
  }

  auto box = std::make_unique<Box2DBox>();
  box->setSize(QSizeF(maxx - minx, maxy - miny));
  box->setPosition(QPointF(minx, miny));
  box->setVisible(false);
  box->setSensor(true);
  addFixture(std::move(box));

  m_texture.setVertices(vertices());
}
Beispiel #2
0
int Doc_Test::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: initTestCase(); break;
        case 1: defaults(); break;
        case 2: addFixture(); break;
        case 3: deleteFixture(); break;
        case 4: fixtureLimits(); break;
        case 5: fixture(); break;
        case 6: findAddress(); break;
        case 7: totalPowerConsumption(); break;
        case 8: addFunction(); break;
        case 9: deleteFunction(); break;
        case 10: function(); break;
        case 11: functionLimits(); break;
        case 12: load(); break;
        case 13: loadWrongRoot(); break;
        case 14: save(); break;
        default: ;
        }
        _id -= 15;
    }
    return _id;
}
Beispiel #3
0
void CzBox2dBody::InitBody(CzBox2dWorld* world, CzShape* body_shape, CzBox2dMaterial* body_mat, CzVec2* pos, float angle, float com_x, float com_y)
{
	if (world == NULL)
		return;

	// Set the box2d world
	World = world;

	b2BodyDef body_def;
	if (body_mat != NULL)
	{
		body_def.type = body_mat->getBodyType();
		body_def.position.Set(World->PixelToWorldX(pos->x), World->PixelToWorldX(pos->y));
		body_def.angle = CzMath::DegToRad(-angle);			// Degrees to radians
		Body = World->getWorld()->CreateBody(&body_def);	
		Body->SetUserData((void*)this);
		Body->SetBullet(body_mat->isBullet());
		Body->SetFixedRotation(body_mat->isFixedRotation());
		if (body_mat->getGravityScale() < 1000000.0f)
			Body->SetGravityScale(body_mat->getGravityScale());
	}
	else
	{
		body_def.type = b2_staticBody;
		body_def.position.Set(World->PixelToWorldX(pos->x), World->PixelToWorldX(pos->y));
		body_def.angle = CzMath::DegToRad(-angle);			// Degrees to radians
		Body = World->getWorld()->CreateBody(&body_def);	
		Body->SetUserData((void*)this);
	}

	if (body_shape != NULL)
		addFixture(body_shape, body_mat, com_x, com_y);
}
void FunctionWizard::slotAddClicked()
{
    FixtureSelection fs(this, _app->doc(), true, fixtureIds());
    if (fs.exec() == QDialog::Accepted)
    {
        QListIterator <quint32> it(fs.selection);
        while (it.hasNext() == true)
            addFixture(it.next());
    }
}
Beispiel #5
0
void Level::readBody(std::fstream& stream, Prototype* game)
{
	b2BodyDef bodyDef;

	bodyDef.type = b2_staticBody;
	bodyDef.position = b2Vec2(0.0f, 0.0f);
	bodyDef.angle = 0;
	bodyDef.fixedRotation = true;

	setBody(&bodyDef, game->world);

	unsigned int numberOfFixtures;
	
	stream.read((char*)&numberOfFixtures, sizeof(numberOfFixtures));

	for (unsigned int i = 0; i < numberOfFixtures; i++)
	{
		b2FixtureDef fixtureDef;

		b2ChainShape shape;

		int vertexCount;
		b2Vec2* vertices;

		stream.read((char*)&vertexCount, sizeof(vertexCount));
		vertices = new b2Vec2[vertexCount];
		stream.read((char*)vertices, vertexCount * sizeof(b2Vec2));

		shape.CreateChain(vertices, vertexCount);

		delete[] vertices;

		fixtureDef.shape = &shape;

		float32 friction;	
		float32 restitution;	
		int		userData;		
		
		stream.read((char*)&friction, sizeof(friction));
		stream.read((char*)&restitution, sizeof(restitution));
		stream.read((char*)&userData, sizeof(userData));
		
		fixtureDef.friction		= friction;
		fixtureDef.restitution	= restitution;
		fixtureDef.userData		= new int(userData);
		
		addFixture(&fixtureDef);

	}


}
Beispiel #6
0
Bullet::Bullet(Circle circle, Item *p) : QBody(p) {
  setPosition(QPointF(circle.pos()));
  setLinearDamping(0);
  setBullet(true);
  setBodyType(Dynamic);

  auto fixture = std::make_unique<Box2DCircle>(this);
  fixture->setShadowCaster(false);
  fixture->setRadius(circle.radius());
  fixture->setGroupIndex(-1);

  addFixture(std::move(fixture));
}
Beispiel #7
0
void StaticLight::initialize(QWorld *w) {
  auto fixture = std::make_unique<Box2DBox>();
  fixture->setPosition(QPointF(-radius(), -radius()));
  fixture->setSize(QSizeF(2 * radius(), 2 * radius()));
  fixture->setSensor(true);
  fixture->setVisible(false);
  fixture->setShadowCaster(false);
  addFixture(std::move(fixture));

  assert(lightSystem());
  lightSystem()->addLight(this);

  QBody::initialize(w);
}
Beispiel #8
0
void Bullet::initialize(QWorld* w) {
  setBodyType(QBody::BodyType::Dynamic);

  auto f = std::make_unique<Box2DBox>();
  QSizeF size(10, 10);
  f->setSize(size);
  f->setPosition(-QPointF(0.5 * size.width(), 0.5 * size.height()));
  f->setTextureSource(":/resources/punch.png");
  f->setSensor(true);
  f->setCollidesWith(
      QFixture::CategoryFlag(QFixture::All & ~QFixture::Category2));
  addFixture(std::move(f));
  setRotation(180.0 / M_PI * atan2(-m_direction.x(), m_direction.y()) + 90.0);
  QBody::initialize(w);
  enqueueFunction(std::bind(&Bullet::onStepped, this));
}
Beispiel #9
0
Player::Player(Item* parent)
    : QBody(parent),
      m_currentPathPoint(),
      m_going(false),
      m_punchSound(std::make_shared<QSound>(":/resources/punch_sound.wav")),
      m_object(this),
      m_score() {
  setBodyType(QBody::BodyType::Dynamic);

  auto circle = std::make_unique<Box2DCircle>();
  circle->setRadius(2.5);
  circle->setFriction(0.0);
  circle->setDensity(1.0);
  circle->setPosition(QPointF(-2.5, -2.5));
  circle->setCategories(QFixture::Category2);
  circle->setCollidesWith(QFixture::Category1);
  circle->setRestitution(0.2);

  addFixture(std::move(circle));

  setPosition(QPointF(70, 950));
  setLinearDamping(5);
  setAngularDamping(5);
}
Beispiel #10
0
bool EFX::loadXML(const QDomElement* root)
{
	QString str;
	QDomNode node;
	QDomElement tag;

	Q_ASSERT(root != NULL);

	if (root->tagName() != KXMLQLCFunction)
	{
		qWarning() << "Function node not found!";
		return false;
	}

	/* Load EFX contents */
	node = root->firstChild();
	while (node.isNull() == false)
	{
		tag = node.toElement();

		if (tag.tagName() == KXMLQLCBus)
		{
			/* Bus */
			str = tag.attribute(KXMLQLCBusRole);
			setBus(tag.text().toInt());
		}
		else if (tag.tagName() == KXMLQLCEFXFixture)
		{
			EFXFixture* ef = new EFXFixture(this);
			ef->loadXML(&tag);
			if (ef->fixture() != KNoID)
			{
				if (addFixture(ef) == false)
					delete ef;
			}
		}
		else if (tag.tagName() == KXMLQLCEFXPropagationMode)
		{
			/* Propagation mode */
			setPropagationMode(stringToPropagationMode(tag.text()));
		}
		else if (tag.tagName() == KXMLQLCEFXAlgorithm)
		{
			/* Algorithm */
			setAlgorithm(tag.text());
		}
		else if (tag.tagName() == KXMLQLCFunctionDirection)
		{
			/* Direction */
			setDirection(Function::stringToDirection(tag.text()));
		}
		else if (tag.tagName() == KXMLQLCFunctionRunOrder)
		{
			/* Run Order */
			setRunOrder(Function::stringToRunOrder(tag.text()));
		}
		else if (tag.tagName() == KXMLQLCEFXWidth)
		{
			/* Width */
			setWidth(tag.text().toInt());
		}
		else if (tag.tagName() == KXMLQLCEFXHeight)
		{
			/* Height */
			setHeight(tag.text().toInt());
		}
		else if (tag.tagName() == KXMLQLCEFXRotation)
		{
			/* Rotation */
			setRotation(tag.text().toInt());
		}
		else if (tag.tagName() == KXMLQLCEFXStartScene)
		{
			/* Start scene */
			setStartScene(tag.text().toInt());

			if (tag.attribute(KXMLQLCFunctionEnabled) ==
			    KXMLQLCTrue)
				setStartSceneEnabled(true);
			else
				setStartSceneEnabled(false);
		}
		else if (tag.tagName() == KXMLQLCEFXStopScene)
		{
			/* Stop scene */
			setStopScene(tag.text().toInt());

			if (tag.attribute(KXMLQLCFunctionEnabled) ==
			    KXMLQLCTrue)
				setStopSceneEnabled(true);
			else
				setStopSceneEnabled(false);
		}
		else if (tag.tagName() == KXMLQLCEFXAxis)
		{
			/* Axes */
			loadXMLAxis(&tag);
		}
		else
		{
			qWarning() << "Unknown EFX tag:" << tag.tagName();
		}
		
		node = node.nextSibling();
	}

	return true;
}
Beispiel #11
0
bool EFX::loadXML(const QDomElement& root)
{
    if (root.tagName() != KXMLQLCFunction)
    {
        qWarning() << "Function node not found!";
        return false;
    }

    if (root.attribute(KXMLQLCFunctionType) != typeToString(Function::EFX))
    {
        qWarning("Function is not an EFX!");
        return false;
    }

    /* Load EFX contents */
    QDomNode node = root.firstChild();
    while (node.isNull() == false)
    {
        QDomElement tag = node.toElement();

        if (tag.tagName() == KXMLQLCBus)
        {
            /* Bus */
            QString str = tag.attribute(KXMLQLCBusRole);
            if (str == KXMLQLCBusFade)
                m_legacyFadeBus = tag.text().toUInt();
            else if (str == KXMLQLCBusHold)
                m_legacyHoldBus = tag.text().toUInt();
        }
        else if (tag.tagName() == KXMLQLCFunctionSpeed)
        {
            loadXMLSpeed(tag);
        }
        else if (tag.tagName() == KXMLQLCEFXFixture)
        {
            EFXFixture* ef = new EFXFixture(this);
            ef->loadXML(tag);
            if (ef->fixture() != Fixture::invalidId())
            {
                if (addFixture(ef) == false)
                    delete ef;
            }
        }
        else if (tag.tagName() == KXMLQLCEFXPropagationMode)
        {
            /* Propagation mode */
            setPropagationMode(stringToPropagationMode(tag.text()));
        }
        else if (tag.tagName() == KXMLQLCEFXAlgorithm)
        {
            /* Algorithm */
            setAlgorithm(stringToAlgorithm(tag.text()));
        }
        else if (tag.tagName() == KXMLQLCFunctionDirection)
        {
            loadXMLDirection(tag);
        }
        else if (tag.tagName() == KXMLQLCFunctionRunOrder)
        {
            loadXMLRunOrder(tag);
        }
        else if (tag.tagName() == KXMLQLCEFXWidth)
        {
            /* Width */
            setWidth(tag.text().toInt());
        }
        else if (tag.tagName() == KXMLQLCEFXHeight)
        {
            /* Height */
            setHeight(tag.text().toInt());
        }
        else if (tag.tagName() == KXMLQLCEFXRotation)
        {
            /* Rotation */
            setRotation(tag.text().toInt());
        }
        else if (tag.tagName() == KXMLQLCEFXStartOffset)
        {
            /* StartOffset */
            setStartOffset(tag.text().toInt());
        }
        else if (tag.tagName() == KXMLQLCEFXAxis)
        {
            /* Axes */
            loadXMLAxis(tag);
        }
        else
        {
            qWarning() << "Unknown EFX tag:" << tag.tagName();
        }

        node = node.nextSibling();
    }

    return true;
}
Beispiel #12
0
bool EFX::loadXML(QXmlStreamReader &root)
{
    if (root.name() != KXMLQLCFunction)
    {
        qWarning() << "Function node not found!";
        return false;
    }

    if (root.attributes().value(KXMLQLCFunctionType).toString() != typeToString(Function::EFX))
    {
        qWarning("Function is not an EFX!");
        return false;
    }

    /* Load EFX contents */
    while (root.readNextStartElement())
    {
        if (root.name() == KXMLQLCBus)
        {
            /* Bus */
            QString str = root.attributes().value(KXMLQLCBusRole).toString();
            if (str == KXMLQLCBusFade)
                m_legacyFadeBus = root.readElementText().toUInt();
            else if (str == KXMLQLCBusHold)
                m_legacyHoldBus = root.readElementText().toUInt();
        }
        else if (root.name() == KXMLQLCFunctionSpeed)
        {
            loadXMLSpeed(root);
        }
        else if (root.name() == KXMLQLCEFXFixture)
        {
            EFXFixture* ef = new EFXFixture(this);
            ef->loadXML(root);
            if (ef->head().isValid())
            {
                if (addFixture(ef) == false)
                    delete ef;
            }
        }
        else if (root.name() == KXMLQLCEFXPropagationMode)
        {
            /* Propagation mode */
            setPropagationMode(stringToPropagationMode(root.readElementText()));
        }
        else if (root.name() == KXMLQLCEFXAlgorithm)
        {
            /* Algorithm */
            setAlgorithm(stringToAlgorithm(root.readElementText()));
        }
        else if (root.name() == KXMLQLCFunctionDirection)
        {
            loadXMLDirection(root);
        }
        else if (root.name() == KXMLQLCFunctionRunOrder)
        {
            loadXMLRunOrder(root);
        }
        else if (root.name() == KXMLQLCEFXWidth)
        {
            /* Width */
            setWidth(root.readElementText().toInt());
        }
        else if (root.name() == KXMLQLCEFXHeight)
        {
            /* Height */
            setHeight(root.readElementText().toInt());
        }
        else if (root.name() == KXMLQLCEFXRotation)
        {
            /* Rotation */
            setRotation(root.readElementText().toInt());
        }
        else if (root.name() == KXMLQLCEFXStartOffset)
        {
            /* StartOffset */
            setStartOffset(root.readElementText().toInt());
        }
        else if (root.name() == KXMLQLCEFXIsRelative)
        {
            /* IsRelative */
            setIsRelative(root.readElementText().toInt() != 0);
        }
        else if (root.name() == KXMLQLCEFXAxis)
        {
            /* Axes */
            loadXMLAxis(root);
        }
        else
        {
            qWarning() << "Unknown EFX tag:" << root.name();
            root.skipCurrentElement();
        }
    }

    return true;
}