Create Polygons & DistanceJoints

Progress! Finally, after struggling for a while to find the correct way to do the resizing of Bodies with more than one shape, I had a chance to add new features to the Box2D Editor.

First I made the Toolbar extensible, so you can now register actions to be added there. I simply retrieve them from a folder in the layer like this:

Lookup actionLookup = Lookups.forPath("Box2DSceneActions");

 Collection actions = actionLookup.lookupAll(Action.class);
 for (Action action : actions) {
 toolbar.add(action);
 }

That saves a lot of work, since I can now use the Action Wizard to add new Actions. I usually make the actions ContextSensitive for my WorldEditorScene, so they get the scene instance automagically from the Editor TopComponents Lookup, and I don’t have to care about the wiring. The Wizard also allows me to easily add Icons. The only change required after the wizard has finishes is to change the @ActionRegistration to register the Action in Folder  ”Box2DSceneActions”. Here’s an example:

@ActionID(</pre>
 category = "Build",
id = "de.eppleton.physics.editor.scene.SetCreateShapeToolAction")
@ActionRegistration(
 iconBase = "de/eppleton/physics/editor/scene/kthememgr.png",
displayName = "#CTL_SetCreateShapeToolAction")
@ActionReference(path = "Box2DSceneActions", position = 0)
@Messages("CTL_SetCreateShapeToolAction=Create Shape")
public final class SetCreateShapeToolAction implements ActionListener {

private final WorldEditorScene context;

public SetCreateShapeToolAction(WorldEditorScene context) {
 this.context = context;
 }

@Override
 public void actionPerformed(ActionEvent ev) {
 context.setActiveTool(WorldEditorScene.CREATE_SHAPE_TOOL);
 }
}

This action sets the Active Tool of my scene to “CREATE_SHAPE_TOOL”, which means, that after clicking it I can start to visually create a new polygon. This is one of the new features. Every click creates a new Dot and a connection to the previous Dot, if available. It’s pretty similar to Geertjan’s example here, except that the dots are created automatically.

So I started organizing the Actions of the Scene and the Widgets using the setActiveTool(String) Method of Scene. One additional Tool is the “Add Distance Joint” Tool. It allows you to connect two Box2D Bodies by a joint that ensures the distance between the Bodies stays the same. You can watch it here:

The next steps are to allow the creation of different Types of Joints, and find a different way of serializing the World. Unfortunately the current serialization, based on Google ProtoBuffers, is too limited and doesn’t support some important features I will be using (e.g. ChainShapes).

Update: I also added a Revolute Joint Action (with very basic Rendering):

The project itself is available on Github: https://github.com/eppleton/JBox2D-Support. Enjoy!

Dieser Eintrag wurde in netbeans geschrieben. Link merken.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>