I am trying to make my first game using Spritekit, so I have a sprite that I need to move using my accelerometer. Well, that is not a problem. the movement is really smooth and responsive, the problem is that when I try to rotate my sprite to make it collide with my own movement, I often “shake” it, as if it has parkinson. (: D)
I realized that this happens when the accelerometer data is too close to 0 on one of the x, y axes.
So the question is: is there a fix for my beloved Parkinson ?: D
Here is the code:
-(void) update:(NSTimeInterval)currentTime{
static CGPoint oldVelocity;
if(_lastUpdatedTime) {
_dt = currentTime - _lastUpdatedTime;
} else {
_dt = 0;
}
_lastUpdatedTime = currentTime;
CGFloat updatedAccelX = self.motionManager.accelerometerData.acceleration.y;
CGFloat updatedAccelY = -self.motionManager.accelerometerData.acceleration.x+sinf(M_PI/4.0);
CGFloat angle = vectorAngle(CGPointMake(updatedAccelX, updatedAccelY));
_velocity = cartesianFromPolarCoordinate(MAX_MOVE_PER_SEC, angle);
if(oldVelocity.x != _velocity.x || oldVelocity.y != _velocity.y){
_sprite.physicsBody.velocity = CGVectorMake(0, 0);
[_sprite.physicsBody applyImpulse:CGVectorMake(_velocity.x*_sprite.physicsBody.mass, _velocity.y*_sprite.physicsBody.mass)];
_sprite.zRotation = vectorAngle(_velocity);
oldVelocity = _velocity;
}
}
static inline CGFloat vectorAngle(CGPoint v){
return atan2f(v.y, v.x);
}
_velocity , updatedAccelX updatedAccelY, >= , , , , 0,1 0,2, , 0,1.
, "" sprite.
, .