Reputation: 3650
I am trying to achieve the following:
A player falls because of gravity
self.physicsWorld.gravity = CGVectorMake(0, -4.0);
When the user touches the screen, the player should ascend
-(void)update:(CFTimeInterval)currentTime {
if(self.isAscending)
{
CGVector relativeVelocity = CGVectorMake(0, 400-self.playerNode.physicsBody.velocity.dy);
self.playerNode.physicsBody.velocity=CGVectorMake(0, self.playerNode.physicsBody.velocity.dy+relativeVelocity.dy*0.05);
}
}
But when I test this on different devices (iPhone 4s, iPhone 6+, iPad, ... ), the object falls and ascends at different speeds.
How do I achieve a consistent speed on all devices?
Upvotes: 2
Views: 381
Reputation: 872
I know one thing I found. I run my MacBook Pro 17" in Retina Mode (960x600). If I run my SpriteKit game, and fire a missile, if I am in Retina Mode, it goes approximately, 3.6 times father, (adjusted velocity). Than if in regular non-hiDPI mode. Which is weird because you are saying if the missile has to travel for more pixels, it should go half as far in retina mode than in non-retina mode. I am looking for how to file a bug, because I believe this is one.
Upvotes: 0
Reputation: 13713
The problem is you are using constants for speed. Remember that the screen density varies between device screens (meaning more pixels for only one logical screen point).
You can use the screen scale property to be applied on your velocity. So for example on an iPhone4 a velocity of 10 pixels per frame will feel the same as velocity of 20 pixels per frame on an iPhone5 screen (since an iPhone5 device has retina display and the number of pixels is doubled)
You can access the scale of the screen as:
CGFloat scale = [[UIScreen.mainScreen] scale];
CGVector relativeVelocity = CGVectorMake(0, (400-self.playerNode.physicsBody.velocity.dy) * scale);
Upvotes: 2