# Box2D Forums

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

 All times are UTC - 8 hours [ DST ]

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: UIKit+Box2D based air hockey gamePosted: Wed Jun 27, 2012 12:31 pm

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

 Post subject: Re: UIKit+Box2D based air hockey gamePosted: Wed Jun 27, 2012 6:00 pm

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

 Post subject: Re: UIKit+Box2D based air hockey gamePosted: Thu Jun 28, 2012 8:19 am

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!

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: