Touches: Device vs Simulator

Apprentice
Posts: 5
Joined: 2009.09
Post: #1
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.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #2
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).
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2009.09
Post: #3
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?
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2009.09
Post: #4
oh so I should just do...

last.time = [touch timestamp];

?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #5
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  touches offset? Jamie W 11 11,363 Aug 18, 2011 10:23 AM
Last Post: MattDiamond
  How to get number of active touches? Rasterman 8 7,227 Apr 21, 2010 03:56 PM
Last Post: longjumper
  not getting touches for top 20 pixels Rasterman 10 6,295 Apr 8, 2010 01:00 AM
Last Post: fattoh
  Touches are slowing down game Jamie W 8 5,542 Mar 26, 2010 03:41 PM
Last Post: Jamie W
  Touches Lagging &amp; Skipping hippocoder 0 1,933 Mar 8, 2010 09:20 AM
Last Post: hippocoder