简体   繁体   English

如何检测对象来自什么类? t

[英]How to detect what class an object is from? Qt

I'm programming a physicssimulation in QT with balls and this is the collision detection I'm using to detect collision between balls. 我正在用球对QT进行物理模拟编程,这是我用来检测球之间碰撞的碰撞检测。

bool Ball:: circCollide(QList <QGraphicsItem *> items) {
    QPointF c1 = mapToScene( this->boundingRect().center());
//    qDebug() << "c1x" << c1.x();
//    qDebug() << "c1y" << c1.y();
//    qDebug() << "Listcount3: " << scene()->collidingItems(this).count();

    foreach (QGraphicsItem * t, items) {
//      qDebug() << "Classname: " << typeid(t).name();
//        if (typeid(t).name() == "Ball")
        if ( t->boundingRect().width()<200)
        {
            double distance = QLineF(c1,mapToScene( t->boundingRect().center())).length();
    //        qDebug() << "tx" << mapToScene(t->boundingRect().center()).x();
    //        qDebug() << "ty" << mapToScene (t->boundingRect().center()).y();
            double radius1 = this->boundingRect().width() / 2;
            double radius2 = t->boundingRect().width() / 2;
            double radii = radius1 + radius2;
    //        qDebug () << "distance radius1: " << radius1;
    //        qDebug () << "distance radius2: " << radius2;
    //        qDebug () << "distance :" << distance;
            if ( distance <= radii )
            {
            //    qDebug() << "true collision";
                return true;
            }
        }
    }
 //   qDebug() << "false collision";
    return false;
}

Now I have the problem that this sometimes is also called for when they collide with my walls. 现在,我遇到的问题是,当它们与我的墙壁碰撞时,有时也会需要它。 The balls have the Class Ball which I made myself and inherits from QGraphicsItem, while the walls have the class QLineF which also inherits from Qgraphicsitem. 这些球具有我自己制作的Class Ball并从QGraphicsItem继承,而墙壁具有QLineF类,它也从Qgraphicsitem继承。 Is there any good way to figure out whether an object "t" belongs to the class Ball? 有什么好办法找出对象“ t”是否属于Ball类吗? I've already tried 我已经试过了

typeid(t).name() 

but that returns the same for both classes (P13QGraphicsItem). 但这对两个类(P13QGraphicsItem)返回相同的结果。

Also the list I feed to at the beginning contains all objects colliding with this item hence I have to feed it a list of QGraphicsItems. 另外,我一开始要提供的列表包含与该项目冲突的所有对象,因此我必须向其提供QGraphicsItems列表。

Override QGraphicsItem::type in Ball. 覆盖Ball中的QGraphicsItem::type Return something above the value of UserType and then call type on each QGraphicsItem to determine if it is a Ball . 返回超出UserType值的值,然后在每个QGraphicsItem上调用type以确定它是否是Ball

You can try casting similar to this example: 您可以尝试类似于以下示例的转换:

Tag* tag = new Tag();
Polygon* polygon = new Polygon();
Polygon* polygon_cast = dynamic_cast<Polygon*>(polygon);
if(polygon_cast) qDebug() << "Cool!";
else qDebug() << "oh..";
Tag* tag_cast = dynamic_cast<Tag*>(tag);
if(tag_cast) qDebug() << "Cool!";
else qDebug() << "oh..";

如果Ball是从QObject派生的,并且在其声明中具有Q_OBJECT宏,则可以简单地调用QObject::metaObjectQMetaObject::className

t->metaObject()->className();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM