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 angle, scale and origin on FlxStrip #2973

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions flixel/FlxCamera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,8 @@ class FlxCamera extends FlxBasic
}

public function drawTriangles(graphic:FlxGraphic, vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>,
?position:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false, ?transform:ColorTransform, ?shader:FlxShader):Void
?position:FlxPoint, angle:Float = 0, ?scale:FlxPoint, ?origin:FlxPoint, ?blend:BlendMode, repeat:Bool = false, smoothing:Bool = false,
?transform:ColorTransform, ?shader:FlxShader):Void
{
if (FlxG.renderBlit)
{
Expand Down Expand Up @@ -932,13 +933,13 @@ class FlxCamera extends FlxBasic
var isColored:Bool = (colors != null && colors.length != 0);

#if !flash
var hasColorOffsets:Bool = (transform != null && transform.hasRGBAOffsets());
final hasColorOffsets:Bool = (transform != null && transform.hasRGBAOffsets());
isColored = isColored || (transform != null && transform.hasRGBMultipliers());
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend, hasColorOffsets, shader);
drawItem.addTriangles(vertices, indices, uvtData, colors, position, _bounds, transform);
final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend, hasColorOffsets, shader);
drawItem.addTriangles(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds, transform);
#else
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend);
drawItem.addTriangles(vertices, indices, uvtData, colors, position, _bounds);
final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(graphic, smoothing, isColored, blend);
drawItem.addTriangles(vertices, indices, uvtData, colors, position, angle, scale, origin, _bounds);
#end
}
}
Expand Down
5 changes: 3 additions & 2 deletions flixel/FlxStrip.hx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ class FlxStrip extends FlxSprite

getScreenPosition(_point, camera).subtractPoint(offset);
#if !flash
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, blend, repeat, antialiasing, colorTransform, shader);
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, angle, scale, origin, blend, repeat, antialiasing, colorTransform,
shader);
#else
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, blend, repeat, antialiasing);
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, angle, scale, origin, blend, repeat, antialiasing);
#end
}
}
Expand Down
37 changes: 34 additions & 3 deletions flixel/graphics/tile/FlxDrawTrianglesItem.hx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package flixel.graphics.tile;

import flixel.math.FlxAngle;
import flixel.FlxCamera;
import flixel.graphics.frames.FlxFrame;
import flixel.graphics.tile.FlxDrawBaseItem.FlxDrawItemType;
import flixel.math.FlxMatrix;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.math.FlxMath;
import flixel.system.FlxAssets.FlxShader;
import flixel.util.FlxColor;
import openfl.display.Graphics;
Expand All @@ -21,6 +23,8 @@ typedef DrawData<T> = #if (flash || openfl >= "4.0.0") openfl.Vector<T> #else Ar
class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
{
static var point:FlxPoint = FlxPoint.get();
static var size:FlxPoint = FlxPoint.get();
static var origin:FlxPoint = FlxPoint.get();
static var rect:FlxRect = FlxRect.get();

#if !flash
Expand Down Expand Up @@ -137,17 +141,24 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
}

public function addTriangles(vertices:DrawData<Float>, indices:DrawData<Int>, uvtData:DrawData<Float>, ?colors:DrawData<Int>, ?position:FlxPoint,
angle:Float = 0, ?scale:FlxPoint, ?originPoint:FlxPoint,
Geokureli marked this conversation as resolved.
Show resolved Hide resolved
?cameraBounds:FlxRect #if !flash , ?transform:ColorTransform #end):Void
{
if (position == null)
position = point.set();

if (size == null)
scale = size.set(1, 1);

if (originPoint == null)
originPoint = origin.set();

if (cameraBounds == null)
cameraBounds = rect.set(0, 0, FlxG.width, FlxG.height);

var verticesLength:Int = vertices.length;
var prevVerticesLength:Int = this.vertices.length;
var numberOfVertices:Int = Std.int(verticesLength / 2);
var numberOfVertices:Int = Std.int(verticesLength * 0.5);
Geokureli marked this conversation as resolved.
Show resolved Hide resolved
var prevIndicesLength:Int = this.indices.length;
var prevUVTDataLength:Int = this.uvtData.length;
var prevColorsLength:Int = this.colors.length;
Expand All @@ -157,10 +168,30 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
var i:Int = 0;
var currentVertexPosition:Int = prevVerticesLength;

var _cos:Float = 1.0;
var _sin:Float = 0.0;
Geokureli marked this conversation as resolved.
Show resolved Hide resolved
if (angle != 0)
{
_cos = FlxMath.fastCos(angle * FlxAngle.TO_RAD);
_sin = FlxMath.fastSin(angle * FlxAngle.TO_RAD);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you be willing to compare this with using Math.sin and cos to see if the lack of precision actually a noticeable difference, be sure to try angles > 360, too please

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the rest, but I'd still like you to test the precision of FastSin and cos

}

while (i < verticesLength)
{
tempX = position.x + vertices[i];
tempY = position.y + vertices[i + 1];
var vertX:Float = (vertices[i] * scale.x) - originPoint.x;
var vertY:Float = (vertices[i + 1] * scale.y) - originPoint.y;

if (angle != 0)
{
final _vx:Float = vertX;
final _vy:Float = vertY;
Geokureli marked this conversation as resolved.
Show resolved Hide resolved

vertX = (_vx * _cos) + (_vy * -_sin);
vertY = (_vx * _sin) + (_vy * _cos);
}

tempX = position.x + vertX;
tempY = position.y + vertY;

this.vertices[currentVertexPosition++] = tempX;
this.vertices[currentVertexPosition++] = tempY;
Expand Down
Loading