Calculating velocity

Official forum for the Chipmunk2D Physics Library.
Post Reply
visionthing
Posts: 8
Joined: Thu Feb 04, 2010 6:52 am
Contact:

Calculating velocity

Post by visionthing »

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!
ShiftZ
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am
Contact:

Re: Calculating velocity

Post by ShiftZ »

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

And speed is length of velocity vector.
visionthing
Posts: 8
Joined: Thu Feb 04, 2010 6:52 am
Contact:

Re: Calculating velocity

Post by visionthing »

Thanks for your answer, but I don't fully understand what you mean, as you cannot perform (CGPoint) LocationWhenTouchMoved - (CGPoint) LocationWhenTouchBegan, can you?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Calculating velocity

Post by slembcke »

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
visionthing
Posts: 8
Joined: Thu Feb 04, 2010 6:52 am
Contact:

Re: Calculating velocity

Post by visionthing »

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

Re: Calculating velocity

Post by visionthing »

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)?
horseshoe7
Posts: 36
Joined: Fri Feb 19, 2010 11:58 am
Contact:

Re: Calculating velocity

Post by horseshoe7 »

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.
Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests