StepListener...how?

Posts that don't fit into other categories.
Pavelo
Posts: 3
Joined: Sun Oct 08, 2017 3:07 pm

StepListener...how?

Postby Pavelo » Sun Oct 08, 2017 3:31 pm

Good evening, my name's Paolo, i'm italian (so...sorry for my bad bad bad bad english :roll: ) and I started to have fun with this library only few hours ago.

I'm trying to change randomically a LinearVelocity attribute of a SimulationBody during the simulation, reading on dyn4j javadoc I thought to achieve this by using the StepListener interface.

I have some problems on implements it, I can't catch the step event. :oops:

Somebody can show me a basic example of the implementation of this interface? It's the best way to solve my problem?

Thanks!!!

William
Site Admin
Posts: 345
Joined: Sat Feb 06, 2010 10:23 pm

Re: StepListener...how?

Postby William » Sun Oct 08, 2017 10:23 pm

You are probably better off using the StepAdapter class so you don't have to implement all the methods. Here's a simple example:

Code: [Select all] [Expand/Collapse] [Download] (Untitled.java)
  1. /// defined elsewhere
  2. public class MyStepListener extends StepAdapter {
  3.   private final Body body;
  4.   public MyStepListener(Body body) {
  5.     this.body = body;
  6.   }
  7.   public void end(Step step, World world) {
  8.      this.body.setLinearVelocity(...);
  9.   }
  10. }
  11.  
  12. /// at world creation time
  13. World world = //
  14. Body body = //
  15. world.addListener(new MyStepListener(body));

Keep in mind that changing the velocity directly isn't recommended. If you want the body controlled in some way, try using the MotorJoint class instead. See the following for an example:

https://github.com/wnbittle/dyn4j/blob/ ... ntrol.java

William

Pavelo
Posts: 3
Joined: Sun Oct 08, 2017 3:07 pm

Re: StepListener...how?

Postby Pavelo » Mon Oct 09, 2017 2:53 am

Thank you very much!!!! This is exactly what I've been looking for!

Pavelo
Posts: 3
Joined: Sun Oct 08, 2017 3:07 pm

Re: StepListener...how?

Postby Pavelo » Mon Oct 09, 2017 6:07 am

I have another little problem. I don't know if I have to open a new thread with the new question.

I created a world with 4 walls, now I implemented the erratic movement following your previous topic, and with great pleasure I notice how the my object collides with the 4 walls.

Now I have created a Slice sensor body, so to disallow collision events. I want this Slice to be jointed to my previous object, like a bidimensional showcone. I use a WeldJoint, but now the object doesn't move anymore.

So I try to access to the object through the WeldJoint

Code: Select all

@Override
   public void end(Step arg0, World arg1) {
      
      Random r = new Random();
      for (Joint joint : body.getJoints())
      {
         Vector2 v = joint.getBody1().getLinearVelocity();
         
         if(r.nextBoolean())
            v.x = v.x + r.nextFloat() * 5;
         else
            v.x = v.x - r.nextFloat() * 5;
         if(r.nextBoolean())
            v.y = v.y + r.nextFloat() * 5;
         else
            v.y = v.y - r.nextFloat() * 5;
         joint.getBody1().setLinearVelocity(v);
      }
   }


On this way I move again my object with the jointed showcone....but....he passes through the walls.
I try to create other bodies in the world, my object collides with them, them collide and bounce on the walls, but my primary object doesn't collide with walls. This behaviour happens because changing the velocity directly isn't recommended or did I do something wrong?

To follow your advice to use the MotorJoint I need two object and move the second erratically so the first try to follow him?

Thanks and please, forgive me for the hassle.

EDIT: I don't know how ....but now it works


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests