Page 1 of 1

Unable to enable a wheeljoint

Posted: Sat Jan 16, 2016 10:31 pm
by fredells
Hello, I am using RUBE for the level and vehicle design of a simple game. I am using Rubeloader to load teh scenes but have been unable to enableMotor() or setMotorSpeed() for any joints that are loaded. See below:

Code: Select all

RubeSceneLoader loader = new RubeSceneLoader();
RubeScene scene = loader.loadScene(Gdx.files.internal("rube/bicycle.json"));
loader.addScene(Gdx.files.internal("rube/lvl1.json"));
//box2d
world = scene.getWorld();
joints = scene.getJoints();

wheel = (RevoluteJoint) joints.get(0);
System.out.print(wheel.isMotorEnabled());
bodies = scene.getBodies();
body = bodies.get(3);
Then, in my input adapter when I attempt to control the bike:

Code: Select all

@Override
			public boolean keyDown(int keycode) {
				switch(keycode) {
				case Keys.RIGHT:
					wheel.enableMotor(true);
					wheel.setMotorSpeed(-50);
					//System.out.print(wheel.getType());
					//body.applyForceToCenter(30, 0, true);
					break;
				case Keys.LEFT:
					wheel.enableMotor(true);
					wheel.setMotorSpeed(50);
					//body.applyForceToCenter(-30, 0, true);
					//body.applyTorque(250, true);
					break;
				case Keys.ESCAPE:
					((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu());
					break;
				}
				return false;
			}
I cannot seem to apply any sort of control to the joint. The one thing I can seem to do is apply a force to a body. body.applyForceToCenter(-30, 0, true); has been working.

Any help would be greatly appreciated

Fred

Re: Unable to enable a wheeljoint

Posted: Mon Jan 18, 2016 8:24 am
by iforce2d
What is the maxMotorTorque value of the joint set to? It needs to be strong enough to move the bodies.
Eg. in RUBE set the Max motor torque property of the joint, or in your code use the SetMaxMotorTorque function.

Re: Unable to enable a wheeljoint

Posted: Thu Jan 21, 2016 2:06 am
by fredells
The maxMotorTorque is set to 75. If I pre-enable the motor in RUBE the motor is enabled in game. But then I cannot disable it, i.e the same problem. When I access the joints from Rubeloader they are in an arraylist of <joints>. I then tag the individual joint as (Revolute Joint). Is the control maybe lost when they are converted?

Re: Unable to enable a wheeljoint

Posted: Fri Jan 22, 2016 12:51 am
by iforce2d
So it looks like the problem is getting a reference to the correct joint then? Since you are using this method to get a reference to the joint, that means there is only a single joint in the entire scene right?

Code: Select all

joints = scene.getJoints();
wheel = (RevoluteJoint) joints.get(0);
You cannot assume that the joints in the arraylist returned by getJoints will be in any particular order. For a bicycle I would think you have more than one joint in the scene.

The recommended way to find a specific joint (or any other type of item for that matter) is to give it a name and then use getJointsByName as outlined in the main b2dJson documentation:
http://www.iforce2d.net/b2djson/#naming

But... maybe the loader you are using does not have a function to find named items?
hmm.... it is this one right? https://github.com/tescott/RubeLoader/b ... Scene.java
There is a "getNamed" function that looks like it is intended for this purpose - although if I'm reading it correctly, it does not differentiate between item types (eg. body, joint, fixture) so if you have a body called "wheel" and a joint called "wheel" it looks like that getNamed function would return an array containing them both, not very useful methinks. Maybe I should write my own loader for libGDX someday...