Get collision component of source Blender model - swift

I have a blender import model with a simple plane with cylinders on it.
Now i want a collision component on the cylinders.
The imported model and children form blender printed out if i start the app. This is working fine but no collision on the cylinders
▿ '/' : Entity, children: 1
⟐ Transform
⟐ SynchronizationComponent
▿ 'cilinders' : Entity, children: 6
⟐ Transform
⟐ SynchronizationComponent
▿ 'Light' : Entity
⟐ Transform
⟐ SynchronizationComponent
▿ 'Camera' : Entity
⟐ Transform
⟐ SynchronizationComponent
▿ 'Plane' : Entity, children: 1
⟐ Transform
⟐ SynchronizationComponent
▿ 'Plane_geometry' : ModelEntity
⟐ Transform
⟐ ModelComponent
⟐ SynchronizationComponent
▿ 'Cylinder' : Entity, children: 1
⟐ Transform
⟐ SynchronizationComponent
▿ 'Cylinder_geometry' : ModelEntity
⟐ Transform
⟐ ModelComponent
⟐ SynchronizationComponent
▿ 'Cylinder_001' : Entity, children: 1
⟐ Transform
⟐ SynchronizationComponent
▿ 'Cylinder_001_geometry' : ModelEntity
⟐ Transform
⟐ ModelComponent
⟐ SynchronizationComponent
▿ 'Cylinder_002' : Entity, children: 1
⟐ Transform
⟐ SynchronizationComponent
▿ 'Cylinder_002_geometry' : ModelEntity
⟐ Transform
⟐ ModelComponent
⟐ SynchronizationComponent
I want the cylinders to collide so here my code for that, this is for one cylinder:
let cylinder1 = importModel.children[0].children[3].children[0] as! ModelEntity
cylinder1.generateCollisionShapes(recursive: false)
And the add it to collision events:
DispatchQueue.main.async {
arView.scene.subscribe(to: CollisionEvents.Began.self,
on: cylinder1) { _ in
print("Collision ball detected!")
}.store(in: &self.subscriptions)
}
Collision is not working. What am I missing here?

My code definitely works. This answer is an addition to that post.
let cylinders = try! Entity.load(named: "cylinders.usdz")
guard let cylinder1 = cylinders.findEntity(named: "Cylinder_001_geometry")
as? (Entity & HasPhysics)
else { fatalError() }
cylinder1.position = [0,-0.5, 0]
cylinder1.orientation = .init(angle: -.pi/2, axis: [1, 0, 0])
cylinder1.scale /= 5
cylinder1.physicsBody = .init()
cylinder1.physicsBody?.mode = .static
cylinder1.generateCollisionShapes(recursive: false)
// ...
anchor.addChild(cylinder1)

Related

From anyObject to array of coordinates

New to Swift and json
I want to extract lng,lat from anyObject elements.
Here a part of my code to extract from json file:
for (key,value) in geoFeature! as [String: AnyObject] {print("key=",key, ptvir, value)}
let geoCoords = geoFeature?["coordinates"] as AnyObject
Resulting to :
Printing description of geoCoords:
▿ 1 element
▿ 0 : 11 elements
▿ 0 : 2 elements
- 0 : -73.596408
- 1 : 45.453657
▿ 1 : 2 elements
- 0 : -73.595466
- 1 : 45.451156
▿ 2 : 2 elements
- 0 : -73.59532
- 1 : 45.450786
▿ 3 : 2 elements
- 0 : -73.596114
- 1 : 45.450639
▿ 4 : 2 elements
- 0 : -73.596616
- 1 : 45.450549
▿ 5 : 2 elements
- 0 : -73.596746
- 1 : 45.450911
▿ 6 : 2 elements
- 0 : -73.596867
- 1 : 45.451248
▿ 7 : 2 elements
- 0 : -73.59716
- 1 : 45.452082
▿ 8 : 2 elements
- 0 : -73.597514
- 1 : 45.45307
▿ 9 : 2 elements
- 0 : -73.597638
- 1 : 45.453437
▿ 10 : 2 elements
- 0 : -73.596408
- 1 : 45.453657
From here, I did not find the code to create an array of coordinates
More details:
From a json file like this part:
{
"type": "FeatureCollection",
"name": "MtlAires2016Bref",
"features": [
{ "type": "Feature", "properties": { "ADIDU": "24661006" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -73.596408, 45.453657 ], [ -73.595466, 45.451156 ], [ -73.59532, 45.450786 ], [ -73.596114, 45.450639 ], [ -73.596616, 45.450549 ], [ -73.596746, 45.450911 ], [ -73.596867, 45.451248 ], [ -73.59716, 45.452082 ], [ -73.597514, 45.45307 ], [ -73.597638, 45.453437 ], [ -73.596408, 45.453657 ] ] ] } }
]
}
My code extract properties for each Feature
The problem is to extract all the polygon coordinates with the result of these 2 lines of code:
for (key,value) in geoFeature! as [String: AnyObject] {print("key=",key, ";", value)}
let geoCoords = geoFeature?["coordinates"] as AnyObject
print("geoCoords=", geoCoords as Any)
result of this print:
geoCoords= (
(
(
"-73.596408",
"45.453657"
),
(
"-73.595466",
"45.451156"
),
(
"-73.59532",
"45.450786"
),
(
"-73.596114",
"45.450639"
),
(
"-73.596616",
"45.450549"
),
(
"-73.596746",
"45.450911"
),
(
"-73.596867",
"45.451248"
),
(
"-73.59716",
"45.452082"
),
(
"-73.597514",
"45.45307"
),
(
"-73.597638",
"45.453437"
),
(
"-73.596408",
"45.453657"
)
)
)
I did not find how to iterate in geoCoords and append coordinates to an array.
Next line give me the same listing; not array
for element in geoCoords as! Array {
//print("elem=",element)}
but if I ask for the type of the variable like this
String(describing: type(of: coordsData))
it's = Array
the next line give an empty array
let tblC = coordsData as [CLLocationCoordinate2D]
Please learn to read JSON. It's pretty easy. There are only two collection types, array ([]) and dictionary ({}). So the value for key coordinates is a nested array of Double ([[[Double]]]).
It's impossible to cast a double array to CLLocationCoordinate2D because the type is not related.
And the unspecified JSON type is Any, never AnyObject
if let geoCoords = geoFeature["coordinates"] as? [[[Double]]] {
for outerCoords in geoCoords {
for innerCoords in outerCoords {
print(innerCoords)
}
}
}

Alamofire 5 send array of dictionaries

I tried to send an array of dictionaries but the server receive another object
ejm
let param: [String: Any] = [
"info1": "test",
"info2": [
[
"material_id": 1,
"qty": 10
],
[
"material_id": 2,
"qty": 5
]
]
]
let request = AF.request(url, method: .post, parameters: params)
request.responseJSON { response in
guard response.error == nil else {
print(response.error)
return
}
print(response.value)
}
in my server when I print $_POST["info2"] the result is:
▿ value : 4 elements
▿ 0 : 1 element
▿ 0 : 2 elements
- key : material_id
- value : 1
▿ 1 : 1 element
▿ 0 : 2 elements
- key : qty
- value : 10
▿ 2 : 1 element
▿ 0 : 2 elements
- key : material_id
- value : 2
▿ 3 : 1 element
▿ 0 : 2 elements
- key : qty
- value : 5
im trying to encode the request but alamofire doesnt allow any to encode

SpriteKit sprite flickers on collision

I have this onscreen joystick which moves sprite around by changing it's position and it works well. I've made some walls for the sprite to collide with (using Physics). And it's not going further as expected but if I keep going into a wall sprite seems to flicker (change it's position for a millisecond and going right back)
override func update(deltaTime seconds: TimeInterval) {
cNode.position = CGPoint(x: playerCurrentPosition.x + xChange,
y: playerCurrentPosition.y + yChange)
}
Did some more debugging and it seems for one update sprite's position is incorrect:
pos Y: -19.168764114379883
pos Y: -19.164892196655273
pos Y: -19.16112518310547
pos Y: -29.928884506225586. <---
pos Y: -19.15281105041504
pos Y: -19.148771286010742
Update:
Okay, I've tried moving with move By like so :
print("[", xChange,", ",yChange,"]", cNode.position.y)
cNode.run(SKAction.moveBy(x: xChange, y: yChange, duration: 1/60))
And I got the same flicker on Y position:
xChange, yChange, cNode.position.y
[ -0.08623046134167665 , 0.4925081801724735 ] -19.026506423950195
[ -0.08623046134167665 , 0.4925081801724735 ] -19.026336669921875
[ -0.08623046134167665 , 0.4925081801724735 ] -19.02557373046875
[ -0.08623046134167665 , 0.4925081801724735 ] -29.92888641357422
[ -0.08623046134167665 , 0.4925081801724735 ] -29.928884506225586
[ -0.08623046134167665 , 0.4925081801724735 ] -29.928884506225586
[ -0.08623046134167665 , 0.4925081801724735 ] -29.928884506225586
[ -0.08623046134167665 , 0.4925081801724735 ] -19.02621841430664
[ -0.08623046134167665 , 0.4925081801724735 ] -19.025728225708008
[ -0.08623046134167665 , 0.4925081801724735 ] -19.02647590637207
[ -0.08623046134167665 , 0.4925081801724735 ] -19.02564811706543
[ -0.08623046134167665 , 0.4925081801724735 ] -19.02585792541504
[ -0.08623046134167665 , 0.4925081801724735 ] -19.02660369873047
[ -0.08623046134167665 , 0.4925081801724735 ] -19.025630950927734
[ -0.08623046134167665 , 0.4925081801724735 ] -19.026046752929688
[ -0.08623046134167665 , 0.4925081801724735 ] -19.026309967041016
[ -0.08623046134167665 , 0.4925081801724735 ] -19.025686264038086
Update 2:
I've made a recording of the issue: Here
Update 3:
It seems my outer green rect had some collision masks, I removed those and it got better, but still there's some bouncing, have a look: Here
(red box is pinned to the green one)
The problem is almost certainly the fact that you are using code - triggered by the joystick - to set the sprite's position directly yet the sprite and the wall cannot overlap due to their physics bodies 'collisionBitMask` settings.
So the sprite is up against the wall and you use the joystick to move the sprite towards the wall. You update the sprite's position which places it overlapping the wall node. The SK engine never saw the 2 nodes begin to overlap but suddenly sees that these nodes overlap massively when they shouldn't and attempts to move them via an impulse so that they don't overlap anymore, causing the sprite to 'jump' to a new position where it doesn't overlap and so it appears to flicker.
You could avoid this my using an SKAction to move the sprite in response to the joystick input which I think will let the SK engine trap the collision between the wall and the spite as soon as they start to overlap and you could use contact detection to then stop the movement.

Converting Firebase data into Swift array

This is how my Firebase data looks like:
[ {
"adress" : "Högåsstigen 10 332 33 Gislaved",
"helg" : "18:00-02:00",
"latitude" : 57.2985,
"longitude" : 13.54326,
"namn" : "Restaurang Åsen",
"outlet" : 0,
"phone" : "0371-123456",
"star" : 0,
"tag" : "Restaurang",
"vardag" : "10:00-19:30"
}, {
"adress" : "Högåsstigen 12 332 33 Gislaved",
"helg" : "18:00-02:00",
"latitude" : 57.9985,
"longitude" : 13.94326,
"namn" : "Kalles ställe",
"outlet" : 0,
"phone" : "0371-123456",
"star" : 2,
"tag" : "Restaurang",
"vardag" : "10:00-19:30"
}, {
"adress" : "Högåsstigen 15 332 33 Gislaved",
"helg" : "18:00-02:00",
"latitude" : 55.603384,
"longitude" : 13.020619,
"namn" : "Olles Pub",
"outlet" : 0,
"phone" : "0371-123456",
"star" : 1,
"tag" : "Krog",
"vardag" : "10:00-19:30"
} ]
I want to grab "adress" from the database.
I'm having problem to convert the data I grab from Firebase to append into an array so I can use them later. I get the error "Value of type 'String' has no member 'generator'".
I have no idea how to proceed in my coding, any suggestions?
var adresserArray = [String]()
let ref = Firebase(url: "https://HIDINGMYURL.firebaseio.com/")
ref.observeEventType(.ChildAdded, withBlock: {snapshot in
let adresser = (snapshot.value.objectForKey("adress")) as! String!
for add in adresser{
adresserArray.append(adresser)
}
})
You are trying to iterate over String here
let adresser = (snapshot.value.objectForKey("adress")) as! String!
for add in adresser{
adresserArray.append(adresser)
}
try just replace this code with
let adresser = (snapshot.value.objectForKey("adress")) as! String
adresserArray.append(adresser)

How can create assoc 3D Array of Array?

I tried create Array 3D but i still don't know how to create one ?
var array = [
"10001": [
"last_index": 0, //Int
"conteiner": [
"id_from": 321, // Int
"val2": "string text" // String
]
]
]
This works for me in Swift 2.0:
let array : [String : [String : Any]] = [
"10001": [
"last_index": 0, //Int
"conteiner": [
"id_from": 321 as Any, // Int
"val2": "string text" as Any // String
]
]
]
array["10001"]?["last_index"] // 0