I have seen two ways to describe falling objects behaviour for different device orientation using CoreMotion framework. I have tried both version in code and they both seem to behave as what I expect, i.e. falling toward earth regardless of device orientation.
I'm just curious why both version behave the same, even though for the first version, the acceleration in x and y direction, i.e. dx
and dy
, are switched at landscape orientation?
1)
if var dx = data?.acceleration.x, var dy = data?.acceleration.y {
switch UIDevice.currentDevice().orientation {
case .Portrait:
dy = -dy
case .PortraitUpsideDown:
break
case .LandscapeRight:
swap(&dx, &dy)
case .LandscapeLeft:
swap(&dx, &dy)
dy = -dy
default:
dx = 0
dy = 0
}
gravity.gravityDirection = CGVector(dx: dx, dy: dy)
}
2)
if var dx = data?.acceleration.x, var dy = data?.acceleration.y {
switch UIDevice.currentDevice().orientation {
case .Portrait:
dy = -dy
case .PortraitUpsideDown:
break
case .LandscapeRight:
break
case .LandscapeLeft:
break
default:
dx = 0
dy = 0
}
gravity.gravityDirection = CGVector(dx: dx, dy: dy)
}
As learned in an earlier unit, free fall is a special type of motion in which the only force acting upon an object is gravity. Objects that are said to be undergoing free fall, are not encountering a significant force of air resistance; they are falling under the sole influence of gravity.
Heavier things have a greater gravitational force AND heavier things have a lower acceleration. It turns out that these two effects exactly cancel to make falling objects have the same acceleration regardless of mass.
Earth's gravitational force (force of gravity) pulls all objects towards the centre of the earth.
Acceleration of freely falling body is the acceleration produced when a body falls under the influence of gravitational force of the earth alone. It is denoted by g and its value on the surface of the earth is 9.8 ms−2.
I guess you are using the simulator, so gravity is always orthogonal to the bottom.
The behaviour should differ on a real device imo., as you will always tilt the device at least slightly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With