예제 #1
0
파일: Map.cpp 프로젝트: jnicholl/AIMaze
void Map::parentHeightChanged(float parentHeight)
{
	const float padding = 10; // Padding of at least 10 on all sides.
	parentHeight = parentHeight - padding*2;
	const float cellWidth = m_mapWidth / m_cols;
	const float cellHeight = parentHeight / m_rows;
	float oldCellSize = m_cellSize;
	m_cellSize = std::min(cellWidth, cellHeight);
	m_mapArea->setPreferredSize(m_cellSize * m_cols, m_cellSize * m_rows);

	QObjectList list = m_mapArea->children();
	if (m_rows * m_cols + 1 != list.count()) { qDebug("Uhoh: List size not correct!"); }

	int i=0;
	for (int y=0; y<m_rows; y++) {
		for (int x=0; x<m_cols; x++) {
			ImageView *cell = qobject_cast<ImageView*>(list[i]);
			if (cell) {
				cell->setPreferredSize(m_cellSize, m_cellSize);
				AbsoluteLayoutProperties *properties = static_cast<AbsoluteLayoutProperties*>(cell->layoutProperties());
				properties->setPositionX(x * m_cellSize);
				properties->setPositionY(y * m_cellSize);
			}
			i++;
		}
	}

	Container *robot = qobject_cast<Container*>(list[i]);
	if (robot) {
		robot->setPreferredSize(m_cellSize, m_cellSize);
		AbsoluteLayoutProperties *properties = static_cast<AbsoluteLayoutProperties*>(robot->layoutProperties());
		properties->setPositionX(properties->positionX() * m_cellSize / oldCellSize);
		properties->setPositionY(properties->positionY() * m_cellSize / oldCellSize);
	}
}
예제 #2
0
void ColorWheelView::selectColor(float x, float y)
{
    AbsoluteLayoutProperties* selectorLayout =
            dynamic_cast<AbsoluteLayoutProperties*>(selectorImageView->layoutProperties());

    selectorLayout->setPositionX(x - (0.50 * selectorImageView->preferredWidth()));
    selectorLayout->setPositionY(y - (0.50 * selectorImageView->preferredHeight()));

    AbsoluteLayoutProperties* wheelLayout =
            dynamic_cast<AbsoluteLayoutProperties*>(wheelImageView->layoutProperties());

    float radius = 0.50 * wheelImageView->preferredWidth();
    float centerX = wheelLayout->positionX() + radius;
    float centerY = wheelLayout->positionY() + radius;

    int _x = x - centerX;
    int _y = y - centerY;

    float angle = atan2(_y, _x) * (180 / M_PI);
    angle = (int) (angle + 360) % 360;

    float s = distance(x, y, centerX, centerY) / radius;

    colorPickerView->setColor(angle, s);
}
예제 #3
0
//! [0]
Player::Player(Board *board, QObject *parent)
    : QObject(parent)
    , m_board(board)
    , m_playerTile(new ImageView)
    , m_currentDirection(Up)
    , m_currentPosition(0, 0)
    , m_currentAnimation(0)
{

    // Initialise the player tile and add it to the board container
    m_playerTile->setPreferredWidth(s_tileSize);
    m_playerTile->setPreferredHeight(s_tileSize);
    m_playerTile->setImage(Image(QUrl("asset:///images/player.png")));
    m_board->board()->add(m_playerTile);

    // Ensure that the x/y position is really 0,0 otherwise using the
    // translationX/translationY properties does not work as expected.
    AbsoluteLayoutProperties *props
        = qobject_cast<AbsoluteLayoutProperties*>(m_playerTile->layoutProperties());

    if (props) {
        props->setPositionX(0);
        props->setPositionY(0);
    }

}
예제 #4
0
void Bbmoviez::physics () {

	b2Body* m_bodies[e_count];

	// Define the gravity vector.
	b2Vec2 gravity(0.0f, -10.0f);

	// Construct a world object, which will hold and simulate the rigid bodies.
	b2World m_world(gravity);

	b2BodyDef bd;
	b2Body* ground = m_world.CreateBody(&bd);
	{
		b2EdgeShape shape;
		shape.Set(b2Vec2(-15.0f, -5.3f), b2Vec2(15.5f, -5.3f));
		ground->CreateFixture(&shape, 0.0f);
		shape.Set(b2Vec2(-15.0f, 80.0f), b2Vec2(15.5f, 80.0f));
		ground->CreateFixture(&shape, 0.0f);
		shape.Set(b2Vec2(-15.0f, -5.3f), b2Vec2(-15.0f, 80.0f));
		ground->CreateFixture(&shape, 0.0f);
		shape.Set(b2Vec2(15.5f, -5.3f), b2Vec2(15.5f, 80.0f));
		ground->CreateFixture(&shape, 0.0f);
	}

	// Define another box shape for our dynamic body.
	b2CircleShape shape;
	// Define the dynamic body fixture.
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &shape;

	// Set the box density to be non-zero, so it will be dynamic.
	fixtureDef.density = 1.0f;

	// Override the default friction.
	fixtureDef.friction = 0.3f;
	float r, x, y;
	float from[2], to[2];
	AbsoluteLayoutProperties* pProperties = AbsoluteLayoutProperties::create();
	for (int32 i = 0; i < e_count; ++i) {
		b2BodyDef bd;
		bd.type = b2_dynamicBody;
		shape.m_radius = rand() % 6 * 1.0f + 1;
		from[0] = to[0] = 0;
		from[1] = 6; to[1] = 768/2;
		float cs = convertToRange(shape.m_radius, from, to);
		QmlDocument *qml = QmlDocument::create("asset:///controls/Node.qml");
		Container* c = qml->createRootObject<Container>();
		m_containerList.append(c);
		c->setPreferredSize(cs, cs);
		qobject_cast<Container*>(m_nodePane->content())->add(c);
		x=r * (r < 3 ? -1 : 1);
		bd.position.Set(x, 70.0f);
		from[0] = -15; from[1] = 15.5;
		to[0] = 0; to[1] = 768;
		pProperties->setPositionX(convertToRange(x, from, to));
		pProperties->setPositionY(0);
		c->setLayoutProperties(pProperties);

		m_bodies[i] = m_world.CreateBody(&bd);

		m_bodies[i]->CreateFixture(&fixtureDef);

		m_bodies[i]->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
	}


	// Prepare for simulation. Typically we use a time step of 1/60 of a
	// second (60Hz) and 10 iterations. This provides a high quality simulation
	// in most game scenarios.
	float32 timeStep = 1.0f / 60.0f;
	int32 velocityIterations = 6;
	int32 positionIterations = 2;

	// This is our little game loop.
	for (int32 i = 0; i < 60 * 3; ++i) {
		// Instruct the world to perform a single step of simulation.
		// It is generally best to keep the time step and iterations fixed.
		m_world.Step(timeStep, velocityIterations, positionIterations);

		for (int32 j = 0; j < e_count; ++j) {
			// Now print the position and angle of the body.
			b2Vec2 position = m_bodies[j]->GetPosition();
			float32 angle = m_bodies[j]->GetAngle();
			//printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
			from[0] = -15; from[1] = 15.5;
			to[0] = 0; to[1] = 768;
			pProperties->setPositionX(convertToRange(position.x, from, to));
			from[0] = -5.3; from[1] = 80;
			to[0] = 0; to[1] = 1280;
			pProperties->setPositionY(convertToRange(position.y, from, to));
			m_containerList[j]->setLayoutProperties(pProperties);
		}
	}
	delete pProperties;

	// When the world destructor is called, all bodies and joints are freed. This can
	// create orphaned pointers, so be careful about your world management.

	return;
}