half-potato
half-potato

Reputation: 1027

Floats SpriteKit

Why does SpriteKit use floats for position even though there can't be a decimal point in pixels?

I am building a game in SpriteKit and am wondering whether I should make the character position in ints to optimize it but am not sure whether there will be any performance improvements.

Is it worth converting the character positions to ints to optimize?

Upvotes: 2

Views: 597

Answers (3)

BadgerBadger
BadgerBadger

Reputation: 696

The other answers are good, but keep in mind that you are NOT working in pixels: you are working in points. Depending on the device, points will hold more or less pixels. The reason behind that was to accommodate the retina displays that had the save physical size. You work with points, and when you add pixels, everything still lines up because the number of pixels per point increased, but the number of points in the screen stayed the same.

Upvotes: 2

Jozey
Jozey

Reputation: 1740

SpriteKit uses floats because floats are not that huge, and numbers you will be using will not be as huge either since the max size for the iPad in itself is 2048 by 1536, there is no need to declare them as integers when floats work just fine for those sizes.

There is no use converting to integers. I've made a game in SpriteKit and it works just fine using CGFloats.

If you convert to integers, do note that almost anything dealing with a sprite's image or object's size will be in floats.

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 385680

In general, it's simpler to use floats than integers when dealing with graphics above the bit-twiddling level. Note that Core Graphics also uses floats for coordinates.

The reason it's simpler is that you want to do things like scale (by a non-integer factor) and rotate coordinates, images, paths, etc. If you use integers, the rounding errors accumulate by a noticeable amount quickly. If you use floats, the rounding errors take much longer to become noticeable - usually they don't become noticeable at all.

Also consider that an SKNode has xScale and yScale properties. If you scale up a node, the fractional part of its children's positions can have a large effect on their positions on the screen.

You should not convert your character positions to ints. SpriteKit uses floats, so you'll end up converting them back to floats to use them with SpriteKit, the CPU has floating point support, and the GPU is designed to perform massive amounts of floating point arithmetic.

Upvotes: 17

Related Questions