user3661721
user3661721

Reputation: 31

iOS UIBezierPath is upside down when rendered using SpriteKit?

A track path is defined using data points with an origin (0,0) at top/left. From these points a UIBezierPath is created.

The cars movement along the path is given as distance, from which percent is calculated. A category on UIBezierPath provides the coordinates for the car along the path.

#import "UIBezierPath-Points.h"
CGPoint currCoordinates = [self.trackPath pointAtPercent:percent withSlope:nil];

The problem is that SpriteKit renders the track path upside-down.

In a SKScene class …

SKShapeNode *shapeNode = [[SKShapeNode alloc] init];
[shapeNode setPath:self.trackPath.CGPath];            <== path is rendered upside-down?
[shapeNode setPath:cgPath];
[shapeNode setStrokeColor:[UIColor blueColor]];
[self addChild:shapeNode];

I have attempted various transforms on the UiBezierPath, but cannot get the coordinates to convert to SpriteKit coordinates which I believe are centre based.

Does anyone know how to convert from UIBezierPath coordinates to SpriteKit (SKScene) coordinates?

*don't have enuf reputation to post image.

Upvotes: 3

Views: 759

Answers (1)

Jorge OM
Jorge OM

Reputation: 79

Excuse me was thinking that it was self-explanatory.

from Source

void ApplyCenteredPathTransform(UIBezierPath *path, CGAffineTransform transform)
{
    CGRect rect = CGPathGetPathBoundingBox(path.CGPath); 
    CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
    CGAffineTransform t = CGAffineTransformIdentity;
    t = CGAffineTransformTranslate(t, center.x, center.y);
    t = CGAffineTransformConcat(transform, t);
    t = CGAffineTransformTranslate(t, -center.x, -center.y);
    [path applyTransform:t];
}

void ScalePath(UIBezierPath *path, CGFloat sx, CGFloat sy)
{
    CGAffineTransform t = CGAffineTransformMakeScale(sx, sy);
    ApplyCenteredPathTransform(path, t);
}

then

ScalePath(path, 1.0, -1.0);

Upvotes: 2

Related Questions