Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to perform object placement based on parent object position.

For example, I have chest and head object. I want when I rotate chest object then as per chest rotation head object also rotate and transform.

For this thing to achieve I did following stuff but I don't achieve desire effect and can't able to understand what was I doing?

private void addRectangle(float angle) {

    Rectangle baseRectangle = new Rectangle(300f, 300f, 100f, 200f);
    baseRectangle.setRotationCenter(0f, 0f);
    baseRectangle.setRotation(angle);
    // red base rectangle
    baseRectangle.setColor(1f, 0f, 0f);
    mMainScene.attachChild(baseRectangle);

    float tAngle = MathUtils.atan2(-200, 100f);
    float dist = FloatMath.sqrt(50 * 50 + 50 * 50);

    float pX = baseRectangle.getX()
            + FloatMath.cos(MathUtils.degToRad(baseRectangle.getRotation())
                    + tAngle) * dist;
    float pY = baseRectangle.getY()
            + FloatMath.sin(MathUtils.degToRad(baseRectangle.getRotation())
                    + tAngle) * dist;

    Rectangle childRectangle = new Rectangle(pX, pY, 50f, 50f);
    childRectangle.setRotationCenter(0f, 0f);
    childRectangle.setRotation(angle);
    // green child rectangle
    childRectangle.setColor(0f, 1f, 0f);
    mMainScene.attachChild(childRectangle);

}

As function argument I provide desire angle on which both the object needs to be rotated. Someone please help me to get out from this problem.

share|improve this question
    
Here I want to know how object get placed based on other object angle and position. –  Siddharth Apr 15 '13 at 3:37
add comment

1 Answer 1

up vote 0 down vote accepted

your angle and distance formula wrong. in your child position you get angle and dist that i define below code.

private void addRectangle(float angle) {
    float centerX, centerY;
    Rectangle baseRectangle = new Rectangle(300f, 300f, 200, 100f);
    centerX = baseRectangle.getX() + baseRectangle.getWidth() / 2;
    centerY = baseRectangle.getY() + baseRectangle.getHeight() / 2;
    baseRectangle.setRotation(angle);
    baseRectangle.setColor(1f, 0f, 0f);
    mScene.attachChild(baseRectangle);
    float tAngle = -0.2f;
    float dist = 128f;
    float pX = centerX + FloatMath.cos(MathUtils.degToRad(angle) + tAngle)
            * dist;
    float pY = centerY + FloatMath.sin(MathUtils.degToRad(angle) + tAngle)
            * dist;
    Rectangle childRectangle = new Rectangle(pX, pY, 50f, 50f);
    childRectangle.setRotationCenter(0, 0);
    childRectangle.setRotation(angle);
    childRectangle.setColor(0f, 1f, 0f);
    mScene.attachChild(childRectangle);
}
share|improve this answer
    
Thanks a lot for your help. It works completely. –  Siddharth Apr 15 '13 at 16:42
    
This thing create another problem for me. In the above code you change the rotation center of child object so when I attach body to this object creates abnormal behavior. Because of different rotation point of child object and its body. So what to do in this situation? –  Siddharth Apr 17 '13 at 4:34
    
if your child not movement after attaching parent and child then after attachChild change your rotation center of your child. or give me some code to identify wht a pro ? –  Divyang Metalia Apr 17 '13 at 7:13
    
Basically I understand from your statement is that first I have to create object with corner rotation center at that time my object body was static and then change rotation center to its normal position when I need dynamic body. I tried this thing in same code only. I attach bodies in the above rectangles and get this problem. This thing I will check and reply you. Am I understand any wrong thing then please suggest. Thanks. –  Siddharth Apr 18 '13 at 2:40
    
you can create body after attachChild or change rotation center. –  Divyang Metalia Apr 18 '13 at 10:22
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.