GMS 2 Mirror effect, reflections without using shaders

TheHatter

Member
I was wondering how to do reflexes without using shaders.

wanted to do something like this:

50e3efac5cf65c2a8bc67ea874be7980.png



Thank you very much in advance
 

FoxyOfJungle

Kazan Games
Drawing the front sprite using a surface, in the same X and Y position of the player, but with the Y slightly higher (more negative)
 

obscene

Member
If you want to do THAT specifically, it's just simply drawing the sprite of the player again inside a small surface. You'll just have to write a bit of code to get the player's x and y as well as his sprite so you can display an alternate reverse sprite.
 

TheHatter

Member
If you want to do THAT specifically, it's just simply drawing the sprite of the player again inside a small surface. You'll just have to write a bit of code to get the player's x and y as well as his sprite so you can display an alternate reverse sprite.
I've never worked with surfaces, I have no idea how to do.

would it be possible to create a reflection area not only for the player, but for everything in that area?
 

TheHatter

Member
Drawing the front sprite using a surface, in the same X and Y position of the player, but with the Y slightly higher (more negative)
I've never worked with surfaces, I have no idea how to do.

would it be possible to create a reflection area not only for the player, but for everything in that area?
 

obscene

Member
You have to understand, that is an effect. Nothing is being reflected. If it were "reflected" in the normal sense of how you do graphics effects, you would see the back of his head again. That is custom code simply drawing the player again but from the other side so it looks like a reflection. You could draw whatever you want in there.

But is this really what you are wanting to do? What kind of reflection are you making because it might be done entirely differently.

All a surface is you are drawing inside of a rectangle which in this case makes the player look cropped and truly inside of it. It's as simple as this...

Create a surface in the create event of an object
mysurface=surface_create(w,h);

Draw in the surface in the draw event
surface_set_target(mysurface);
draw_clear(c_black); // Clear the surface to erase the previous frame
//draw stuff
surface_reset_target();
draw_surface(x,y,mysurface);

And destroy the surface in the cleanup event
surface_free(mysurface);

There's a bit more to it but start here and you'll figure out the rest.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Old games would handle mirrors (and water reflections, etc) by having two copies of the scene, with one being flipped. Players and other moving entities have a special mirror copy that mirrors the player's movement (usually areas are restricted so only the player can enter the mirror room, to cut down on the amount of objects that needs to be in the mirror). Water reflections usually would have a gradient to dark green or black so it looks like the terrain fades away in the murkiness, which helps the illusion.
 

chamaeleon

Member
Can use draw_sprite_part() or draw_sprite_part_ext() for the reflection as well, drawing only the part that is within the scope of the mirror. Bounds required left as an exercise for the interested reader.
 

Gradius

Member
I think everyone here is overthinking it, and it'd be far easier just to have a section of wall with a cutout where the mirror is, then just draw the player sprite behind it. No need to mess with surfaces or alpha writes.
 

FoxyOfJungle

Kazan Games



GML:
// draw the mirror sprite
draw_sprite(sprite_index,0,x,y);


// mirror position
_xx = x+32;
_yy = y+32;


// create the mirror
_surf = surface_create(52,18); // mirror size

surface_set_target(_surf);
draw_clear_alpha(c_black,0);
draw_sprite(spr_player,1,obj_player.x - _xx, (_yy - obj_player.y) + 64); // draw player inverted
surface_reset_target();

draw_surface(_surf,_xx,_yy); // draw mirror
surface_free(_surf) // clear mirror





You can also draw anything you want inside the mirror.
 

TheHatter

Member



GML:
// draw the mirror sprite
draw_sprite(sprite_index,0,x,y);


// mirror position
_xx = x+32;
_yy = y+32;


// create the mirror
_surf = surface_create(52,18); // mirror size

surface_set_target(_surf);
draw_clear_alpha(c_black,0);
draw_sprite(spr_player,1,obj_player.x - _xx, (_yy - obj_player.y) + 64); // draw player inverted
surface_reset_target();

draw_surface(_surf,_xx,_yy); // draw mirror
surface_free(_surf) // clear mirror





You can also draw anything you want inside the mirror.
if it's not too much trouble, can i ask a question?
in draw_sprite why subtract obj_player.x - _xx. I can't understand. Because x in draw_sprite is aligned with that of the player in the game, why with just obj_player.x doesn't it work? because the sprite drawing follows x from obj_player :(
 

FoxyOfJungle

Kazan Games
if it's not too much trouble, can i ask a question?
in draw_sprite why subtract obj_player.x - _xx. I can't understand. Because x in draw_sprite is aligned with that of the player in the game, why with just obj_player.x doesn't it work? because the sprite drawing follows x from obj_player :(
Because it is necessary to subtract the player's position from the position of the surface, since the surface is drawn in position 0.

 

muki

Member
The beauty of this thread is that it shows there are usually several solutions!

I replayed Celeste recently and was really intrigued how they did the mirror temple wall reflections. Sort of a fragmented broken mirror version of the sprites, looked really great! Probably using a normalmap.
 

TheHatter

Member
Because it is necessary to subtract the player's position from the position of the surface, since the surface is drawn in position 0.

I simulated, basically what happens to x and y from the draw_sprite as I understand it and x doesn't match.
sorry for all
sorry, I don't know what happens to x in my mind.

y:
ystudy.png

x:
xstudy.png
the reflection does not match

*the pink square is a representation of obj_player
 
Last edited:
Top