Implementing mirror


(B6ka) #1

Hi is there an easy way to implement a mirror in a platformer? It should be a rectangular surface that reflects player or enemy sprite that walks by its side.


(Martí Angelats i Ribera) #2

How about rendering the player in the surface with an offset?


(rostok) #3

Why not use it as top level entity that copies some part of the frame buffer? You could have this entity have graphic Image with say pink pixels being actual mirror. Then you could overrider render() and go through all the area and overwrite pink pixels by copying colors from other areas of the screen. Hope this is clear enough. Show us some screenshots or sketches.


(Martí Angelats i Ribera) #4

The problem with this is that you actually have to render everything that’s in front of the mirror again.


(B6ka) #5

Maybe it is a bad sketch, but I am thinking something like this

I add a new entity and want to copy pixels from a certain area say x-100, y same. How do I copy the pixels from an area? What is the code?


(Martí Angelats i Ribera) #6

As i said, i recomed you to render the player in the BitmapData after the backgroud had been added.

public class Mirror extends Graphic
{
	
	public function Mirror(background:*, reflected:Entity = null, offsetX:Number = 20, offsety:Number = 0)
	{
		if (background == null) return; //error
		else if (background is Class)
		{
			_background = FP.getBitmap(background);
		}
		else if (background is BitmapData)
		{
			_background = background;
		}
		else return; //error
		
		_rect = _background.rect;
		_source = new BitmapData(_rect.width, _rect.height, true, 0);
		
		_mirrorMatrix = new Matrix( -1, 0, 0, 1, _rect.width, 0); //mirror in the x axis
		
		_reflected = reflected;
		_offset = new Point(offsetX, offsetY);
	}
	
	override public function render(target:BitmapData, point:Point, camera:Point):void
	{
		_point.x = point.x + x - camera.x * scrollX;
		_point.y = point.y + y - camera.y * scrollY;
		
		target.copyPixels(_background, _rect, _point, null, null, true);
		
		_source.fillRect(_rect, 0);
		_reflected.graphic.render(_source, _offset, _zero);
		
		_source.draw(_source, _mirrorMatrix, null, null, _rect);
		target.copyPixels(_source, _rect, _point, null, null, true);
	}
	
	private var _background:BitmapData;
	private var _rect:Rectangle;
	private var _source:BitmapData;
	private var _reflected:Entity;
	private var _offset:Point;
	private var _point:Point;
	
	private var _zero:Point = new Point;
	private var _mirrorMatrix:Matrix;
}

(Zachary Lewis) #7

There’s no “the code” to do something like this. It all depends on how you’ve set your game up.


(B6ka) #8

@Copying Thank you very much this is very helpful :slight_smile:

@zachwlewis No code for general issue, but there are so many experienced programmers here that are very kind to share the way they would approach it :slight_smile: