I decided to learn godot and started with a tetris clone to learn on. Everything is going fine except that every once in a while when I rotate a Node2d object, it doesn't set as a whole number. Then when I check each box that makes up a tetromino, I get an intersection because it is just barely...just slightly askew.
Looking for any ideas on how to stop this behavior. I tried forcing by only allowing ints to be added/subtracted but that didn't work
the code is very simple. The variables: boundary => passes in a Rect2 defining the play area playedblocks => an array of individual blocks that have been set from previous pieces get_boundary() => returns the piece's Rect2 based on the individual block positions that make up the tetromino piece
func rotate_piece(boundary,playedblocks):
if(!movable):return
color_blocks(Color.RED)
self.rotation_degrees += int(90)
var isenclosed = boundary.encloses(get_boundary())
var doesintersect = IntersectsPlayedPieces(playedblocks)
if(!isenclosed || doesintersect):
self.rotation_degrees -= int(90)
func IntersectsPlayedPieces(playedblocks)-> bool:
var intersects = false
for setblock in playedblocks:
if(!setblock.is_deleted()):
var setrect = setblock.get_rect()
for currblock in blocks:
var currrect = currblock.get_rect()
if(setrect.intersects(currrect,false)):
intersects = true
return intersects
*edited examples and expounding on findings:
I set the rotation exclusively to be one of 0,90,180,270 and I am now no longer seeing the weird rotation values on the piece. I am seeing weird positional values of the child blocks if the piece is rotated. I added labels on to the blocks so I can see the positional values of each block.
I cleared 12 lines without turning a block to see if it was the movement down function of the blocks that was creating an error. It was not.
However on the 3rd piece when I started rotating pieces, the block highlighted landed with a slightly off x positional value. There is something going on behind the scenes that slightly alters whole number values of positional data after a rotation.
I do have a rotation correction for each block to keep it upright when the parent node is being rotated.
rotation correction code:
func _process(delta):
var parent_rotation = get_parent().rotation
set_rotation(-parent_rotation)
self.rotation_degrees = int(floor(self.rotation_degrees))
var text = str("x: ",self.position.x,"\ny: ", self.position.y)
label.text = text
pic of rotated piece position values after it lands vs. non-rotated pieces:
Because it helped with the problem, I add my idea as an answer, without knowing the exact problem with the underlying math of godot.
It seems like there is a rounding problem, even if you substract an integer from the current rotation.
One solution can be:
This could look like this: