Esempio n. 1
0
float Collider::angleBetweenVectors(QVector2D v1, QVector2D v2)
{
    float dot = QVector2D::dotProduct(v1,v2);
    float det = v1.x ()*v2.y() - v1.y ()*v2.x();

    return (-qRadiansToDegrees(atan2(det,dot)));
}
Esempio n. 2
0
QVector<QVector3D> SurfaceItem::vertices() const
{
    QSize size = surface()->size();

    qreal w = (m_height * size.width()) / size.height();
    qreal h = m_height;

    QVector3D pos = m_pos + m_normal * m_depthOffset;

    QVector2D center(pos.x(), pos.z());

    QVector3D perp = QVector3D::crossProduct(QVector3D(0, 1, 0), m_normal);
    QVector2D delta = w * QVector2D(perp.x(), perp.z()).normalized() / 2;

    qreal scale = qMin(1.0, m_time.elapsed() * 0.002);

    qreal top = m_pos.y() + h * 0.5 * scale;
    qreal bottom = m_pos.y() - h * 0.5 * scale;

    QVector2D left = center - delta * scale;
    QVector2D right = center + delta * scale;

    QVector3D va(left.x(), top, left.y());
    QVector3D vb(right.x(), top, right.y());
    QVector3D vc(right.x(), bottom, right.y());
    QVector3D vd(left.x(), bottom, left.y());

    QVector<QVector3D> result;
    result << va << vb << vc << vd;
    return result;
}
Esempio n. 3
0
/**
 * update the bounding box by adding a new point.
 *
 * @param newPt new point
 */
void BBox::addPoint(const QVector2D& newPt) {
	minPt.setX(qMin(minPt.x(), newPt.x()));
	minPt.setY(qMin(minPt.y(), newPt.y()));

	maxPt.setX(qMax(maxPt.x(), newPt.x()));
	maxPt.setY(qMax(maxPt.y(), newPt.y()));
}
QPixmap ContainerSVG::createScaledContainer_(const QVector2D &factors)
{
    if (factors.x() == 1 && factors.y() == 1 && m_cache.contains(m_size))
    {
        return m_cache[m_size];
    }

    QSize mySize(qRound(m_size.width() * factors.x()),
                 qRound(m_size.height() * factors.y()));

    if (!m_nocache && m_cache.contains(mySize))
    {
        return m_cache[mySize];
    }

    QPixmap pix(mySize);
    pix.fill(Qt::transparent);
    QPainter pixPainter(&pix);
    pixPainter.setRenderHints(QPainter::Antialiasing |
                              QPainter::SmoothPixmapTransform, true);
    m_renderer->render(&pixPainter);
    pixPainter.end();

    if (!m_nocache)
    {
        m_cache.insert(mySize, pix);
    }

#ifdef IMAGEMAP_DEBUG
    wzLog(LOG_IM) << "\tGenerated:" << m_filename << pix.size();
#endif

    return pix;
}
Esempio n. 5
0
void DesignDialog::save()
{
    QList<Graph*> graph_list = graphView->getGraphList();

    FileController fc;
    fc.setFile(fileName);
    fc.clearFile();
    for(int i=0; i<graph_list.size(); i++) {
        for(int j=0; j<graph_list[i]->getSize(); j++) {
            QVector2D pnt = graph_list[i]->getPoint(j);
            fc.appendPair(pnt.x(), pnt.y());
        }
    }

    fc.setFile(fileName.left(fileName.lastIndexOf(".")) + ".tbl");
    fc.clearFile();
    for(int i=0; i<graph_list.size(); i++) {
        qreal x_step = ui->intervalSpinBox->value();
        qreal x_start = 0.;
        qreal x_end = graph_list[i]->getPoint(graph_list[i]->getSize() - 1).x();
        for ( int x=x_start; x<x_end; x+= x_step ) {
            QVector2D pnt = graph_list[i]->getFunctionAtX(x);
            fc.appendPair(pnt.x(), pnt.y());
        }
    }

}
Esempio n. 6
0
void OpenGLWidget::mouseMoveEvent(QMouseEvent *e)
{
    if (e->buttons() & Qt::LeftButton)
    {
        // Mouse release position - mouse press position
        QVector2D diff = QVector2D(e->localPos()) - m_lastMousePosition;
        m_lastMousePosition = QVector2D(e->localPos());

        // Rotation axis is perpendicular to the mouse position difference vector
        QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();

        // Update rotation
        m_rotation = QQuaternion::fromAxisAndAngle(n, 2) * m_rotation;

        // Update scene
        updateGL();
    }
    else if (e->buttons() & Qt::MiddleButton)
    {
        QVector2D diff = (QVector2D(e->localPos()) - m_lastMousePosition) / 100;
        m_lastMousePosition = QVector2D(e->localPos());

        QVector3D n = QVector3D(-diff.x(), diff.y(), 0);
        m_camera.setViewCenter(m_camera.viewCenter() + n);

        updateGL();
    }
}
Esempio n. 7
0
bool SegmentIntersection(QVector2D &result, QVector2D seg11, QVector2D seg12, QVector2D seg21, QVector2D seg22)
{
	// Store the values for fast access and easy
	// equations-to-code conversion
	double x1 = seg11.x(), x2 = seg12.x(), x3 = seg21.x(), x4 = seg22.x();
	double y1 = seg11.y(), y2 = seg12.y(), y3 = seg21.y(), y4 = seg22.y();
 
	double d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
	// If d is zero, there is no intersection
	if (d == 0) return NULL;

	// Get the x and y
	double pre = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4);
	double x = ( pre * (x3 - x4) - (x1 - x2) * post ) / d;
	double y = ( pre * (y3 - y4) - (y1 - y2) * post ) / d;
 
	// Check if the x and y coordinates are within both lines
	if ( x < std::min(x1, x2) || x > std::max(x1, x2) ||
	x < std::min(x3, x4) || x > std::max(x3, x4) ) return false;
	if ( y < std::min(y1, y2) || y > std::max(y1, y2) ||
	y < std::min(y3, y4) || y > std::max(y3, y4) ) return false;
 
	// Return the point of intersection
	result.setX(x);
	result.setY(y);
	return true;
}
Esempio n. 8
0
QPixmap ContainerImage::pixmap(const Image &image,
                         const QSize &size,
                         const AspectRatioMode &mode)
{

    QVector2D factors = calculateFactors_(size, image.size, mode);

#ifdef IMAGEMAP_DEBUG
    wzLog(LOG_IM) << "\tContainer:" << m_filename;
    wzLog(LOG_IM) << "\tMode:" << mode;
    wzLog(LOG_IM) << "\tFactors:" << factors;
    wzLog(LOG_IM) << "\tSize:" << qRound(image.size.width() * factors.x()) <<
                             qRound(image.size.height() * factors.y());
    wzLog(LOG_IM) << "\tPosition:" << qRound(image.xPosition * factors.x()) <<
                                 qRound(image.yPosition * factors.y());
#endif

    // Creates a full scaled copy of the container image with the factors.
    QPixmap full = createScaledContainer_(factors);

    // Extracts the image.
    QPixmap result = full.copy(qRound(image.xPosition * factors.x()),
                               qRound(image.yPosition * factors.y()),
                               qRound(image.size.width() * factors.x()),
                               qRound(image.size.height() * factors.y()));

    return result;
}
bool SegmentIntersection(QVector2D &result, QVector2D seg11, QVector2D seg12, QVector2D seg21, QVector2D seg22)
{
    // 保存这个数值以便快速访问并简化公式到代码的转换
	double x1 = seg11.x(), x2 = seg12.x(), x3 = seg21.x(), x4 = seg22.x();
	double y1 = seg11.y(), y2 = seg12.y(), y3 = seg21.y(), y4 = seg22.y();
 
	double d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
    //如果d是零,没有交集
	if (d == 0) return NULL;

    // 获取x和y的值
	double pre = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4);
	double x = ( pre * (x3 - x4) - (x1 - x2) * post ) / d;
	double y = ( pre * (y3 - y4) - (y1 - y2) * post ) / d;
 
    // 检查,x和y坐标是否都在两条轴线内里
	if ( x < std::min(x1, x2) || x > std::max(x1, x2) ||
	x < std::min(x3, x4) || x > std::max(x3, x4) ) return false;
	if ( y < std::min(y1, y2) || y > std::max(y1, y2) ||
	y < std::min(y3, y4) || y > std::max(y3, y4) ) return false;
 
    // 返回交点坐标
	result.setX(x);
	result.setY(y);
	return true;
}
Esempio n. 10
0
bool GLWidget::isSelected(const QVector3D &vertex, const QVector2D &min, const QVector2D &max)
{
    QVector2D result;
    fromWorldToScreen(&result, vertex);
    //if point on screen is inside rectangle, it's selected
    return result.x() > min.x() && result.x() < max.x() && result.y() > min.y() && result.y() < max.y();
}
Esempio n. 11
0
// TODO: second plane
void SliceAdjustmentTool::toolMouseReleaseEvent(QMouseEvent *ev)
{
    voxie::data::SliceImage& currentImg = (currentSlice == 0) ? this->sv->sliceImageFirst() : this->sv->sliceImageSecond();
    voxie::data::SliceImage& otherImg   = (currentSlice == 1) ? this->sv->sliceImageFirst() : this->sv->sliceImageSecond();
    voxie::data::Slice* otherSlice = (currentSlice == 0) ? this->sv->slices().at(0) : this->sv->slices().at(1);

    if(this->ctrlDown){
        if(this->rotatingInProgress){
            this->rotatingInProgress = false;
            QVector2D cursorOnPlane = QVector2D(currentImg.pixelToPlanePoint(ev->pos(), true)).normalized();
            float angle = (float) qAcos(QVector3D::dotProduct(this->rotationHypotenuse, cursorOnPlane));
            // check clockwise or counterclockwise rotation
            float sign = 1;
            {
                /* to know if cursor is left of hypotenuse rotate their coordinatesystem so that
                 * hypotenuse is points in xAxis direction and check if cursor.y is positive.
                 * Rotation matrix for this can be obtained from hypotenuse since its normalized
                 *      counterclockwise                clockwise
                 *      cos(a)  -sin(a)                 cos(a)/det   sin(a)/det
                 *      sin(a)   cos(a)                -sin(a)/det   cos(a)/det
                 *
                 * with cos(a) = hypotenuse.x , sin(a) = hypotenuse.y , det = determinant(clockwise rotMat)
                 */
                qreal cos = this->rotationHypotenuse.x();
                qreal sin = this->rotationHypotenuse.y();
                qreal det = cos*cos + sin*sin;
                // y-part of matrix multiplication (clockwise*cursor)
                qreal rotCursorY = -sin/det * cursorOnPlane.x() + cos/det * cursorOnPlane.y();
                sign = rotCursorY > 0 ? 1:-1;
            }
            if(this->selectedBoth)
            {
                QVector2D cursorOnPlane = QVector2D(otherImg.pixelToPlanePoint(ev->pos(), true)).normalized();
                float angle = (float) qAcos(QVector3D::dotProduct(this->rotationHypotenuse2, cursorOnPlane));
                // check clockwise or counterclockwise rotation
                float sign = 1;
                {
                    /* to know if cursor is left of hypotenuse rotate their coordinatesystem so that
                     * hypotenuse is points in xAxis direction and check if cursor.y is positive.
                     * Rotation matrix for this can be obtained from hypotenuse since its normalized
                     *      counterclockwise                clockwise
                     *      cos(a)  -sin(a)                 cos(a)/det   sin(a)/det
                     *      sin(a)   cos(a)                -sin(a)/det   cos(a)/det
                     *
                     * with cos(a) = hypotenuse.x , sin(a) = hypotenuse.y , det = determinant(clockwise rotMat)
                     */
                    qreal cos = this->rotationHypotenuse2.x();
                    qreal sin = this->rotationHypotenuse2.y();
                    qreal det = cos*cos + sin*sin;
                    // y-part of matrix multiplication (clockwise*cursor)
                    qreal rotCursorY = -sin/det * cursorOnPlane.x() + cos/det * cursorOnPlane.y();
                    sign = rotCursorY > 0 ? 1:-1;
                }
                rotateSlice(otherSlice, otherSlice->normal(), ((angle)/3.1415f) * 180 * sign);
            }
            rotateSlice(this->slice, this->slice->normal(), ((angle)/3.1415f) * 180 * sign);
        }
    }
}
Esempio n. 12
0
int PointLineCompare(QVector2D pointm, QVector2D dir, QVector2D quarrypoint)//returns 1 if point is on right, -1 if point is on left
{
	//double MAx = (quarrypoint.x() - pointm.x());
	//double MAy = (quarrypoint.y() - pointm.y());

	double position = (dir.x()*(quarrypoint.y() - pointm.y())) - (dir.y()*(quarrypoint.x() - pointm.x()));
	return -int(ceil(position));
}
int PointLineCompare(QVector2D pointm, QVector2D dir, QVector2D quarrypoint)//如果点在右则返回1,如果点在左,则返回-1
{
	//double MAx = (quarrypoint.x() - pointm.x());
	//double MAy = (quarrypoint.y() - pointm.y());

	double position = (dir.x()*(quarrypoint.y() - pointm.y())) - (dir.y()*(quarrypoint.x() - pointm.x()));
	return -int(ceil(position));
}
Esempio n. 14
0
bool PointsShare(QVector2D point1, QVector2D point2, double tolerance)
{
    if(IsZero(point2.x() - point1.x(), tolerance) && IsZero(point2.y() - point1.y(),tolerance))
    {
        return true;
    }
    return false;
}
Esempio n. 15
0
bool BBox2D::contains(const QVector2D &p) {
	if (p.x() < corner0.x()) return false;
	if (p.y() < corner0.y()) return false;

	if (p.x() > corner1.x()) return false;
	if (p.y() > corner1.y()) return false;

	return true;
}
Esempio n. 16
0
PropertyGroup::PropertyGroup(QVector2D min, QVector2D max, QVector2D t, QString name)
{
	createBox(name);

	properties.emplace_back(new Property(min.x(), max.x(), t.x(), box));
	properties.emplace_back(new Property(min.y(), max.y(), t.y(), box));

	config();
}
void GameApplication::loadScene()
{
    disconnect(&m_window, &QWindow::activeChanged, this, &GameApplication::loadScene);
    CaveGenerator cgen(MAP_SIZE, MAP_SIZE, 10);
    m_map = cgen.GetCaveMap();
    ObjectsGenerator ogen(m_map);
    m_map = ogen.GenerateObj();

    std::shared_ptr<BaseScene> scene = std::make_shared<BaseScene>();
    scene->camera().setViewport(m_window.size());

    //new ColoredCube(scene.get(), {0, 0, 0}, ColoredCube::WallType::CaveGround);
    new SkyBox(scene.get());

    int x = 0;
    int z = 0;
    for (size_t i = 0; i < MAP_SIZE; i++, z += WALL_LEN)
    {
        x = 0;
        for (size_t j = 0; j < MAP_SIZE; j++, x += WALL_LEN)
        {
            if (m_map[i][j] == WALL_CELL)
            {
                new ColoredCube(scene.get(), {z, 0, x}, ColoredCube::WallType::CaveWall);
            }
            else if (m_map[i][j] == ENTERANCE_CELL)
            {
                m_player = new PlayerNode(scene.get(), QVector2D(z, x));
                new ColoredCube(scene.get(), {z, 0, x}, ColoredCube::WallType::CaveGround);
            }
            else if (m_map[i][j] == SIDE_EXIT_CELL)
            {
                m_exit = new ExitNode(scene.get(), QVector2D(z, x));
            }
            else if (m_map[i][j] == GROUND_EXIT_CELL)
            {

            }
            else if (m_map[i][j] == FREE_CELL)
            {
                new ColoredCube(scene.get(), {z, 0, x}, ColoredCube::WallType::CaveGround);
            }
        }
    }

    scene->setPlayer(m_player);
    scene->setExit(m_exit);
    QVector2D pos = m_player->GetCoords();
	scene->camera().lookAt(QVector3D(pos.x(), CAM_UP, pos.y() - CAM_RANGE), QVector3D(pos.x(), CAM_UP_ANGLE, pos.y()), QVector3D(0, 0, 1));
    CollisionHandler colHandler;
    colHandler.SetMap(m_map);
    colHandler.SetCoord(pos);
    m_window.SetCollisionHandler(colHandler);

    m_window.pushScene(scene);
}
QVector<QVector2D > GoogleMapWidget::generateTile(const QVector2D &position)
{
	const QVector2D pixelPosition = position * Tile::SIZE;

	return QVector<QVector2D >() <<
		QVector2D(pixelPosition.x(), 			  pixelPosition.y() + Tile::SIZE) << QVector2D(0, 1) <<
		QVector2D(pixelPosition.x(), 			  pixelPosition.y()				) << QVector2D(0, 0) <<
		QVector2D(pixelPosition.x() + Tile::SIZE, pixelPosition.y() + Tile::SIZE) << QVector2D(1, 1) <<
		QVector2D(pixelPosition.x() + Tile::SIZE, pixelPosition.y()				) << QVector2D(1, 0);
}
Esempio n. 19
0
/**
 * Compute the difference in angle that is normalized in the range of [0, PI].
 * absolute == falseの時は、dir1の角度 - dir2の角度を[-PI, PI]で返却する。
 */
float Util::diffAngle(const QVector2D& dir1, const QVector2D& dir2, bool absolute) {
	float ang1 = atan2f(dir1.y(), dir1.x());
	float ang2 = atan2f(dir2.y(), dir2.x());

	if (absolute) {
		return fabs(normalizeAngle(ang1 - ang2));
	} else {
		return normalizeAngle(ang1 - ang2);
	}
}
Esempio n. 20
0
std::vector<float> BresenhamRayCaster::cast(QVector3D& location, QVector3D& direction, Volume& data, QVector2D& angle)
{
	std::vector<float> samples;

	float value = 0.0;
	QVector3D current = QVector3D(location);

	//Code to compute angle out of direction vector, now implemented as Parameter of this function, due computational expenses and accuraccy
	/*
	QVector2D xz_dir = QVector2D(direction.x(), direction.z());
	double xz_angle = QVector2D().dotProduct(xz_dir.normalized(), QVector2D(0, 1));
	xz_angle = acos(xz_angle) * 57.2957549;
	*/
	
	int width = data.width();
	int height = data.height();
	int depth = data.depth();

	float factorX = cos(angle.y() * 0.0174533);
	float factorZ = sin(angle.y() * 0.0174533);

	float factorX_Y = cos(angle.x() * 0.0174533);
	float factorX_Z = sin(angle.x() * 0.0174533);

	int steps;
	if (angle.x() > 0 || angle.y() > 0) 
	{
		QVector3D a = QVector3D(data.width() * -factorZ, data.height() * direction.y(), data.depth() * -factorX);
			QVector3D b = QVector3D(location.x() * -factorZ, location.y() * direction.y(), location.z() * -factorX);
		//QVector3D a = QVector3D(data.width() * direction.x(), data.height() * factorX_Z, data.depth() * factorX_Y);
		//QVector3D b = QVector3D(location.x() * direction.x(), location.y() * factorX_Z, location.z() * factorX_Y);
		steps = (a.length() + QVector2D(a - b).length()) / direction.length(); //nicht weit genug gesampelt?
	}
	else
	{
		steps = data.depth() / direction.length();
	}

	int i = 0;
	while (i < steps)
	{
		if (current.x() < 0 || current.x() >= width ||
			current.y() < 0 || current.y() >= height ||
			current.z() < 0 || current.z() >= depth)
			samples.push_back(0);

		value = data.value((int)current.x(), (int)current.y(), (int)current.z());

		samples.push_back(value);
		current += direction;
		i++;
	}

	return samples;
}
Esempio n. 21
0
const QVector2D NavigationMath::raySquareIntersection(
	const QVector2D & point
,	const float length)
{
	const float ax = abs(point.x());
    const float ay = abs(point.y());

	if(ax >= ay) // intersection is with left or right border
        return QVector2D(sign(point.x()), point.y() / ax) * length;
	else // intersection is with bottom or top border
		return QVector2D(point.x() / ay, sign(point.y())) * length;
}
Esempio n. 22
0
/**
 * Project latitude/longitude coordinate to world coordinate.
 * Mercator projection cannot be used for this purpose, becuase
 * it deforms the area especially in the high latitude regions.
 * Hubeny's theorum should be used for this purpose, but not yet implemented yet.
 *
 * @param lat		latitude
 * @param lon		longitude
 * @param centerLat	latitude of the center of the map
 * @param centerLon	longitude of the center of the map
 * @return			the world coordinate (Z coordinate is dummy.)
 */
