Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix borders on rotating camera #2679

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

NeeEoo
Copy link
Contributor

@NeeEoo NeeEoo commented Nov 22, 2022

This fixes the black borders that appear when you rotate a FlxCamera, since it rotates the openfl Sprite which the camera renders to.

Reason it's a draft is because i haven't been able to test it on blitting render, and might need some testing since isOnScreen probably doesn't account for the camera angle.

@Raltyro
Copy link

Raltyro commented Nov 22, 2022

maybe make it so your still able to change the flashsprite rotation, maybe FlxCamera.rotation?

@Raltyro
Copy link

Raltyro commented Nov 22, 2022

and also make the sprites able to render inside the bound that has angle applied, cause it seems like any sprites outside the original bound won't render even if it's in the camera view

@Geokureli
Copy link
Member

@NeeEoo, Do you have a test case for this? I don't see how I'm supposed to change _sinAngle and _cosAngle

@NeeEoo
Copy link
Contributor Author

NeeEoo commented Dec 29, 2022

It just changes how the angle property looks like visually by making it not rotate the openfl sprite that the camera uses

@Geokureli
Copy link
Member

So I made this test case:
https://github.com/Geokureli/flixel-tests/blob/main/Source/states/CameraAngleTestState.hx

I think I misunderstood the purpose for this. I assumed the goal was to actually rotate the camera's view space, but after trying this out, it seems it was literally made to "prevent the black borders when rotating the camera", not to show FlxSprites in that region

If that's all you really wanted to do, all you need to do is:

final bgColor = 0xFF404040;
FlxG.cameras.bgColor = bgColor;
FlxG.stage.color = bgColor;

@NeeEoo
Copy link
Contributor Author

NeeEoo commented Dec 29, 2022

It was meant to show FlxSprites in that rotated space, but i haven't figured out the code in the isOnScreen to show the stuff outside of the view.

Aka rotating the camera view

@Starmapo
Copy link
Contributor

I'm using this in my fork of HaxeFlixel and I made these changes:

// Modified to take angle into account
function transformRect(rect:FlxRect):FlxRect
{
  if (FlxG.renderBlit)
  {
	  rect.offset(-viewMarginX, -viewMarginY);
  
	  if (_useBlitMatrix)
	  {
		  rect.x *= zoom;
		  rect.y *= zoom;
		  rect.width *= zoom;
		  rect.height *= zoom;
  
		  if (angle != 0)
		  {
			  var origin = FlxPoint.weak(FlxMath.lerp(viewMarginLeft, viewMarginRight, 0.5), FlxMath.lerp(viewMarginTop, viewMarginBottom, 0.5));
			  origin.x -= FlxMath.lerp(rect.left, rect.right, 0.5);
			  origin.y -= FlxMath.lerp(rect.top, rect.bottom, 0.5);
			  rect.getRotatedBounds(angle, origin, rect);
		  }
	  }
  }
  
  return rect;
}

// Modified to take angle into account
public function getViewMarginRect(?rect:FlxRect)
{
  if (rect == null)
	  rect = FlxRect.get();
  
  rect.set(viewMarginLeft, viewMarginTop, viewWidth, viewHeight);
  if (angle != 0)
  {
	  rect.getRotatedBounds(angle, FlxPoint.weak(FlxMath.lerp(rect.left, rect.right, 0.5), FlxMath.lerp(rect.top, rect.bottom, 0.5)), rect);
  }
  return rect;
}

public inline function containsPoint(point:FlxPoint, width:Float = 0, height:Float = 0):Bool
{
  var viewRect = getViewMarginRect();
  var contained = (point.x + width > viewRect.left) && (point.x < viewRect.right) && (point.y + height > viewRect.top) && (point.y < viewRect.bottom);
  point.putWeak();
  viewRect.put();
  return contained;
}

public inline function containsRect(rect:FlxRect):Bool
{
  var viewRect = getViewMarginRect();
  var contained = (rect.right > viewRect.left) && (rect.x < viewRect.right) && (rect.bottom > viewRect.top) && (rect.y < viewRect.bottom);
  rect.putWeak();
  viewRect.put();
  return contained;
}

and it works perectly in tile render mode. I haven't tested it in blit render mode however.
I don't know how to request these changes to the pull request, so I'm just putting them here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants