Why it's impossible to do in-object collision detection? - ios

Is there a particular reason why Apple would choose to only allow collision detection through the world, rather than allowing individual objects to detect when they collide with other things? This seems like a horrible design choice, but since I haven't found much complaining I'm guessing there is something I'm missing. So, is there?

Some great comments here and not sure why either. They base the whole thing on nodes and names of nodes - seems like it could be better as looking things up after the fact is slower. Here is how I handled it, similar to what others are saying.
Since I have a bunch of missiles, I just name the node = "Missi" + String(format: "%04d", vIndex), name = "Explo" + String(format: "%04d", vIndex), etc., that way I can prefix what I'm looking for and go directly to my array of nodes and do my thing. I have hundreds of things going, but the collision function is pretty small.
func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact)
{
guard let nodeNameA = contact.nodeA.name else { return }
guard let nodeNameB = contact.nodeB.name else { return }
//print("CONTACT: \(nodeNameA) \(nodeNameB)")
if(nodeNameA.prefix(5) == "Explo" && nodeNameB.prefix(5) == "Enemy")
{
gameControl.defenseMissileHit(vIndex: Int(nodeNameB.suffix(4))!)
}
if(nodeNameA.prefix(5) == "Enemy" && nodeNameB.prefix(5) == "Explo")
{
gameControl.defenseMissileHit(vIndex: Int(nodeNameA.suffix(4))!)
}
if(nodeNameA.prefix(5) == "Explo" && nodeNameB.prefix(5) == "Missi")
{
//print("HIT")
gameControl.enemyMissileHit(vIndex: Int(nodeNameB.suffix(4))!)
}
if(nodeNameA.prefix(5) == "Missi" && nodeNameB.prefix(5) == "Explo")
{
//print("HIT")
gameControl.enemyMissileHit(vIndex: Int(nodeNameA.suffix(4))!)
}
}
I don't subclass nodes, I create classes such as DefenseObject with the stuff I want, then create an array that I can directly access or loop through.
var defenseObjects: [Int:DefenseObject] = [:]
class DefenseObject: ObjectBase
{
var index: Int = 0 // Index of object
var name: String = "" // Object name
var isActive: Bool = false // Object is active
init(vGameType: gameTypes, vCount: Int, position: SCNVector3)
{
super.init(vGameType: vGameType, vIndex: vCount)
isActive = true
name = "Enemy" + String(format: "%04d", vIndex)
}
}
Then I can go right to the index and do defenseObjects[NDex].call(). I also don't try to cleanup the nodes during a wave, I set an isActive switch and hide them. At the end of the wave, I clean 'em up.
Hope that helps.

Related

Using reduce to change struct in Swift

I'm trying to understand how to change a struct (or class?) in an array by using reduce. Creating 4 countdown timers, on tap pause the current timer and start the next. So I tried something like this:
var timer1 = CountdownTimer()
// var timer2 = CountdownTimer() etc.
.onTapGesture(perform: {
var timers: [CountdownTimer] = [timer1, timer2, timer3, timer4]
var last = timers.reduce (false) {
(setActive: Bool, nextValue: CountdownTimer) -> Bool in
if (nextValue.isActive) {
nextValue.isActive = false;
return true
} else {
return false
}
}
if (last) {
var timer = timers[0]
timer.isActive = true
}
})
############# CountdownTimer is a struct ######
struct CountdownTimer {
var timeRemaining: CGFloat = 1000
var isActive: Bool = false
}
This does not work, two errors I'm seeing
the timers in the array are copies of the timers, not the actual timer so changing them doesn't actually change the timers being displayed on screen.
nextValue (i.e. the next timer) can't be changed because it's a let variable in the reduce declaration. I don't know how to change this (or if it's even relevant because presumably it's a copy of the copy of the timer and not the one I actually want to change).
Am I approaching this in a way thats idiomatically wrong for Swift? How should I be changing the original timers?
I agree with Paul about the fact that this should likely all be pulled out into an observable model object. I'd make that model hold an arbitrary list of timers, and the index of the currently active timer. (I'll give an example of that at the end.)
But it's still worth exploring how you would make this work.
First, SwiftUI Views are not the actual view on the screen like in UIKit. They are descriptions of the view on the screen. They're data. They can be copied and destroyed at any time. So they're readonly objects. The way you keep track of their writable state is through #State properties (or similar things like #Binding, #StateObject, #ObservedObject and the like). So your properties need to be marked #State.
#State var timer1 = CountdownTimer()
#State var timer2 = CountdownTimer()
#State var timer3 = CountdownTimer()
#State var timer4 = CountdownTimer()
As you've discovered, this kind of code doesn't work:
var timer = timer1
timer.isActive = true
That makes a copy of timer1 and modifies the copy. Instead, you want WriteableKeyPath to access the property itself. For example:
let timer = \Self.timer1 // Note capital Self
self[keyPath: timer].isActive = true
Finally, reduce is the wrong tool for this. The point of reduce is to reduce a sequence to a single value. It should never have side-effects like modifying the values. Instead, you just want to find the right elements, and then change them.
To do that, it would be nice to be able to easily track "this element and the next one, and the last element is followed by the first." That seems very complicated, but it's surprisingly simple if you include Swift Algorithms. That gives cycled(), which returns a Sequence that repeats its input forever. Why is that useful? Because then you can do this:
zip(timers, timers.cycled().dropFirst())
This returns
(value1, value2)
(value2, value3)
(value3, value4)
(value4, value1)
Perfect. With that I can fetch the first active timer (keypath) and its successor, and update them:
let timers = [\Self.timer1, \.timer2, \.timer3, \.timer4]
if let (current, next) = zip(timers, timers.cycled().dropFirst())
.first(where: { self[keyPath: $0.0].isActive })
{
self[keyPath: current].isActive = false
self[keyPath: next].isActive = true
}
That said, I wouldn't do that. There are subtle requirements here that should be captured in a type. In particular, you have this assumption that there is only one active timer, but nothing enforces that. If that's what you mean, you should make a type that says so. For example:
class TimerBank: ObservableObject {
#Published private(set) var timers: [CGFloat] = []
#Published private(set) var active: Int?
var count: Int { timers.count }
init(timers: [CGFloat]) {
self.timers = timers
self.active = timers.startIndex
}
func addTimer(timeRemaining: CGFloat = 1000) {
timers.append(timeRemaining)
}
func start(index: Int? = nil) {
if let index = index {
active = index
} else {
active = timers.startIndex
}
}
func stop() {
active = nil
}
func cycle() {
if let currentActive = active {
active = (currentActive + 1) % timers.count
print("active = \(active)")
} else {
active = timers.startIndex
print("(init) active = \(active)")
}
}
}
With this, timerBank.cycle() replaces your reduce.
By using the modulus operator ( % ) on Index, we could cycle through last to first without zipping.
let timers = [\Self.timer1, \.timer2, \.timer3, \.timer4]
if let onIndex = timers.firstIndex(where: { self[keyPath: $0].isActive }) {
self[keyPath: timers[onIndex]].isActive = false
let nextIndex = (onIndex + 1) % 4 // instead of 4, could use timers.count
self[keyPath: timers[nextIndex]].isActive = true
}

Objects of same class have the same properties even though declared differently when pushing segue

I've been stuck trying to fix an issue in my application that prevents me from getting my desired result.
When pushing segue to the next view controller, I passed two objects of the same class to var original:[Person] = [] and var edited:[Person] = []
if (segue.identifier == "showSummary") {
let vc = segue.destination as! SummaryViewController
vc.original = myObject
vc.edited = Bill.calculateBill(myObject, switchServiceCharge.isOn, switchGST.isOn)
vc.gst = (switchServiceCharge.isOn, switchGST.isOn)
}
Before calculateBill, the original value of a property inside is 11.
The calculateBill is a class function in another class Bill:
class func calculateBill(_ bill: [Person],_ svcCharge: Bool,_ GST: Bool) -> [Person] {
var mutableBill = bill
for i in 0 ..< mutableBill.count {
for j in 0 ..< mutableBill[i].items.count {
let tax = Bill.getGSTForIndividual(mutableBill[i].items[j].itemPrice, svcCharge, GST)
mutableBill[i].items[j].itemPrice += tax
}
}
return mutableBill
}
class func getGSTForIndividual(_ individualAmt: Decimal,_ svcCharge: Bool,_ GST: Bool) -> Decimal {
var taxCost : Decimal = 0.00
let SERVICE_CHARGE = Bill.getServiceCharge() //0.10
let GOODS_SERVICE_TAX = Bill.getGST() //0.07
if (svcCharge && GST) {
taxCost = individualAmt * SERVICE_CHARGE
taxCost = taxCost + ((individualAmt + taxCost) * GOODS_SERVICE_TAX)
}
else if (!svcCharge && GST) {
taxCost = individualAmt * GOODS_SERVICE_TAX
}
else if (svcCharge && !GST) {
taxCost = individualAmt * SERVICE_CHARGE
}
else {
taxCost = 0.00
}
return taxCost
}
When I did a print() to test whether the properties inside are different, they both yield the same results somehow...
print(original[0].items[0].itemPrice) //12.947000000000000123904, originally 11
print(edited[0].items[0].itemPrice) //12.947000000000000123904
What exactly is going on and why do both the objects have the same properties even though I have declared them differently?
I'm guessing that Person is probably declared as a class, not a struct. This means that it's a reference type, and when you:
var mutableBill = bill
you're not actually making a copy of bill, but rather another reference to the same object. So, when you change the properties on mutableBill, you also change the properties on bill, since they both point to the same object.
The solution is to make an actual copy of bill. The two options are either to declare Person as a struct, in which case you will always get a copy, or else to make an initializer on Person that takes another Person and initializes itself using the passed-in Person's properties:
init(person: Person) { ... }

How to observe a change in a class's property from another class

I've got a question on property observers. There's some example code below. What I want is for the property Analysis.hasChanged to be updated to true if a.value is changed. Is there a way I can do this?
class Number {
var value: Double
init(numberValue: Double) {
self.value = NumberValue
}
}
class Analysis {
var a: Number
var hasChanged = false
init(inputNumber: Number) {
self.a = inputNumber
}
}
testNumber = Number(numberValue: 4)
testAnalysis = Analysis(inputNumber: testNumber)
print(testAnalysis.hasChanged) // will print "false"
testNumber.value = 10
print(testAnalysis.hasChanged) // will still print "false", but I want it to print "true"
In the end, I want the user to be able to be notified if any of their analyses use numbers that have been changed so that they can update the results of the analyses if they choose.
You can use the built-in property observers provided by Swift.
Every time you set a new value, the didSet will be called. You just need to attach the closure, wrapping the desired behaviour, to the Number class
class Number {
var valueDidChangeClosure: (()->())?
var value: Double {
didSet {
//won't call the valueDidChangeClosure
//if the value was changed from 10 to 10 for example..
if oldValue != value {
valueDidChangeClosure?()
}
}
}
init(numberValue: Double) {
self.value = numberValue
}
}
class Analysis {
var a: Number
var hasChanged = false
init(inputNumber: Number) {
self.a = inputNumber
self.a.valueDidChangeClosure = {
self.hasChanged = true
}
}
}
let testNumber = Number(numberValue: 4)
let testAnalysis = Analysis(inputNumber: testNumber)
print(testAnalysis.hasChanged) // will print "false"
testNumber.value = 10
print(testAnalysis.hasChanged) // will print "true"
I would do something like this, I apologize in advance if I have some syntax wrong (I usually use C/C++, think of this as more psudo code since you'd have to have a way to copy Number classes, etc.).
class Number {
var value: Double
init(numberValue: Double) {
self.value = NumberValue
}
}
class Analysis {
var a: Number
var _a: Number
bool hasChanged() {
if (a != _a) {
_a = a
return true;
}
return false;
}
init(inputNumber: Number) {
self.a = inputNumber
self._a = self.a
}
}
testNumber = Number(numberValue: 4)
testAnalysis = Analysis(inputNumber: testNumber)
print(testAnalysis.hasChanged()) // will print "false"
testNumber.value = 10
print(testAnalysis.hasChanged()) // will still print "false", but I want it to print "true"
In the end, I want the user to be able to be notified if any of their analyses use numbers that have been changed so that they can update the results of the analyses if they choose.
I don't know if this really addresses that question, I based my answer off of the code you provided. So there may be additional functionality if you want there to be some triggering method (instead of calling .hasChanged()).
Comparing doubles (and any other floating point type) with '=' or '!=' is not a good idea.
Use epsilon function instead.
Details: jessesquires.com/blog/floating-point-swift-ulp-and-epsilon/

How to create a random range for enemies to appear?

I'm making a game where hero should jump over the enemies using Sprite Kit and Swift. I have 4 types of them but they're basically the same. When I coded appearance of enemies I thought it would be ok if they could appear in group but now I see that it's not very nice. How can I make a random range between enemies to appear? It should provide enough space for hero to land from a jump at least. I tried but end up with something bizarre.
That's how I'm spawning them:
func random() -> UInt32 {
var time = UInt32(60)..<UInt32(200)
return time.startIndex + arc4random_uniform(time.endIndex - time.startIndex + 1)
}
Then I have different file for appearance:
class EnemyAppear {
var nowAppear = false
var waitToAppear = UInt32(0)
var appearInterval = UInt32(0)
init(nowAppear:Bool, waitToAppear:UInt32, appearInterval:UInt32) {
self.nowAppear = nowAppear
self.waitToAppear = waitToAppear
self.appearInterval = appearInterval
}
func shouldRun() -> Bool {
return self.appearInterval > self.waitToAppear
}
}
then I have a function to run them on screen
func enemyRun() {
for(enemy, enemyAppear) in self.enemyStatus {
var thisPet = self.childNodeWithName(enemy)!
if enemyAppear.shouldRun() {
enemyAppear.waitToAppear = random()
enemyAppear.appearInterval = 0
enemyAppear.nowAppear = true
}
And then for each enemy I have a "Status"
var enemyStatus:Dictionary<String,EnemyAppear> = [:]
enemyStatus["mouse"] = EnemyAppear(nowAppear: false, waitToAppear: random(), appearInterval: UInt32(0))
I can't understand how to make them appear just like that but with an empty space between each other and where to put it.

Swift Optional Checking "Unexpectedly found nil"

I'm not sure why Swift is complaining that list1 = list1!.next unexpectedly found nil when my if statement checks if list1 != nil. Could anyone explain why checking if list1 != nil is not enough? I've tried changing it to
if list1 {
list1 = list1!.next
}
but it suggested that I check using != nil
Here's a gist
Here's my code:
class Node {
var data: Int
var next: Node?
init (data: Int) {
self.data = data
}
}
func addReversed (var list1: Node?, var list2: Node?) -> Node {
var carry = 0
var head: Node? = nil
var prev: Node? = nil
while list1 != nil || list2 != nil {
var newNode = addNode(list1, list2, &carry)
appendNode(head, prev, newNode)
if list1 != nil {
list1 = list1!.next
}
if list2 != nil {
list2 = list2!.next
}
}
if carry > 0 {
var carryNode = Node(data: carry)
appendNode(head, prev, carryNode)
}
return head!
}
This is not what you asked, but I think it may have a lot to do with the problem. I tested your Node and your basic addReversed code and I couldn't get a crash. But at that time I didn't have any of your other global functions, so I had to just comment them out while testing. Since I didn't crash, therefore I came to believe that the problem is in one of those global functions.
Then you published your gist, with the code to those global functions, and I can see immediately that this code is deeply flawed:
func appendNode(var head: Node?, var prev: Node?, node: Node) {
if head == nil {
head = node
}
if prev == nil {
prev = node
}
else {
prev!.next = node
prev = node
}
}
You seem to think that saying e.g. prev = node will replace the node that came in as prev with the node that came in as node in some outside world. It will not. These are the names of local variables only. So none of that code has any effect at all - except for prev!.next = node, because there you are mutating an instance that exists also in the outside world (because this is a reference type, a class instance).
If you want to replace the object that came in on a parameter with another object, you need an inout parameter.
For example:
class Dog {
let name:String
init(_ s:String) {
name = s
}
}
func replaceDog(var d:Dog) {
d = Dog("Rover") // this is what you are doing
}
var d = Dog("Fido")
replaceDog(d)
d // still Fido
But:
class Dog {
let name:String
init(_ s:String) {
name = s
}
}
func replaceDog(inout d:Dog) {
d = Dog("Rover")
}
var d = Dog("Fido")
replaceDog(&d)
d // Rover
Your code is full of this same false assumption, and I think, although I have not worked out the details, that this assumption is the cause of your difficulty here. My guess is that you got this linked list implementation from some other language that has pointers, but you are not using pointers in your implementation. The way to use pointers in Swift is with inout!

Resources