Hi,
Looked trough many threads, can't find a solution.
I've implemented accelerometer ala "Tilt to Live", from here
Accelerometer code works very nice, no issue here. But there is 2 strange things:
- first, I downloaded example from link above and little image moves much faster in example project with same settings compared to my project (example project in in OpenGL)
- second, in my game when sprite moves I can see a little trail behind image as if it doesn't refresh nicely, sort of flickering effect.
Basically code is this:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
Vec2 accel2D(0,0);
Vec3 ax(1, 0, 0);
Vec3 ay(-.63f, 0,-.92f);
Vec3 az(Vec3::Cross(ay,ax).normalize());
ax = Vec3::Cross(az,ay).normalize();
accel2D.x = -Vec3::Dot(Vec3(acceleration.x, acceleration.y, acceleration.z), ax);
accel2D.y = -Vec3::Dot(Vec3(acceleration.x, acceleration.y, acceleration.z), az);
const float xSensitivity = 2.8f;
const float ySensitivity = 2.8f; // yay magic numbers!
const float tiltAmplifier = 8; // w0ot more magic numbers
// since we are in a landscape orientation.
// now apply it to our player's velocity data.
// we also rotate the 2D vector by 90 degrees by switching the components and negating one
vx += -(accel2D.y) * tiltAmplifier * xSensitivity;
vy += -accel2D.x * tiltAmplifier * ySensitivity;
}
Then in update I do this:
-(void) update:(ccTime)delta
{
x += vx;
y += vy;
// put the breaks on our velocity.
vx *= 0.2f;
vy *= 0.2f;
//bound the player
if (x > 460)
{
x = 460;
}
if( x < 20)
{
x = 20;
}
if(y > 300)
{
y = 300;
}
if( y < 20)
{
y = 20;
}
playerSprite.position = ccp(x, y);
}
Tried with setting animation refresh to 1/30, also accelerometer update interval to 1/30, nothing helps.
If I increase sensitivity via xSensitivity, ySensitivity I can get sprite to move as fast as in example project, but flickering effect is even worse.
Any idea what I'm doing wrong? Why image in my game runs much slower then in example with same settings? Could it be because in cocos2D we have update method?