2
\$\begingroup\$

We're making a Tetris like game and need help rotating shapes.

Moving the shape works when there is no rotation.

However, after rotation, snapping the shape to the grid fails because the vector between the shape's current position and destination is affected by the amount of rotation (e.g., rotated 90 degrees).

As a simple example, let's assume a shape consists of one square and currently sits at (8, 0).

If the user rotates the shape 90 degrees (ignore that this functionally does nothing since the shape is only one square) then wants to move the shape to (1,0), what should the vector be? And how do you generalize the vector math?

We're using Swift 3 for an iOS game.

    if isShapePosValid(shape: curShape) {
        let firstTile = curShape.getLowestTile()
        let nearestBoardTile = getNearestBoardTile(shapeTile: firstTile)!
        let newTilePos = firstTile.parent!.convert(nearestBoardTile.position, from: nearestBoardTile.parent!)
        let moveVector = CGVector(point: newTilePos - firstTile.position)
        let move = SKAction.move(by: moveVector, duration: 0.1)
        curShape.run(move) {
            self.printShapeToBoard(shape: curShape)
        }
    } 

fileprivate func rotateShape(shape: BoardShapeSprite) {
    let rotate = SKAction.rotate(byAngle: CGFloat(Double.pi/2), duration: 0.1)
    shape.run(rotate)
}

fileprivate func printShapeToBoard(shape: ShapeSprite) {
    let tiles = shape.getTiles().filter{ $0.value != .Empty }

    for t in tiles {
        let boardTile = getNearestBoardTile(shapeTile: t)
        boardTile?.setValue(value: .Taken)
    }
}
\$\endgroup\$
  • \$\begingroup\$ Can you show us the code which does the rotation? \$\endgroup\$ – Philipp Jul 31 '17 at 11:27
  • \$\begingroup\$ @Philipp yup, just added \$\endgroup\$ – GameNoviceDev01 Jul 31 '17 at 17:04
1
\$\begingroup\$

Are you using a game engine? you didn't clarify that. (I assume you are using Unity) You should change the tetris piece's world position instead of moving in the direction of the right or forward vector. Or you make the tetris piece the child of another object and move the parent instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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