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 ask that how to rotate body from corner?

movableBeam.getBeamBody().setTransform(movableBeam.getBeamBody().getPosition(), angle);

The above line of code rotate the beam from center that I want rotate from one of the conner. Any member please help me.

EDIT :

            float beamCenterX = movableBeam.getX() + movableBeam.getWidth()
                    / 2f;
            float beamCenterY = movableBeam.getY()
                    + movableBeam.getHeight() / 2f;

            float cornerOffsetX = movableBeam.getX() - beamCenterX;
            float cornerOffsetY = movableBeam.getY() - beamCenterY;

            float bodyAngle = (float) Math.atan2(cornerOffsetY,
                    cornerOffsetX);

            float newAngle = imageAngle + bodyAngle;
            float newCornerOffsetX = (float) Math.cos(Math
                    .toDegrees(newAngle));
            float newCornerOffsetY = (float) Math.sin(Math
                    .toDegrees(newAngle));

            cornerOffsetX = movableBeam.getX() - movableBeam.getWidth()
                    / 2f;
            cornerOffsetY = movableBeam.getY() - movableBeam.getHeight()
                    / 2f;

            Vector2 postion = new Vector2(
                    (newCornerOffsetX - cornerOffsetX + movableBeam.getX())
                            / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
                    (newCornerOffsetY - cornerOffsetY + movableBeam.getY())
                            / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);

            movableBeam.getBeamBody().setTransform(postion, newAngle);
share|improve this question
2  
You store the position of the corner before the rotation, subtract it from the position of the corner after rotation and offset(move) the position of said object by that vector. –  Zehelvion Oct 1 '12 at 10:57

3 Answers 3

up vote 7 down vote accepted

Rotate and then move the object

When we rotate a object in 2d, each corner is a vector (x,y) away from the center. That vector has an angle.

Get the offset of the selected corner from the center:

cornerOffset.x =  corner.x - center.x;
cornerOffset.y =  corner.y - center.y;

To calculate this angle we do something like this in AS3 / Java

angle = Math.Atan2(corner.y - center.y, corner.x - center.x); //you can use the previously calculated values

It's length

length = Math.sqrt((corner.x - center.x)^2 + (corner.y - center.y)^2); //same

Sometime, if the object is rectangular, the corner will probably be at:

cornerOffset = center.x (+/-) width/2.0, corner.y (+/-) height

After rotating the object from the center by (B) degrees, the vector for the offset has been rotated as well.

So the new corner position will be:

newAngle = angle + B;
newCornerOffset.x = length * Math.cos(newAngle);
newCornerOffset.y = length * Math.sin(newAngle);

objectOffset.x = cornerOffset.x - newCornerOffset.x;
objectOffset.y = cornerOffset.y - newCornerOffset.y;

Now give the object sufficient speed to get it back to where you want it.

Speed = objectOffset / deltaTime .. 

In your specific case the object is (168px wide, 14px tall) so the original offset would be (84, 7)

Each frame you need to check the current angle and know the next angle.

Then calculate :

cornerOffset.x = length * Math.cos(currentAngle);
cornerOffset.x = length * Math.sin(currentAngle);

newAngle = currentAngle + angleChange; //angle change for this frame
newCornerOffset.x = length * Math.cos(newAngle);
newCornerOffset.y = length * Math.sin(newAngle);

moveObject(currentCornerOffset.x - newCornerOffset.x, currentCornerOffset.y - newCornerOffset.y);
share|improve this answer
    
All the (x,y) coordinate I have to get for body or sprite that I have not understand. –  Siddharth Oct 1 '12 at 11:37
    
Can you paste a screenshot of the shape you are rotating? –  Zehelvion Oct 1 '12 at 12:24
    
To calculate the position of the corner, I would need to know more about the object you are moving, is it rectangular, what is the width and the height? If not, the could you please elaborate. –  Zehelvion Oct 1 '12 at 12:40
    
It is simply rectangular beam with width of 168px and height of 14px. I tried to convert you code but I doesn't get desired output. –  Siddharth Oct 1 '12 at 12:46
    
Can you share all the new code in the body of the question? You probably need to calculate the speed each frame, based on the previous angle and the current angle of the object. –  Zehelvion Oct 1 '12 at 12:51

By default the body was rotated from center but you can change that point following way.

PolygonShape shape = new PolygonShape();
shape.setAsBox(width, height, new Vector2(0f, height / 2f), angle);

In this manner you can create body that rotate from the corner not the center.

share|improve this answer

you can rotate the body from corner by just making a body from where you want to rotate and set the density 1000 and above depends on body weight . and after that you can use body.setTransform(new Vector2(x,y), MathUtil.degToRad(Sprite.getRotation()).

share|improve this answer
    
This is a poorly formatted summary of Arthur's answer... –  Anko Feb 23 '13 at 14:38

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.