c ++ – How to place a square after a collision in 2D?

I program in C ++ a 2D game from scratch, for now, I have two blue squares that will be moving object and brown rectangles that will be the ground, collisions will be detected but I do not know not how to locate the celestial square so that it does not cross the brown rectangle.

Until now, I was detecting which side of the object the collision was and I was calculating its new position. For example, if it hits in its new position, Y will be Y = (position "Y" of the brown rectangle) – (height of the celestial square). I want to know if there is a better way to do the same thing.
(the X axis is horizontal and the Y axis is vertical, the x, y point of the object is the upper right corner)

That's what I wear, the brown objects are fixed and the celestial objects 2 are the ones that move, the brown squares turn green to indicate that it collides.
enter the description of the image here