Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with nested bodies #97

Open
folibis opened this issue May 27, 2015 · 3 comments
Open

Problem with nested bodies #97

folibis opened this issue May 27, 2015 · 3 comments

Comments

@folibis
Copy link
Contributor

folibis commented May 27, 2015

Since Body object isn't derived now from QQuickItem sometimes I meet difficulties with nested items. For example suppose we have custom component in Wheel.qml:

Item {
    id: root
    Rectangle {
        id: wheel
        width: 100
        height: 100
        radius: width / 2
        color: "green"
        Body {
            id: wheelBody
            target: wheel
            bodyType: Body.Dynamic
            fixtures: Circle {
                radius: wheel.width / 2
                density: 0.5
            }
        }
    }
}

Now I want to use this component in the main scene:

Window {
    visible: true
    width:800
    height: 600
    World { id: world }
    Wheel {
        x: 300
        y: 200
    }
}

The scene now looks not as expected. The reason is that wheel positioned is local coordinates (x=0,y=0) relative to its parent but wheelBody is positioned in world coordinates (x=300,y=200) so body and its traget are not syncronized.
I think it's serious problem since nested bodies is not allowed. In more or less compicated games bodies are compound.

@bjorn
Copy link
Member

bjorn commented Jun 3, 2015

This has nothing to do with Body being a QQuickItem or not. Box2D does not support nested bodies either way, and it makes sense because this does not exist in the real world either. If you want parts to be held together, they need to be held together by contact points (joints) and not by introducing some virtual coordinate space (the shared parent) that somehow keeps things in one place.

The tricky part is of course to find a way to do it properly in QML. In an imperative language, it would be easy since you would just have a class that you pass a reference to the world, and it would instantiate all its parts inside that world, along with joints to hold things together.

I think in QML you'd need to do something similar, for example by setting the Item.parent property as appropriate for the visual pieces.

@folibis
Copy link
Contributor Author

folibis commented Jun 3, 2015

Yes, @bjorn you are right. It just maybe I put it wrong when said "nested". Sure I talk about Body nested into QML items, not a Body inside another Body. For example, if you put a car in a game it can be construct from logical components:

Item {
    id: car
    Wheels {}
    Engine {}
    Wagon {}
    ...
}

when each component located in different file.
The problem I'm talking about is that in QML structure a Body's target can be inside another QML item and while initialization (inside Box2DBody::createBody()) the Body get its coordinates from mTarget->position() i.e. relative to target's parent not to scene (or world). So in most of case it will be (0,0). In example in my first post I put Wheel at (300,200) but its Body will be initialized at (0,0) in World just because of that wheel position is (0,0), relative to root.

@bjorn
Copy link
Member

bjorn commented Jun 4, 2015

@folibis Actually your example for grouping car elements should work fine as long as your top-level car item sits at 0,0 and is not transformed. Basically, you need to avoid introducing any new coordinate systems because these do not exist in Box2D.

The following approach may work to position car elements without relying on a coordinate system that does not match up with the world:

Item {
    id: car

    property real carX: 0
    property real carY: 0

    Wheel { x: carX; y: carY + 50 }
    Wheel { ... }
    Engine { ... }
    Wagon { ... }
    ...
}

JosephMillsAtWork pushed a commit to JosephMillsAtWork/qml-box2d that referenced this issue Sep 20, 2016
…ox2d#108 Added qmltypes to make system for Qml tooling. qml-box2d#109 Added qrc file for common icons and removed doubles. qml-box2d#110 Added a example applicaiton that makes it so one can test out all the exmaples without qmlscence. qml-box2d#111 made all examples install to qt's example dir in prep for docs and better stucture, qml-box2d#112 , qml-box2d#13 Set up stucture and role back point for qt-ish style of libs and plugins,
JosephMillsAtWork pushed a commit to JosephMillsAtWork/qml-box2d that referenced this issue Sep 25, 2016
…d#109 qrc & icons. qml-box2d#110 qmlscence optinal. qml-box2d#111 , qml-box2d#112 qt's example dir in prep for docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants