Calculating velocity

Official forum for the Chipmunk Physics Library.

Calculating velocity

Postby visionthing on Mon Feb 08, 2010 6:01 am

Hello all,

I have a couple of questions.

1. Do you think this is an adequate way of calculating velocity?

-(BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
LocationWhenTouchMoved = [[Director sharedDirector] convertCoordinate: [touch locationInView: [touch view]]];

TimeWhenTouchMoved = frames;

CGFloat deltaY = fabsf(LocationWhenTouchMoved.y - LocationWhenTouchBegan.y);

if (deltaY >= kMimimunGestureLength) {

float temporary = fabsf(LocationWhenTouchMoved.y - LocationWhenTouchBegan.y):

velocity = (float) temporary / (TimeWhenTouchMoved - TimeWhenTouchBegan);

}


I think that works fine, but there's one problem.

2. How can I calculate velocity so that it isn't dependent on TimeWhenTouchBegan? Because if the player first puts her finger on the screen and waits a while, velocity will be slow even though she moves her finger fast. It should be dependent on the first move instead, but I don't know how to achieve that.

Cheers!
visionthing
 
Posts: 8
Joined: Thu Feb 04, 2010 6:52 am

Re: Calculating velocity

Postby ShiftZ on Mon Feb 08, 2010 8:40 am

Velocity actually is vector.
velocity = (LocationWhenTouchMoved - LocationWhenTouchBegan) / dt

And speed is length of velocity vector.
ShiftZ
 
Posts: 52
Joined: Sat Mar 07, 2009 7:23 am

Re: Calculating velocity

Postby visionthing on Tue Feb 09, 2010 2:06 am

Thanks for your answer, but I don't fully understand what you mean, as you cannot perform (CGPoint) LocationWhenTouchMoved - (CGPoint) LocationWhenTouchBegan, can you?
visionthing
 
Posts: 8
Joined: Thu Feb 04, 2010 6:52 am

Re: Calculating velocity

Postby slembcke on Tue Feb 09, 2010 10:06 am

On the iPhone as of Chipmunk 5.1, cpVect is actually defined to be GCPoint to make it easier to interact with Cocoa Touch APIs. So you could just do cpvsub(a, b). Though no, in C or Objective-C you cannot use the '-' operator on things that are not numbers.
I'm the guy that wrote Chipmunk. Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 1485
Joined: Tue Aug 14, 2007 7:13 pm

Re: Calculating velocity

Postby visionthing on Wed Feb 10, 2010 3:59 am

Thank you, slembcke, for the clearing up!
visionthing
 
Posts: 8
Joined: Thu Feb 04, 2010 6:52 am

Re: Calculating velocity

Postby visionthing on Thu Feb 11, 2010 9:58 am

I would be glad if you knew the answer to my second question. I would really like to count the speed of the ball from the first move, not the first touch. But I don't know how to do this.

Cheers!


Hmm, does it work if I check when ccTouchesMoved method is called for the first time and save the time of the first calling to a variable (TimeWhenTouchBegan)?
visionthing
 
Posts: 8
Joined: Thu Feb 04, 2010 6:52 am

Re: Calculating velocity

Postby horseshoe7 on Fri Feb 19, 2010 12:08 pm

perhaps I haven't understood exactly, but velocity has speed and direction.

the direction component i would actually try to average over the last few frames (because people might have inaccurate fat fingers), but the speed would be:

(position_last_frame - position_current_frame) / frame_time = speed

the above means, it just traveled a distance of delta_y over a time period of (1/frame_rate) of your app.

that is also pseudocode, but those positions have x and y components.
horseshoe7
 
Posts: 17
Joined: Fri Feb 19, 2010 11:58 am


Return to Chipmunk Physics

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron