0

Running into an issue trying to use the radial gravity fields in SpriteKit

I don't want objects of same kind (as define by categoryBitMask to attract each others)

Here's how I do it :

struct PhyscisCategory {
    static let None : UInt32 = 0
    static let All : UInt32 = UInt32.max
    static let Star : UInt32 = 0b1
    static let Planet : UInt32 = 0b10
}
<....>
 Planet1.physicsBody!.categoryBitMask = PhyscisCategory.Planet
 Planet1.physicsBody!.fieldBitMask = PhyscisCategory.Star

 Planet2.physicsBody!.categoryBitMask = PhyscisCategory.Planet
 Planet2.physicsBody!.fieldBitMask = PhyscisCategory.Star

Whatever I tried Planets are always mutually attracted (except if I set the fieldBitMask to 0, but they are of course not anymore attracted by Star either) ! I would expect of this 2 lines of code that only stars gravity fields would behave an effect on planets...

from the documentation :

"fieldBitMask : A mask that defines which categories of physics fields can exert forces on this physics body. When a physics body is inside the region of an SKFieldNode object, that field node’s categoryBitMask property is compared to this physics body’s fieldBitMask property by performing a logical AND operation. If the result is a nonzero value, the field node’s effect is applied to the physics body."

Am I doing anything wrong ?

Xav
  • 270
  • 3
  • 13
  • Well problem seems deeper : I made some additional tests and the fieldBitMask seems totally ignored except when it is set to 0 or all 1.... (I'm targeting ios 9 App in xcode 7) – Xav Sep 25 '15 at 12:35
  • May want to use this as a workaround. http://stackoverflow.com/a/31502698/2158465 – Epic Byte Sep 25 '15 at 15:39
  • Thanks for the advise... Working fine reimplementing the gravity field and the categoryBitMask&fieldBitMask check... – Xav Sep 30 '15 at 16:31

1 Answers1

1

Your masks are out of whack. In binary, they look like this:

0000 0000 1011 0001 (stars)
0000 1011 0001 0000 (planets)

A logical AND of those two yields a non-zero value :

0000 0000 0001 0000

So if SpriteKit evaluates your planet's field bitmask with another planet's category bitmask, they will attract each other. Try those masks instead:

static let Star : uint32_t = 0x1 << 0
static let Planet : uint32_t = 0x1 << 1

(Not sure if that is valid swift code, but you get the idea). You can extend the masks by always left-shifting one bit further. These masks will then always yield a zero value if being AND-ed.

Double M
  • 1,449
  • 1
  • 12
  • 29
  • nope they're ok... :) 0b1 = ..0000001 0b10 = ..0000010 – Xav Oct 09 '15 at 08:36
  • But each hex digit converts to four binary digits. 1 = 0001, b = 1011. Have you tried the shifting method? I think one of us gets something wrong. – Double M Oct 09 '15 at 08:56
  • 1
    it's not hex notation (0x...) but swift binary notation (0b....) they are UINT32 (32 bits) 0b1 = 1 0b10 = 2 0b100 = 4 – Xav Oct 09 '15 at 09:06
  • Ohhh I see. My bad, then :) I should really start learning Swift. – Double M Oct 09 '15 at 09:55