0

How can I rotate a segment connected to another segment but limiting the angle of rotation. So, for example, if I have a static segment and another dynamic segment connected with a Pivot Joint and rotating around with a Simple Motor. The motor does a whole revolution, but I only want the dynamic segment to move from a certain point to another back and forth continuously. My goal is to make a walking animal and move the legs back and forth. How can I achieve that ?

This is what I have now :

seg1= Segment((100, 100), (100, 200), 10, True, True, (0, 255, 0, 0))    # density=10, static, filter, green
seg2= Segment((100, 200), (100, 300), 10, False, True, (255, 0, 0, 0))     # dynamic, filer, red

PivotJoint(seg1.body, seg2.body, (100, 200), (100, 200))
SimpleMotor(seg1.body, seg2.body, 1)
RotaryLimitJoint(seg1.body, seg2.body, -pi, pi)

Here, I would want the seg2 to go from (0, 200) to (200, 200) back and forth. I know changing the rate to negative will rotate the segment backwards but it doesn't seem to change when I do this in the draw() function like this :

    def draw(self):
        self.screen.fill(GRAY)
        space.debug_draw(self.draw_options)
        pygame.display.update()

        self.rate = self.rate*(-1)                       # going from 1 to -1 continuously
        self.motor.rate = self.rate

        text = f'fpg: {self.clock.get_fps():.1f}'
        pygame.display.set_caption(text)
amk
  • 1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Feb 21 '23 at 17:33

1 Answers1

0

It looks like your problem is that to invert the rate every time you draw. But what you want to do is to only invert it when it reaches the 'end'..

You could try to check the angle of the rotating segment and set the rate to negative value when the angle is more than your limit, and the reverse when it's below your limit. (Easiest I guess is to do this in your draw method since thats where you already have some code, but even nicer might be the place where you call space step.)

viblo
  • 4,159
  • 4
  • 20
  • 28