Page 1 of 1
Using the Accelerometer with a specific body from RUBE Scene
Posted: Sat Nov 09, 2013 11:50 pm
by Phantom Ppepper
Hello,
I've been breaking several things from the sample loaders in an effort to try to control a specific body from a RUBE scene. For example, the truck from the *"images" sample loader. Any advice would be greatly appreciated.
*Cocos2D Sample Loader
Thanks!
Re: Using the Accelerometer with a specific body from RUBE S
Posted: Sun Nov 10, 2013 12:10 am
by Phantom Ppepper
Actually, to be more specific to what I am trying to do, how would I control a "ball" from a top down view with the accelerometer?
Re: Using the Accelerometer with a specific body from RUBE S
Posted: Sun Nov 10, 2013 10:22 pm
by iforce2d
Near the end of this video (about 25:00) is an example of finding a joint in the scene and controlling it:
http://www.youtube.com/watch?v=0UsKnXuZ2nQ
To control a ball in a top-down view would be very similar - find the ball body and apply a force to it.
Re: Using the Accelerometer with a specific body from RUBE S
Posted: Tue Nov 12, 2013 8:47 pm
by Phantom Ppepper
After applying the same practices from the video, I still cannot seem to control my body with the accelerometer. To verify, acceleromter is enabled, and I am using the
Code: Select all
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
method in my class. I cannot seem to effect the gravity of the world either. I am also using
Code: Select all
-(void)afterLoadProcessing:(class b2dJson *)json
{
[super afterLoadProcessing:json];
like from the video.
Has anyone succesfully used the accelerometer with one of the sample loaders? (iphone cocos2d sample loader, latest update). Thanks in advance for any advice.
Re: Using the Accelerometer with a specific body from RUBE S
Posted: Wed Nov 13, 2013 1:24 am
by iforce2d
What are the values you are giving to ApplyForce? Are they large enough? The accelerometer gives you values between -1 and 1 so you would have to scale them up or down to suit the mass of whatever you are trying to move.
To move a ball as if it was rolling on a table in a top-down view, I think you would want to use the x and y components.
The b2World class has a SetGravity function that should change the gravity.
I wonder if you have seen this, it looks helpful:
http://www.merowing.info/2013/04/learn- ... v-level-0/
You could use ApplyForce to move the body, or change the gravity, but I don't think it makes sense to do both.
Re: Using the Accelerometer with a specific body from RUBE S
Posted: Wed Nov 13, 2013 2:29 am
by Phantom Ppepper
ahh! I am anxious to get home and try another go at this. My current force values are
Code: Select all
m_player->SetLinearVelocity(b2Vec2(acceleration.x *20,m_player->GetLinearVelocity().y*20));
I had previously tried using "applyforce" and probably still will, now that I have your link! Thanks!
In my other searches, this was the only thing that I hadn't seen:
Code: Select all
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.updateInterval = 1.0f / 30.0f;
accelerometer.delegate = self;
On the website, he is using chipmunk and references "LevelViewController viewDidAppear" method. Would this be the tick method in Box2D?
I'm anxious to see if that does it.
Re: Using the Accelerometer with a specific body from RUBE S
Posted: Wed Nov 13, 2013 5:26 am
by iforce2d
I'm pretty sure SetLinearVelocity is not the way to go with this. Applying forces is a much better representation of gravity changing. Probably something like:
Code: Select all
float gravMod = 10;
b2Vec2 direction(acceleration.x, acceleration.y);
m_player->ApplyForce( gravMod * direction, m_player->GetWorldCenter() );
I think the viewDidAppear is just something that gets called once at the beginning. For cocos2d I suppose it would be analogous to applicationDidFinishLaunching.
Re: Using the Accelerometer with a specific body from RUBE S
Posted: Wed Nov 13, 2013 7:35 pm
by Phantom Ppepper
Ahh I see, you are correct. Apply force is MUCH better

thanks!!