I'm working on something that creates floating objects then plays notes when they collide. The velocity is worked out then plays a note using AKOscillatorBank play notes. When multiple notes with different velocities are sent, AKOscillatorBank starts clicking and volumes start jumping all over the place. It's like the velocity for any given note affects the volume for the whole oscillator bank.
Works fine when every note has the same velocity. Tried to use a new AKOsillator for every object instead of AKOSillatorBank but that creates its own set of problems.
oscBank = AKOscillatorBank(waveform: AKTable(.sine), attackDuration: 0.01, decayDuration: 0.4, sustainLevel: 0, releaseDuration: 0.4, pitchBend: 0, vibratoDepth: 0, vibratoRate: 0)
// each object has a playNote function that calculate velocity of object then converts to midi velocity value.
//Works fine when velocity is set to a static number eg: oscBank.play(noteNumber: midiNote, velocity: 100)
func playNote (_ velocity: CGFloat) {
oscBank.play(noteNumber: midiNote, velocity: phyVelToMidiVel(velocity))
_ = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { timer in
self.oscBank.stop(noteNumber: self.midiNote)
timer.invalidate()
}
}
func phyVelToMidiVel (_ phyVel: CGFloat) -> MIDIVelocity {
var midiVel = (127 / resizeConstant) * phyVel
if midiVel > 127 {
midiVel = 127
}
return UInt8(midiVel)
}
Loads of clicking when AKOsillatorBank is being sent multiple notes with different velocities.
Related
I have a spinning wheel rotating at an angular speed ω, no acceleration involved, implemented with SpriteKit.
When the user push a button I need to slowly decelerate the wheel from the current angle ∂0 and end-up in a specified angle (lets call it ∂f).
I created associated to it a mass of 2.
I already tried the angularDamping and the SKAction.rotate(toAngle: duration:) but they do not fit my needs because:
With the angularDamping I cannot specify easy the angle ∂f where I want to end up.
With the SKAction.rotate(toAngle: duration:) I cannot start slowing down from the current rotation speed and it doesn't behave natural.
The only remaining approach I tried is by using the SKAction.applyTorque(duration:).
This sounds interesting but I have problems calculating the formula to obtain the correct torque to apply and especially for the inertia and radius of the wheel.
Here is my approach:
I'm taking the starting angular velocity ω as:
wheelNode.physicsBody?.angularVelocity.
I'm taking the mass from wheelNode.physicsBody?.mass
The time t is a constant of 10 (this means that in 10 seconds I want the wheel decelerating to the final angle ∂f).
The deceleration that I calculated as:
let a = -1 * ω / t
The inertia should be: let I = 1/2 * mass * pow(r, 2)*. (see notes regarding the radius please)
Then, finally, I calculated the final torque to apply as: let t = I * a (taking care that is opposite of the current angular speed of the wheel).
NOTE:
Since I don't have clear how to have the radius of the wheel I tried to grab it both from:
the wheelNode.physicsBody?.area as let r = sqrt(wheelNode.physicsBody?.area ?? 0 / .pi)
by converting from pixel to meters as the area documentation says. Then I have let r = self.wheelNode.radius / 150.
Funny: I obtain 2 different values :(
UNFORTUNATLY something in this approach is not working because so far I have no idea how to end up in the specified angle and the wheel doesn't stop anyway as it should (or the torque is too much and spins in the other direction, or is not enough). So, also the torque applied seems to be wrong.
Do you know a better way to achieve the result I need? Is that the correct approach? If yes, what's wrong with my calculations?
Kinematics makes my head hurt, but here you go. I made it to where you can input the amount of rotations and the wheel will rotate that many times as its slowing down to the angle you specify. The other function and extension are there to keep the code relatively clean/readable. So if you just want one giant mess function go ahead and modify it.
• Make sure the node's angularDampening = 0.0
• Make sure the node has a circular physicsbody
// Stops a spinning SpriteNode at a specified angle within a certain amount of rotations
//NOTE: Node must have a circular physicsbody
// Damping should be from 0.0 to 1.0
func decelerate(node: SKSpriteNode, toAngle: CGFloat, rotations: Int) {
if node.physicsBody == nil { print("Node doesn't have a physicsbody"); return } //Avoid crash incase node's physicsbody is nil
var cw:CGFloat { if node.physicsBody!.angularVelocity < CGFloat(0.0) { return -1.0} else { return 1.0} } //Clockwise - using int to reduce if statments with booleans
let m = node.physicsBody!.mass // Mass
let r = CGFloat.squareRoot(node.physicsBody!.area / CGFloat.pi)() // Radius
let i = 0.5 * m * r.squared // Intertia
let wi = node.physicsBody!.angularVelocity // Initial Angular Velocity
let wf:CGFloat = 0 // Final Angular Velocity
let ti = CGFloat.unitCircle(node.zRotation) // Initial Theta
var tf = CGFloat.unitCircle(toAngle) // Final Theta
//Correction constant based on rate of rotation since there seems to be a delay between when the action is calcuated and when it is run
//Without the correction the node stops a little off from its desired stop angle
tf -= 0.00773889 * wi //Might need to change constn
let dt = deltaTheta(ti, tf, Int(cw), rotations)
let a = -cw * 0.5 * wi.squared / abs(dt) // Angular Acceleration - cw used to determine direction
print("A:\(a)")
let time:Double = Double(abs((wf-wi) / a)) // Time needed to stop
let torque:CGFloat = i * a // Torque needed to stop
node.run(SKAction.applyTorque(torque, duration: time))
}
func deltaTheta(_ ti:CGFloat, _ tf:CGFloat, _ clockwise: Int, _ rotations: Int) -> CGFloat {
let extra = CGFloat(rotations)*2*CGFloat.pi
if clockwise == -1 {
if tf>ti { return tf-ti-2*CGFloat.pi-extra }else{ return tf-ti-extra }
}else{
if tf>ti { return tf-ti+extra }else{ return tf+2*CGFloat.pi+extra-ti }
}
}
}
extension CGFloat {
public var squared:CGFloat { return self * self }
public static func unitCircle(_ value: CGFloat) -> CGFloat {
if value < 0 { return 2 * CGFloat.pi + value }
else{ return value }
}
}
I'm currently working on a game in scenekit with swift, and i've got a spaceship flying around. I'm using the following code to make the camera follow the spaceship:
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
updateCameraPosition()
}
func updateCameraPosition () {
let currentPosition = player.node.presentation.position
let x: Float = lerp(a: Float(prevCameraPosition.x), b: currentPosition.x, t: 0.03)
let z: Float = lerp(a: Float(prevCameraPosition.z), b: currentPosition.z, t: 0.03) + (cameraZoom/2)
let vector = SCNVector3(x: x, y: cameraZoom, z: z)
cameraNode.runAction(SCNAction.move(to: vector, duration: 0.2))
prevCameraPosition = currentPosition
}
func lerp (a: Float, b: Float, t: Float) -> Float {
return (1 - t) * a + t * b;
}
in addition to just following the ship, it add's some nice offset motion when you change directions for a nice fluid camera.
The problem i'm facing is the ship glitches back and forth a good portion of the time, it always moves in the correct directions, but it almost looks like the ship position is getting reset back a few frames. You can see this in action with this video.
Without the camera follow code, the ship moves much smoother, as you can see in this video
Can anybody see anything wrong with my code that is maybe inefficient? Maybe there is a more optimized way to do this? Any tips/resources/advice is greatly appriciated!
You need to stop the previous SCNAction before applying new SCNAction.
To add an action with a key you use the
cameraNode.runAction(SCNAction, forKey: String)
method.
Now you can remove a specific action by that key
cameraNode.removeAction(forKey: String)
I tried to move sprites move randomly on screen but the sprite move once time to random position and stop to move
Here I call to make shapes func with timer
//Make shape Timer
func makeshapetimer () {
maketimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(makerandomShape), userInfo: nil, repeats: true)
}
//Make random shape
func makerandomShape () {
//Check if have more than 12 shapes
if shapesamount <= 12 {
let sprite = shape.copy() as! SKShapeNode
sprite.name = "Shpae \(shapesamount)"
sprite.position = CGPoint(x: frame.minX - sprite.frame.width, y: frame.maxY)
shapes.addChild(sprite)
shapesamount += 1
moveRandom(node: sprite)
}
}
And here I make a random position action and repeat the action forever but is run only once time per shape
//Move shape radmonly
func moveRandom (node: SKShapeNode) {
move = SKAction.move(to: CGPoint(x: CGFloat.random(min: frame.minX, max: frame.maxX), y: CGFloat.random(min: frame.minY, max: frame.maxY)), duration: shapespeed)
node.run(SKAction.repeatForever(move))
}
Your moveRandom function is called only once per sprite.
Here's what you tell it to do:
get a random x,y position --- let's say it got 120,200
move to 120,200 --- and repeat moving to 120,200 forever
So, the sprite dutifully moves to that random position, and keeps moving to that position. It never goes back to its starting point, and it never gets a new position to move to.
If you want the sprites to keep moving to new random positions, you need to create a new random position each time the current move finishes.
I'm using Swift 3.0 and Xcode 8.2.1, testing on an iPhone 6s running iOS 10.2.
I'm simply attempting to rotate an SKCameraNode at a rate that is slowing down (I.E. it has both an angular velocity and acceleration).
This is my current solution, and yes I know it's weird:
cam.run(
SKAction.repeatForever(
SKAction.sequence([
SKAction.run
{
self.cam.run(SKAction.rotate(toAngle: 0.0, duration: 0.7, shortestUnitArc: true))
},
SKAction.wait(forDuration: 0.1)])))
The camera's zRotation starts at some non-zero place, and then this is called. It then calls the SKAction.rotate part every 0.1 seconds. It's not shown here, but I have it terminate after 3 seconds.
This provides the exact effect that I want, because every time the SKAction is called, the zRotation is closer to its 0.0, but the time it needs to take to get there stays at 0.7, so the rate at which it approaches 0.0 slows down.
HOWEVER, 1 of 3 things happen from this:
It works perfectly as intended
It feels like it stops and continues every 0.1 seconds
The camera just immediately and completely stops functioning as a camera as soon as the SKAction is called, and the player is just stuck looking at the same spot till the cameras are switched.
I tried reducing the waitForDuration from 0.1 to 0.01 but then it always does option 3.
Is there some sort of "rules of execution" that I'm not following when it comes to using cameras?
Thanks!
You can use a single rotate(toAngle:) action that decelerates when the camera's zRotation is close to the ending angle and setting the timingMode property to .easeOut. For example,
cam.zRotation = CGFloat.pi
let action = SKAction.rotate(toAngle: 0, duration: duration, shortestUnitArc: true)
// Slows when zRotation is near 0
action.timingMode = .easeOut
cam.run(action)
Alternatively, you can define your own timing function to customize the ease-out behavior by replacing timingMode statement with
action.timingFunction = {
time in
// Custom ease-out, modify this as needed
return 1-pow(1-time, 5)
}
Moreover, you can use the following to calculate the action's duration so the angular velocity and acceleration are consistent regardless of the starting angle (i.e., the amount of rotation)
let duration = TimeInterval(normalizedArcFromZero(angle:cam.zRotation)) * 3
func normalizedArcFromZero(angle:CGFloat) -> CGFloat {
let diff = angle.mod(dividingBy:CGFloat.pi*2)
let arc = CGFloat.pi - (diff + CGFloat.pi).mod(dividingBy:2 * CGFloat.pi)
return abs(arc)/CGFloat.pi
}
Lastly, since the above requires a modulo function that performs a "floored" division (instead of Swift's truncatingRemainder), you'll need to add this to your project
extension CGFloat {
func mod(dividingBy x:CGFloat) -> CGFloat {
return self - floor(self/x) * x
}
}
Our scene only has about 20 nodes. The code below lets the user pan, conducting a hit test on each pan -- the goal is to highlight blocks as the user pans around the screen.
However, it is noticeably sluggish on a iPhone 5S. It's not deterministic but happens often enough to be irritating (every 5-10 pans).
We considered using hitTestWithSegment since you could tightly bound the range for testing but believe that should be slower because you must first compute the two points required for the function.
Moreover, the SCNHitTestClipToZRangeKey option for hitTest should provide a comparable performance boost by tightening the hit range without requiring the computation of two additional points.
Any suggestions for speeding up the performance of hitTest?
func sceneViewPannedOneFinger(sender: UIPanGestureRecognizer) {
// Get pan distance & convert to radians
let translation = sender.translationInView(sender.view!)
var xRadians = GLKMathDegreesToRadians(Float(translation.x))
var yRadians = GLKMathDegreesToRadians(Float(translation.y))
// Get x & y radians
xRadians = (xRadians / 4) + curXRadians
yRadians = (yRadians / 4) + curYRadians
// Limit yRadians to prevent rotating 360 degrees vertically
yRadians = max(Float(-M_PI_2), min(Float(M_PI_2), yRadians))
// Set rotation values to avoid Gimbal Lock
cameraNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: yRadians)
userNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: xRadians)
// Save value for next rotation
if sender.state == UIGestureRecognizerState.Ended {
curXRadians = xRadians
curYRadians = yRadians
}
// Set preview block
setPreviewBlock(sender)
}
private func setPreviewBlock(recognizer: UIGestureRecognizer) {
let point = recognizer.locationInView(sceneView)
let options = [SCNHitTestRootNodeKey: sceneView.scene!.rootNode, SCNHitTestClipToZRangeKey: 15, SCNHitTestSortResultsKey: true]
let hits = sceneView.hitTest(point, options: options)
print(hits.first?.worldCoordinates)
}