Box2D Forums

It is currently Sun May 19, 2013 6:49 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Jun 27, 2012 12:31 pm 
Offline

Joined: Wed Jun 27, 2012 12:19 pm
Posts: 2
I'm developing an air hockey game for iOS based on UIKit and Box2D. I managed to model the world (table limits, puck and handles), and move the handles around their respective play areas, however, I'm still unable to cause the puck (ball) to move around the table when I hit it with the handle (it simply gets displaced a little). I guess this is because the mallet does not have a velocity vector, I'm simply setting its position every time its touchesMoved method gets called.
Code:
    CGFloat Xm = px2m(pos.x);
    CGFloat Ym = px2m(480.0 - pos.y);   
    malletBody->SetTransform(b2Vec2(Xm, Ym), 0);


I'm a complete Box2D newbie so I'll appreciate any pointer you can give me about how I can get the expected result.
Sorry for my broken English!

edit: (px2m is a function that translates pixel coordinates to Box2D coordinate system).


Top
 Profile  
 
PostPosted: Wed Jun 27, 2012 6:00 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
You could SetLinearVelocity of the body so that it will cover the necessary distance to the touch point in one time step. eg. if the body needs to move (2,3) units to get to the desired location and your timestep is 60Hz you could SetLinearVelocity(120,180) for one time step. In the next time step you would of course have to set the velocity back to zero to stop it flying of into the distance.


Top
 Profile  
 
PostPosted: Thu Jun 28, 2012 8:19 am 
Offline

Joined: Wed Jun 27, 2012 12:19 pm
Posts: 2
Thank you so much! Yesterday I wasted two hours trying to understand how this worked, but with your help I fixed it in minutes! :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group