Simulator vs Device Contact Point Conversion Differences?

39 views Asked by At

I have been struggling with this for a while now and cant seem to find the issue.

I have an SKScene which i will refer to as self, and an SKNode that is called chapterScene that gets added to self. I have a boundary set up that contains a movable character. Here is how I have set this up

ViewController.m (the controller that presents the SKScene subclass OLevel

- (void)viewDidLoad {
    [super viewDidLoad];

    // Configure the view.
    SKView *skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    scene = [OLevel sceneWithSize:self.view.frame.size];
    scene.scaleMode = SKSceneScaleModeAspectFit;

    // Present the scene.
    [skView presentScene:scene];

    // Do things after here pertaining to initial loading of view.
}

Here is my OLevel.m

- (id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {
        NSLog(@"Creating scene");

        [self setUpScene];
    }

    return self;
}

- (void)setUpScene {

    NSLog(@"Setting up scene");
    //self.speed = 0.9f;

#pragma 1 Set up scene

    // Set up main chapter scene
    self.anchorPoint = CGPointMake(0.5, 0.5); //0,0 to 1,1
    chapterScene = [SKNode node];
    chapterScene.position = CGPointZero;
    chapterScene.name = @"chapterScene";
    [self addChild:chapterScene];


    // Set up physics boundary
    self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);
    self.physicsWorld.contactDelegate = self;
    .
    .
    .
}

The main point here, is that ultimately I have set up my scene and its child nodes correctly (as I expected until of late). When I run on the simulator (iPhone 6), I am using the - (void)didBeginContact:(SKPhysicsContact *)contact method to monitor and collisions. Whenever contact does begin, I am logging the following

CGPoint contactPoint = contact.contactPoint;
NSLog(@"non conv: %f, %f", contactPoint.x, contactPoint.y);
CGPoint sceneContactPoint = [self convertPoint:contactPoint toNode:chapterScene];
NSLog(@"1 conv pt: %f, %f", sceneContactPoint.x, sceneContactPoint.y);

I am also logging the characters position as well to make sure that this converted point is correct.

When I run this on the simulator, and the moving node character hits the wall, I get this:

2016-02-25 20:02:31.102 testGame[43851:14374676] non converted point: 0.143219, 29.747963
2016-02-25 20:02:31.102 testGame[43851:14374676] 1 conv pt: -140.206223, 615.699341
2016-02-25 20:02:31.102 testGame[43851:14374676] Player hit the wall
2016-02-25 20:02:31.103 testGame[43851:14374676] player pos: -140.206238, 590.749268

which is quite correct.

HOWEVER, for whatever reason I can not seem to find, this is the exact same code running on my iPhone 5C...

2016-02-25 20:04:50.062 testGame[2907:1259447] non converted point: 160.337631, 310.808350
2016-02-25 20:04:50.063 testGame[2907:1259447] 1 conv pt: 70.996162, 900.004272
2016-02-25 20:04:50.064 testGame[2907:1259447] Player hit the wall
2016-02-25 20:04:50.065 testGame[2907:1259447] player pos: -89.003845, 593.984009

I really hope this is a simple fix that I am missing. If anyone can help me I would greatly appreciate it. thanks

UPDATE It appears that all that is happening is that when I run it on the simulator, the point is being referenced from the center of the screen (0,0) whereas on the device, the reference point is the true origin, the top left corner being (0,0), with the center being, in the iPhone 5c's case, (160, 284). Still not sure how to correct this...or why it is even happening.

So far this is the only solution I can think of...

if (!TARGET_OS_SIMULATOR) {
    contactPoint = CGPointMake(sceneContactPoint.x - screenBounds.size.width/2.0f, sceneContactPoint.y - screenBounds.size.height/2.0);
}
else {
    contactPoint = CGPointMake(sceneContactPoint.x, sceneContactPoint.y);
}

But this is embarrassing. Either this is a bug with Xcode or Apple, or there is reason this is happening and a different solution.

0

There are 0 answers