QVector2D Util::projLatLonToMeter(const QVector2D &latLon, const QVector2D &centerLatLon) {
	QVector2D result;

	double y = latLon.y() / 180 * M_PI;
	double dx = (latLon.x() - centerLatLon.x()) / 180 * M_PI;
	double dy = (latLon.y() - centerLatLon.y()) / 180 * M_PI;

	double radius = 6378137;

	result.setX(radius * cos(y) * dx);
	result.setY(radius * dy);

	return  result; 
}
float ControlAlgorithm::calcAngleFromVectors(QVector2D vec1,QVector2D vec2){

    float angle;
    //angle btwn 2 vectors:
    //A · B = A B cos θ = |A||B| cos θ
    //θ = cos-1(A · B/|A||B|)

    angle = acos(QVector2D::dotProduct(vec1,vec2)/
                 (sqrt(vec1.x()*vec1.x()+vec1.y()*vec1.y())*sqrt(vec2.x()*vec2.x()+vec2.y()*vec2.y())))*180/3.1416;


    return angle;

}
void ControlAlgorithm::drawToFrame(QVector2D kitePos, QVector2D heading){
    kiteTracer.push_back(QVector2D(kitePos.x(),kitePos.y()));

    if(kiteTracer.size()>150){

        kiteTracer.pop_front();


    }

    //length of visual line vector
    int lineLength = 120;
    //still use recent kite x,y to keep an eye on the actual position
    //of kite regardless if it has moved out of the bounding rect or not


    //get handle to currentFrame so we can
    //manipulate stuff on the image (draw paths etc.)
    currentFrame = kiteColorTracker->getFrameHandle();

    //test
    if(currentFrame!=NULL){


        cv::line(*currentFrame,cv::Point(kitePos.x(),kitePos.y()),cv::Point(kitePos.x()+lineLength*heading.x(),kitePos.y()+lineLength*heading.y()),
                 cv::Scalar(0,0,255),4);


        cv::Point pointmem(0,0);
        if(kiteTracer.size()>0)
            pointmem=cv::Point(kiteTracer.at(0).x(),kiteTracer.at(0).y());
        for(int j=0;j<kiteTracer.size();j++){

            cv::line(*currentFrame,pointmem,cv::Point(kiteTracer.at(j).x(),kiteTracer.at(j).y()),cv::Scalar(0,255,0),1);
            cv::circle(*currentFrame,cv::Point(kiteTracer.at(j).x(),kiteTracer.at(j).y()),2,cv::Scalar(255,0,0),1);
            pointmem = cv::Point(kiteTracer.at(j).x(),kiteTracer.at(j).y());
        }

        //draw quadrants for visualization

        cv::rectangle(*currentFrame,cv::Rect(Q1->getLeftX(),Q1->getTopY(),Q1->getRightX()-Q1->getLeftX(),Q1->getBottomY()-Q1->getTopY()),cv::Scalar(0,255,255));
        cv::rectangle(*currentFrame,cv::Rect(Q2->getLeftX(),Q2->getTopY(),Q2->getRightX()-Q2->getLeftX(),Q2->getBottomY()-Q2->getTopY()),cv::Scalar(0,255,255));
        cv::rectangle(*currentFrame,cv::Rect(Q3->getLeftX(),Q3->getTopY(),Q3->getRightX()-Q3->getLeftX(),Q3->getBottomY()-Q3->getTopY()),cv::Scalar(0,255,255));
        cv::rectangle(*currentFrame,cv::Rect(Q4->getLeftX(),Q4->getTopY(),Q4->getRightX()-Q4->getLeftX(),Q4->getBottomY()-Q4->getTopY()),cv::Scalar(0,255,255));
        cv::rectangle(*currentFrame,cv::Rect(Q5->getLeftX(),Q5->getTopY(),Q5->getRightX()-Q5->getLeftX(),Q5->getBottomY()-Q5->getTopY()),cv::Scalar(100,100,255));

        // currentFrame=NULL;
    }

}
Esempio n. 25
0
void BBox2D::addPoint(const QVector2D &p) {
	if (p.x() < corner0.x()) {
		corner0.setX(p.x());
	}
	if (p.y() < corner0.y()) {
		corner0.setY(p.y());
	}

	if (p.x() > corner1.x()) {
		corner1.setX(p.x());
	}
	if (p.y() > corner1.y()) {
		corner1.setY(p.y());
	}
}
Esempio n. 26
0
//Default implementation
void GameObject::updatePosition(float timeElapsed)
{
  //For each frame, compute the new speed and position regarding the acceleration value
  //and the time elapsed since the last computation

  //Compute new position, with speed threshold, to avoid shaking effect of the sprite on low speeds

    QVector2D tempSpeed = speed ;
    if(abs(tempSpeed.x())<=4)
        tempSpeed.setX(0);
    if(abs(tempSpeed.y())<=4)
        tempSpeed.setY(0);
    position += (tempSpeed*timeElapsed + (1/2)*acceleration*timeElapsed*timeElapsed).toPointF();

  //Compute new speed, with frictional resistance (typically 0.995)  FIXME : the game seems laggy with a coeff friction...
  speed = frictionCoef*speed + acceleration*timeElapsed;
  if(speed.length()>=m_maxSpeed)
      speed = m_maxSpeed*speed.normalized();


  //Adjust position with the window borders
  //WARN: Modulus operator does not work for floats
  position.setX( position.x() - (float)((int)(position.x()/screenWidthGlobal))*screenWidthGlobal );
  if(position.x()<0)
    position.setX(position.x() + (float)screenWidthGlobal) ;

  position.setY( position.y() - (float)((int)(position.y()/screenHeightGlobal))*screenHeightGlobal );
  if(position.y()<0)
    position.setY(position.y() + (float)screenHeightGlobal);

  //Donatien: Is there a nicer way to saturate speed and acceleration?
  //Same values for every objects or a configurable one?
}
Esempio n. 27
0
void GameScene::advance() {
    currentTime = time.elapsed();
    if(firstStep) {
        _dt = 0;
        firstStep = false;
    } else {
        _dt = (currentTime - lastFrameTime) / 1000.0;
    }
    lastFrameTime = currentTime;
    particlesToAdd += _dt * particlesPerSecond;

    // Activate the next not yet active particle
    if(currentParticleNumber < level()*100000 ) {
        int particlesToAddNow = (int)particlesToAdd; // how many particles (integer number) to add this frame
        for(int i = 0; i < particlesToAddNow; i++) {
            Particle* particle = new Particle();
            _particles.append(particle);
            addItem(particle); // always add items before adjusting position (because they need a gameScene)
            particle->setActive(true);
            QVector2D particlePosition = generatorPosition;
            particlePosition.setY(particlePosition.y() + random()*0.001);
            particle->setPosition(particlePosition);
            QVector2D particleVelocity = generatorVelocityDirection * generatorVelocityMagnitude;
            particleVelocity.setX(particleVelocity.x() + random()*0.001);
            particleVelocity.setY(particleVelocity.y() + random()*0.001);
            particle->setVelocity(particleVelocity);
            currentParticleNumber++; // Increase the global counter
            particlesToAdd--; // For each added particle, remove one from the queue
        }
    }
    QGraphicsScene::advance();
}
Esempio n. 28
0
void GLWidget3D::mousePressEvent(QMouseEvent *event) {
	QVector2D pos;

	if (Qt::ControlModifier == event->modifiers()) {
		controlPressed = true;
	} else {
		controlPressed = false;
	}

	this->setFocus();

	lastPos = event->pos();
	mouseTo2D(event->x(), event->y(), pos);

	if (event->buttons() /*& Qt::LeftButton*/) {
		switch (mainWin->mode) {
		case MainWindow::MODE_AREA_SELECT:
			if (altPressed) {
					// normal Gaussian edition
					float change=mainWin->controlWidget->ui.terrainPaint_changeSlider->value()*0.003f;
					float radi=mainWin->controlWidget->ui.terrainPaint_sizeSlider->value()*0.01f;
					if (event->buttons() & Qt::RightButton) {
						change = -change;
					}
					if (event->buttons() & Qt::MiddleButton) {
						change=FLT_MAX;//hack: flat terrain
					}
					//mainWin->urbanGeometry->vboRenderManager->addValue(pos.x(), pos.y(), change);
					float xM=1.0f-(vboRenderManager.side/2.0f-pos.x())/vboRenderManager.side;
					float yM=1.0f-(vboRenderManager.side/2.0f-pos.y())/vboRenderManager.side;
					vboRenderManager.vboTerrain.updateTerrain(xM,yM,change,radi);//rad,change);
					mainWin->urbanGeometry->adaptToTerrain();/// !! GEN did not have it here (enough in move?)
					shadow.makeShadowMap(this);
					updateGL();
			} else {
				// select a vertex or an edge
				bool found = false;
				
				if (shiftPressed) {	// select am edge
					// select a vertex or an edge
						if (GraphUtil::getEdge(mainWin->urbanGeometry->roads, pos, 30, selectedEdgeDesc)) {
							selectEdge(mainWin->urbanGeometry->roads, selectedEdgeDesc);
						} else {
							vertexSelected = false;
							edgeSelected = false;
						}
				} else {	// select a vertex
					// select a vertex or an edge
						if (GraphUtil::getVertex(mainWin->urbanGeometry->roads, pos, 30, selectedVertexDesc)) {
							selectVertex(mainWin->urbanGeometry->roads, selectedVertexDesc);
						} else {
							vertexSelected = false;
							edgeSelected = false;
						}
				}
			}
			break;
		}
	}
}
/**
* Calculates the distance between the two set points considering the resolution
* retrieved by the given metadata.
* The distance is calculated between the start point and end point.
* If the end point is not yet available, then the current point (curPoint) is used.
* \sa getDistanceInCm() getDistanceInInch()
**/
void DkDistanceMeasure::calculateStartEndDistance() {

	if (points[0].isNull()) return;

	// get the image resolution for distance calculation
	x_res = 72;		// markus: 72 dpi is the default value assumed
	y_res = 72;

	// >DIR: get metadata resolution if available [21.10.2014 markus]
	if (metaData) {

		QVector2D res = metaData->getResolution();
		x_res = res.x();
		y_res = res.y();
	}

	float length_x_inch, length_y_inch;

	if (!points[1].isNull()) curPoint = points[1];
	
	length_x_inch = abs(curPoint.x() - points[0].x()) / x_res;
	length_y_inch = abs(curPoint.y() - points[0].y()) / y_res;

	dist_inch = sqrt(length_x_inch * length_x_inch + length_y_inch * length_y_inch);
	dist_cm = dist_inch * 2.54;
}
Esempio n. 30
0
void GLWidget::mouseMoveEvent(QMouseEvent *e) {
	float dx = (float)(e->x() - lastPos.x());
	float dy = (float)(e->y() - lastPos.y());
	lastPos = e->pos();
	//float camElevation = camera->getCamElevation();

	QVector2D pos;
	mouseTo2D(e->x(), e->y(), &pos);
	float dx2D = pos.x() - last2DPos.x();
	float dy2D = pos.y() - last2DPos.y();
	last2DPos = pos;

	if (e->buttons() & Qt::LeftButton) {
		selectedArea.addPoint(last2DPos);
	} else if (e->buttons() & Qt::MidButton) {   // Shift the camera
		camera->changeXYZTranslation(-dx * camera->dz * 0.001f, dy * camera->dz * 0.001f, 0);
	} else if (e->buttons() & Qt::RightButton) { // Zoom the camera
		setCursor(Qt::SizeVerCursor);

		camera->changeXYZTranslation(0, 0, -dy * camera->dz * 0.02f);
		if (camera->dz < MIN_Z) camera->dz = MIN_Z;
		if (camera->dz > MAX_Z) camera->dz = MAX_Z;

		// tell the Z coordinate to the road graph so that road graph updates rendering related variables.
		roads.setZ(camera->dz);

		lastPos = e->pos();
	}

	last2DPos = pos;

	updateGL();
}