Using the Accelerometer with a specific body from RUBE Scene

General discussion about the R.U.B.E editor
Post Reply
Phantom Ppepper
Posts: 22
Joined: Tue Sep 10, 2013 3:59 am

Using the Accelerometer with a specific body from RUBE Scene

Post 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!
Last edited by Phantom Ppepper on Sun Nov 10, 2013 12:17 am, edited 1 time in total.
Phantom Ppepper
Posts: 22
Joined: Tue Sep 10, 2013 3:59 am

Re: Using the Accelerometer with a specific body from RUBE S

Post 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?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Using the Accelerometer with a specific body from RUBE S

Post 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.
Phantom Ppepper
Posts: 22
Joined: Tue Sep 10, 2013 3:59 am

Re: Using the Accelerometer with a specific body from RUBE S

Post 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.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Using the Accelerometer with a specific body from RUBE S

Post 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.
Phantom Ppepper
Posts: 22
Joined: Tue Sep 10, 2013 3:59 am

Re: Using the Accelerometer with a specific body from RUBE S

Post 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.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Using the Accelerometer with a specific body from RUBE S

Post 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.
Phantom Ppepper
Posts: 22
Joined: Tue Sep 10, 2013 3:59 am

Re: Using the Accelerometer with a specific body from RUBE S

Post by Phantom Ppepper »

Ahh I see, you are correct. Apply force is MUCH better :) thanks!!
Post Reply