iDevGames Forums
Touches: Device vs Simulator - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Touches: Device vs Simulator (/thread-734.html)



Touches: Device vs Simulator - vectorscope - Sep 22, 2009 01:53 PM

Hey All,

I have been working on game for a few weeks now, exclusively through the simulator which was working beautifully. Cool

I finally got a developer account setup so I could test on an actual device and...eeep...I am finding the way touches are registered completely suck. Mad

I am listening for the start of the touch, moving and end touch to move my character. As I said this works fine in the simulator but on the device it is very choppy and unresponsive.

Here is how I'm handling each event:

Code:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event view:(UIView*)aView
{
    UITouch    *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    
    last.touch = VectorCreate(location.x, location.y);
    last.time = [[NSDate date] timeIntervalSince1970];
}

Moved:

Code:
- (void)touchMoved:(NSSet*)touches withEvent:(UIEvent*)event view:(UIView*)aView
{
        UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    
    TouchInformation end;
    end.touch = VectorCreate(location.x, location.y);
    end.time = [[NSDate date] timeIntervalSince1970];
    
    float distance = Vector2fDistance(end.touch, last.touch);
    float v = distance / (end.time - last.time);
    
    // clamp
    if (v > MOVEMENT_CLAMP)
    {
        v = MOVEMENT_CLAMP;
    }
    else if (v < -MOVEMENT_CLAMP)
    {
        v = -MOVEMENT_CLAMP;
    }
    
    float opposite = (end.touch.y - last.touch.y);
    float adjacent = (end.touch.x - last.touch.x);
    float sinAlpha = opposite / distance;
    float cosAlpha = adjacent / distance;
    
    [player updateVelocity:v sinAlpha:sinAlpha cosAlpha:cosAlpha];
    
    // update last touch
    last.touch = VectorCreate(location.x, location.y);
    last.time = [[NSDate date] timeIntervalSince1970];
}

Then in my player class I do the following:

Code:
- (void) updateVelocity:(float)v sinAlpha:(float)sa cosAlpha:(float)ca
{
    currentV = v;
    
    sinAlpha = sa;
    cosAlpha = ca;

    origin.x = position.x;
    origin.y = position.y;
    
    acceleration.x = cosAlpha * 10;
    acceleration.y = sinAlpha * 10;
    
    movementTimer = 0.0f;
        moving = YES;
}

- (void) update:(float) delta
{
    float displacement = 0.0f;
        
    if (moving)
    {
        displacement = currentV * movementTimer - (ACCELERATION * (movementTimer * movementTimer)) / 2;
            
    if (currentV < (ACCELERATION * movementTimer))
    {
        moving = NO;
        origin.x = origin.x + displacement * cosAlpha;
        origin.y = origin.y + displacement * sinAlpha;
        displacement = 0.0f;
    }
    else
    {
        movementTimer += delta;
    }
    }

    // if the new position is still on the screen...update it        
    if ([self checkValidPosition])
    {
        position.x = origin.x + displacement * cosAlpha;
    position.y = origin.y + displacement * sinAlpha;
    }
}

Any idea why things are so different in the sim vs the device?

Or how I can improve this so movement of the player on the phone is smooth (usable for that matter).

Thank you for your time and attention.


Touches: Device vs Simulator - longjumper - Sep 22, 2009 02:38 PM

I'm not familiar with touchesBegan:withEvent:view: or touchMoved:withEvent:view:. These methods are not documented.

The methods you should be overriding in your UIResponder subclass are touchesBegan:withEvent:, touchesMoved:withEvent: and touchesEnded:withEvent:.

Besides that, you are doing work that has already been done for you. You can send the message timestamp to a UITouch and it will return you the number of seconds since system startup, which is not only less work than creating a NSDate and computing the time interval since the epoch, but your deltas will be more precise.

Also, why are you asking the event for all of its touches and then pulling any object from it? Send anyObject to the touches NSSet sent to you in that method.

Those wouldn't cause much of a speed problem, but they are things you should probably know. I don't know why your touch events are sluggish. I don't even see how your methods are being called, but there could be some undocumented API you are using. The touchesMoved method you have should never be called, as it doesn't match at all (touchMoved should be touchesMoved).


Touches: Device vs Simulator - vectorscope - Sep 22, 2009 03:08 PM

Thanks long jumper...yes actually these are my own handlers...

The touches are first captured in my view class:

Code:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    [game touchesBegan:touches withEvent:event view:self];
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    [game touchesMoved:touches withEvent:event view:self];
}

And as you can see are forwarded onto my game class:

From there depending on the current state of the game they are forwarded again to the appropriate handlers...a couple of which I have posted above.

Code:
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event view:(UIView*)aView
{
    [[controller currentState] updateWithTouchLocationBegan:touches withEvent:event view:aView];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event view:(UIView*)aView
{
    [[controller currentState] updateWithTouchLocationMoved:touches withEvent:event view:aView];    
}

So that is how the original methods I posted are called. Make sense?


Touches: Device vs Simulator - vectorscope - Sep 22, 2009 03:19 PM

oh so I should just do...

last.time = [touch timestamp];

?


Touches: Device vs Simulator - Skorche - Sep 22, 2009 03:25 PM

Not sure what the specific problem is here, but there are a ton of subtle differences/bugs between the actual device and the simulator. From now on, you really are going to want to test early and often on the real device.