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 am currently trying to move an object (a square) around the stage in 2D space. Think Cuboid but in 2D platformer space, and vertical walls as well as the ground.

The idea is that during each rotation, if any collision occurs, I want the block to move onto that square.

For example, if the "player" is moving left, and there is a wall directly in front of him, he will hit the wall and start to move up the wall instead of on the ground.

There will be edge cases of the player moving left, no block directly in front of him, but a block one width and height to the left of him. I.E. there is a block diagonally up-left of the player, so when the player moves once more to the left, he will collide with the block during the rotation. When that happens, I want to continue the player's movement up the wall as well.

Below is the code of how I am rotation the object and moving it. I do not want to rotate the object around it's center point and move it at along a constant speed. It looks better moving the object with the rotation.

The questions are:

a) Is there a better way to accomplish what I am trying to do (I'm sure there is).

b) Is there a way to get the positions of each corner while the object is rotating? How I am currently doing it, the only static point is always the bottom left of the player. Since I rotate around that point, and when the rotation has rotated a full 90 degrees, I scaleX and move the object to the new position. So the registration point is always at the bottom left.

Object info: blocks and the player are both 16x16. Their registration points are bottom left.

Game info currently: I just have the player bouncing between the stage boundaries. Hence the moveLeft boolean checks.

if (moveLeft){
    rotation -= moveSpeed;
    if (Math.abs(rotation) >= 90){
        if (x == 16){
            moveLeft = false;
            scaleX *= -1;
        }
        else{
            x -= 16;
        }
        rotation = 0;
    }
}
else{
    rotation += moveSpeed;
    if (Math.abs(rotation) >= 90){
        if (x == 624){
            moveLeft = true;
            scaleX *= -1;
        }
        else{
            x += 16;
        }
        rotation = 0;
    }
}

If you need anymore information, please let me know. I hope I explained the situation sufficiently.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You could use matrices for your transformation. Using these, it should be simple to rotate around any given point.

Here's a visualization of the process:

matrix transform

Let's assume your cube has its origin at the center of the cube. To rotate it around a corner, you would have to transform the cube, so that the center lies at the desired corner. To rotate around the lower right corner, you would use something like this:

matrix.tx = cube.width * -0.5;
matrix.ty = cube.height * -0.5;
matrix.rotate( rotationInRadians );

Then you apply the matrix (cube.transform.matrix = matrix) and move the cube back by the negative offset.

For the movement, you could have an array containing all offsets for each corner. Then pick the corner-offset depending on the rotation of the cube/rectangle and movement direction.

The same matrix can be used to transform any point. So you could just use a matrix to transform all corner points to get the coordinates of every corner of the cube/rectangle when it's rotated.

share|improve this answer

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.