How to rotate pymunk joints at will? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Pymunk servo jointtop down friction in pymunkAdding points to pymunk polyPymunk body's shapes don't rotate when I rotate the bodyWhy is pymunk terminating?Dampening a collision in pymunkCompile pymunk on mac OS Xpymunk - How to restrict movement?Pymunk servo jointRotating group of bodies in pymunkUnits of velocity in Pymunk

France's Public Holidays' Puzzle

Is a self contained air-bullet cartridge feasible?

What's called a person who work as someone who puts products on shelves in stores?

What was Apollo 13's "Little Jolt" after MECO?

Is there a verb for listening stealthily?

Is there an efficient way for synchronising audio events real-time with LEDs using an MCU?

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

How did Elite on the NES work?

Why is arima in R one time step off?

Suing a Police Officer Instead of the Police Department

SQL Server placement of master database files vs resource database files

Test if all elements of a Foldable are the same

Like totally amazing interchangeable sister outfit accessory swapping or whatever

Is it accepted to use working hours to read general interest books?

Why do people think Winterfell crypts is the safest place for women, children & old people?

Is there a possibility to generate a list dynamically in Latex?

Why doesn't the university give past final exams' answers?

Are these square matrices always diagonalisable?

Coin Game with infinite paradox

How to compute a Jacobian using polar coordinates?

Is it appropriate to mention a relatable company blog post when you're asked about the company?

What is ls Largest Number Formed by only moving two sticks in 508?

What were wait-states, and why was it only an issue for PCs?

Is Bran literally the world's memory?



How to rotate pymunk joints at will?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Pymunk servo jointtop down friction in pymunkAdding points to pymunk polyPymunk body's shapes don't rotate when I rotate the bodyWhy is pymunk terminating?Dampening a collision in pymunkCompile pymunk on mac OS Xpymunk - How to restrict movement?Pymunk servo jointRotating group of bodies in pymunkUnits of velocity in Pymunk



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm trying to create a walking spider like this:
enter image description here



I considered using a SimpleMotor at the pink and red joints and control them using the rate function. But when I tried, I get an error that the function is not callable.




self.motorJoint1.rate(0.0) TypeError: 'float' object is not callable



I don't see any other functions in the pymunk API that allow controlling the joints at will. Is there really no function or am I missing something?



Basically in the run loop I want to specify rotations to the joints at certain points of time, to not just make the spider walk, but to eventually be able to use Neural Networks to allow it to experiment with various configurations of leg positions and figure out which ones can make it walk:



angle1 = 30
angle2 = 10
redJoint1.rotate(angle1)
pinkJoint2.rotate(angle2)
if angle1 < 50:
angle1 = angle1 + 1


Is it possible at all to achieve such a level of control over joints using Pymunk? To be able to stop moving the legs (without needing to put the body to sleep), or to rotate the leg joints to whatever angle the spider 'wishes to' at any point in time?
Sample code would be a great help.










share|improve this question






























    0















    I'm trying to create a walking spider like this:
    enter image description here



    I considered using a SimpleMotor at the pink and red joints and control them using the rate function. But when I tried, I get an error that the function is not callable.




    self.motorJoint1.rate(0.0) TypeError: 'float' object is not callable



    I don't see any other functions in the pymunk API that allow controlling the joints at will. Is there really no function or am I missing something?



    Basically in the run loop I want to specify rotations to the joints at certain points of time, to not just make the spider walk, but to eventually be able to use Neural Networks to allow it to experiment with various configurations of leg positions and figure out which ones can make it walk:



    angle1 = 30
    angle2 = 10
    redJoint1.rotate(angle1)
    pinkJoint2.rotate(angle2)
    if angle1 < 50:
    angle1 = angle1 + 1


    Is it possible at all to achieve such a level of control over joints using Pymunk? To be able to stop moving the legs (without needing to put the body to sleep), or to rotate the leg joints to whatever angle the spider 'wishes to' at any point in time?
    Sample code would be a great help.










    share|improve this question


























      0












      0








      0








      I'm trying to create a walking spider like this:
      enter image description here



      I considered using a SimpleMotor at the pink and red joints and control them using the rate function. But when I tried, I get an error that the function is not callable.




      self.motorJoint1.rate(0.0) TypeError: 'float' object is not callable



      I don't see any other functions in the pymunk API that allow controlling the joints at will. Is there really no function or am I missing something?



      Basically in the run loop I want to specify rotations to the joints at certain points of time, to not just make the spider walk, but to eventually be able to use Neural Networks to allow it to experiment with various configurations of leg positions and figure out which ones can make it walk:



      angle1 = 30
      angle2 = 10
      redJoint1.rotate(angle1)
      pinkJoint2.rotate(angle2)
      if angle1 < 50:
      angle1 = angle1 + 1


      Is it possible at all to achieve such a level of control over joints using Pymunk? To be able to stop moving the legs (without needing to put the body to sleep), or to rotate the leg joints to whatever angle the spider 'wishes to' at any point in time?
      Sample code would be a great help.










      share|improve this question
















      I'm trying to create a walking spider like this:
      enter image description here



      I considered using a SimpleMotor at the pink and red joints and control them using the rate function. But when I tried, I get an error that the function is not callable.




      self.motorJoint1.rate(0.0) TypeError: 'float' object is not callable



      I don't see any other functions in the pymunk API that allow controlling the joints at will. Is there really no function or am I missing something?



      Basically in the run loop I want to specify rotations to the joints at certain points of time, to not just make the spider walk, but to eventually be able to use Neural Networks to allow it to experiment with various configurations of leg positions and figure out which ones can make it walk:



      angle1 = 30
      angle2 = 10
      redJoint1.rotate(angle1)
      pinkJoint2.rotate(angle2)
      if angle1 < 50:
      angle1 = angle1 + 1


      Is it possible at all to achieve such a level of control over joints using Pymunk? To be able to stop moving the legs (without needing to put the body to sleep), or to rotate the leg joints to whatever angle the spider 'wishes to' at any point in time?
      Sample code would be a great help.







      chipmunk pymunk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 15:10







      Nav

















      asked Mar 22 at 14:58









      NavNav

      9,5342264111




      9,5342264111






















          1 Answer
          1






          active

          oldest

          votes


















          0














          From the servo example I took a hint and implemented this basic leg:



          enter image description here



          import sys

          import pygame
          from pygame.locals import USEREVENT, QUIT, KEYDOWN, KEYUP, K_s, K_r, K_q, K_ESCAPE, K_UP, K_DOWN, K_RIGHT, K_LEFT
          from pygame.color import THECOLORS

          import pymunk
          from pymunk import Vec2d
          import pymunk.pygame_util

          class Simulator(object):

          def __init__(self):
          self.display_flags = 0
          self.display_size = (600, 600)

          self.space = pymunk.Space()
          self.space.gravity = (0.0, -1900.0)
          #self.space.damping = 0.999 # to prevent it from blowing up.

          # Pymunk physics coordinates start from the lower right-hand corner of the screen.
          self.ground_y = 100
          ground = pymunk.Segment(self.space.static_body, (5, self.ground_y), (595, self.ground_y), 1.0)
          ground.friction = 1.0
          self.space.add(ground)

          self.screen = None

          self.draw_options = None

          def reset_bodies(self):
          for body in self.space.bodies:
          if not hasattr(body, 'start_position'):
          continue
          body.position = Vec2d(body.start_position)
          body.force = 0, 0
          body.torque = 0
          body.velocity = 0, 0
          body.angular_velocity = 0
          body.angle = body.start_angle

          def draw(self):
          self.screen.fill(THECOLORS["white"])### Clear the screen
          self.space.debug_draw(self.draw_options)### Draw space
          pygame.display.flip()### All done, lets flip the display

          def main(self):
          pygame.init()
          self.screen = pygame.display.set_mode(self.display_size, self.display_flags)
          width, height = self.screen.get_size()
          self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)

          def to_pygame(p):
          return int(p.x), int(-p.y+height) #Small hack to convert pymunk to pygame coordinates
          def from_pygame(p):
          return to_pygame(p)

          clock = pygame.time.Clock()
          running = True
          font = pygame.font.Font(None, 16)

          # Create the spider
          chassisXY = Vec2d(self.display_size[0]/2, self.ground_y+100)
          chWd = 70; chHt = 50
          chassisMass = 10

          legWd_a = 50; legHt_a = 5
          legWd_b = 100; legHt_b = 5
          legMass = 1
          relativeAnguVel = 0

          #---chassis
          chassis_b = pymunk.Body(chassisMass, pymunk.moment_for_box(chassisMass, (chWd, chHt)))
          chassis_b.position = chassisXY
          chassis_shape = pymunk.Poly.create_box(chassis_b, (chWd, chHt))
          chassis_shape.color = 200, 200, 200
          print("chassis position");print(chassis_b.position)

          #---first left leg a
          leftLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
          leftLeg_1a_body.position = chassisXY - ((chWd/2)+(legWd_a/2), 0)
          leftLeg_1a_shape = pymunk.Poly.create_box(leftLeg_1a_body, (legWd_a, legHt_a))
          leftLeg_1a_shape.color = 255, 0, 0

          #---first left leg b
          leftLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
          leftLeg_1b_body.position = leftLeg_1a_body.position - ((legWd_a/2)+(legWd_b/2), 0)
          leftLeg_1b_shape = pymunk.Poly.create_box(leftLeg_1b_body, (legWd_b, legHt_b))
          leftLeg_1b_shape.color = 0, 255, 0

          #---first right leg a
          rightLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
          rightLeg_1a_body.position = chassisXY + ((chWd/2)+(legWd_a/2), 0)
          rightLeg_1a_shape = pymunk.Poly.create_box(rightLeg_1a_body, (legWd_a, legHt_a))
          rightLeg_1a_shape.color = 255, 0, 0

          #---first right leg b
          rightLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
          rightLeg_1b_body.position = rightLeg_1a_body.position + ((legWd_a/2)+(legWd_b/2), 0)
          rightLeg_1b_shape = pymunk.Poly.create_box(rightLeg_1b_body, (legWd_b, legHt_b))
          rightLeg_1b_shape.color = 0, 255, 0

          #---link left leg b with left leg a
          pj_ba1left = pymunk.PinJoint(leftLeg_1b_body, leftLeg_1a_body, (legWd_b/2,0), (-legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
          motor_ba1Left = pymunk.SimpleMotor(leftLeg_1b_body, leftLeg_1a_body, relativeAnguVel)
          #---link left leg a with chassis
          pj_ac1left = pymunk.PinJoint(leftLeg_1a_body, chassis_b, (legWd_a/2,0), (-chWd/2, 0))
          motor_ac1Left = pymunk.SimpleMotor(leftLeg_1a_body, chassis_b, relativeAnguVel)
          #---link right leg b with right leg a
          pj_ba1Right = pymunk.PinJoint(rightLeg_1b_body, rightLeg_1a_body, (-legWd_b/2,0), (legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
          motor_ba1Right = pymunk.SimpleMotor(rightLeg_1b_body, rightLeg_1a_body, relativeAnguVel)
          #---link right leg a with chassis
          pj_ac1Right = pymunk.PinJoint(rightLeg_1a_body, chassis_b, (-legWd_a/2,0), (chWd/2, 0))
          motor_ac1Right = pymunk.SimpleMotor(rightLeg_1a_body, chassis_b, relativeAnguVel)

          self.space.add(chassis_b, chassis_shape)
          self.space.add(leftLeg_1a_body, leftLeg_1a_shape, rightLeg_1a_body, rightLeg_1a_shape)
          self.space.add(leftLeg_1b_body, leftLeg_1b_shape, rightLeg_1b_body, rightLeg_1b_shape)
          self.space.add(pj_ba1left, motor_ba1Left, pj_ac1left, motor_ac1Left)
          self.space.add(pj_ba1Right, motor_ba1Right, pj_ac1Right, motor_ac1Right)

          #---prevent collisions with ShapeFilter
          shape_filter = pymunk.ShapeFilter(group=1)
          chassis_shape.filter = shape_filter
          leftLeg_1a_shape.filter = shape_filter
          rightLeg_1a_shape.filter = shape_filter
          leftLeg_1b_shape.filter = shape_filter
          rightLeg_1b_shape.filter = shape_filter


          simulate = False
          rotationRate = 2
          while running:
          for event in pygame.event.get():
          if event.type == QUIT or (event.type == KEYDOWN and event.key in (K_q, K_ESCAPE)):
          #running = False
          sys.exit(0)
          elif event.type == KEYDOWN and event.key == K_s:
          # Start/stop simulation.
          simulate = not simulate
          elif event.type == KEYDOWN and event.key == K_r:
          # Reset.
          # simulate = False
          self.reset_bodies()
          elif event.type == KEYDOWN and event.key == K_UP:
          motor_ba1Left.rate = rotationRate
          elif event.type == KEYDOWN and event.key == K_DOWN:
          motor_ba1Left.rate = -rotationRate
          elif event.type == KEYDOWN and event.key == K_LEFT:
          motor_ac1Left.rate = rotationRate
          elif event.type == KEYDOWN and event.key == K_RIGHT:
          motor_ac1Left.rate = -rotationRate
          elif event.type == KEYUP:
          motor_ba1Left.rate = 0
          motor_ac1Left.rate = 0

          self.draw()

          ### Update physics
          fps = 50
          iterations = 25
          dt = 1.0/float(fps)/float(iterations)
          if simulate:
          for x in range(iterations): # 10 iterations to get a more stable simulation
          self.space.step(dt)

          pygame.display.flip()
          clock.tick(fps)

          if __name__ == '__main__':
          sim = Simulator()
          sim.main()


          It can be controlled with the up, left, right and down arrow keys after first pressing the s key to start the simulation. I've also made sure the variables are created properly linked with each other and named well.



          The part about making the joints move to a desired angle is yet to be implemented, but perhaps that could be calculated by taking the x,y positions of the ends of the joints and using a formula to calculate the angle and then move the motor until it reaches a desired angle.



          If there's a better way, do let me know by posting an answer or editing this one.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302406%2fhow-to-rotate-pymunk-joints-at-will%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            From the servo example I took a hint and implemented this basic leg:



            enter image description here



            import sys

            import pygame
            from pygame.locals import USEREVENT, QUIT, KEYDOWN, KEYUP, K_s, K_r, K_q, K_ESCAPE, K_UP, K_DOWN, K_RIGHT, K_LEFT
            from pygame.color import THECOLORS

            import pymunk
            from pymunk import Vec2d
            import pymunk.pygame_util

            class Simulator(object):

            def __init__(self):
            self.display_flags = 0
            self.display_size = (600, 600)

            self.space = pymunk.Space()
            self.space.gravity = (0.0, -1900.0)
            #self.space.damping = 0.999 # to prevent it from blowing up.

            # Pymunk physics coordinates start from the lower right-hand corner of the screen.
            self.ground_y = 100
            ground = pymunk.Segment(self.space.static_body, (5, self.ground_y), (595, self.ground_y), 1.0)
            ground.friction = 1.0
            self.space.add(ground)

            self.screen = None

            self.draw_options = None

            def reset_bodies(self):
            for body in self.space.bodies:
            if not hasattr(body, 'start_position'):
            continue
            body.position = Vec2d(body.start_position)
            body.force = 0, 0
            body.torque = 0
            body.velocity = 0, 0
            body.angular_velocity = 0
            body.angle = body.start_angle

            def draw(self):
            self.screen.fill(THECOLORS["white"])### Clear the screen
            self.space.debug_draw(self.draw_options)### Draw space
            pygame.display.flip()### All done, lets flip the display

            def main(self):
            pygame.init()
            self.screen = pygame.display.set_mode(self.display_size, self.display_flags)
            width, height = self.screen.get_size()
            self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)

            def to_pygame(p):
            return int(p.x), int(-p.y+height) #Small hack to convert pymunk to pygame coordinates
            def from_pygame(p):
            return to_pygame(p)

            clock = pygame.time.Clock()
            running = True
            font = pygame.font.Font(None, 16)

            # Create the spider
            chassisXY = Vec2d(self.display_size[0]/2, self.ground_y+100)
            chWd = 70; chHt = 50
            chassisMass = 10

            legWd_a = 50; legHt_a = 5
            legWd_b = 100; legHt_b = 5
            legMass = 1
            relativeAnguVel = 0

            #---chassis
            chassis_b = pymunk.Body(chassisMass, pymunk.moment_for_box(chassisMass, (chWd, chHt)))
            chassis_b.position = chassisXY
            chassis_shape = pymunk.Poly.create_box(chassis_b, (chWd, chHt))
            chassis_shape.color = 200, 200, 200
            print("chassis position");print(chassis_b.position)

            #---first left leg a
            leftLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
            leftLeg_1a_body.position = chassisXY - ((chWd/2)+(legWd_a/2), 0)
            leftLeg_1a_shape = pymunk.Poly.create_box(leftLeg_1a_body, (legWd_a, legHt_a))
            leftLeg_1a_shape.color = 255, 0, 0

            #---first left leg b
            leftLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
            leftLeg_1b_body.position = leftLeg_1a_body.position - ((legWd_a/2)+(legWd_b/2), 0)
            leftLeg_1b_shape = pymunk.Poly.create_box(leftLeg_1b_body, (legWd_b, legHt_b))
            leftLeg_1b_shape.color = 0, 255, 0

            #---first right leg a
            rightLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
            rightLeg_1a_body.position = chassisXY + ((chWd/2)+(legWd_a/2), 0)
            rightLeg_1a_shape = pymunk.Poly.create_box(rightLeg_1a_body, (legWd_a, legHt_a))
            rightLeg_1a_shape.color = 255, 0, 0

            #---first right leg b
            rightLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
            rightLeg_1b_body.position = rightLeg_1a_body.position + ((legWd_a/2)+(legWd_b/2), 0)
            rightLeg_1b_shape = pymunk.Poly.create_box(rightLeg_1b_body, (legWd_b, legHt_b))
            rightLeg_1b_shape.color = 0, 255, 0

            #---link left leg b with left leg a
            pj_ba1left = pymunk.PinJoint(leftLeg_1b_body, leftLeg_1a_body, (legWd_b/2,0), (-legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
            motor_ba1Left = pymunk.SimpleMotor(leftLeg_1b_body, leftLeg_1a_body, relativeAnguVel)
            #---link left leg a with chassis
            pj_ac1left = pymunk.PinJoint(leftLeg_1a_body, chassis_b, (legWd_a/2,0), (-chWd/2, 0))
            motor_ac1Left = pymunk.SimpleMotor(leftLeg_1a_body, chassis_b, relativeAnguVel)
            #---link right leg b with right leg a
            pj_ba1Right = pymunk.PinJoint(rightLeg_1b_body, rightLeg_1a_body, (-legWd_b/2,0), (legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
            motor_ba1Right = pymunk.SimpleMotor(rightLeg_1b_body, rightLeg_1a_body, relativeAnguVel)
            #---link right leg a with chassis
            pj_ac1Right = pymunk.PinJoint(rightLeg_1a_body, chassis_b, (-legWd_a/2,0), (chWd/2, 0))
            motor_ac1Right = pymunk.SimpleMotor(rightLeg_1a_body, chassis_b, relativeAnguVel)

            self.space.add(chassis_b, chassis_shape)
            self.space.add(leftLeg_1a_body, leftLeg_1a_shape, rightLeg_1a_body, rightLeg_1a_shape)
            self.space.add(leftLeg_1b_body, leftLeg_1b_shape, rightLeg_1b_body, rightLeg_1b_shape)
            self.space.add(pj_ba1left, motor_ba1Left, pj_ac1left, motor_ac1Left)
            self.space.add(pj_ba1Right, motor_ba1Right, pj_ac1Right, motor_ac1Right)

            #---prevent collisions with ShapeFilter
            shape_filter = pymunk.ShapeFilter(group=1)
            chassis_shape.filter = shape_filter
            leftLeg_1a_shape.filter = shape_filter
            rightLeg_1a_shape.filter = shape_filter
            leftLeg_1b_shape.filter = shape_filter
            rightLeg_1b_shape.filter = shape_filter


            simulate = False
            rotationRate = 2
            while running:
            for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key in (K_q, K_ESCAPE)):
            #running = False
            sys.exit(0)
            elif event.type == KEYDOWN and event.key == K_s:
            # Start/stop simulation.
            simulate = not simulate
            elif event.type == KEYDOWN and event.key == K_r:
            # Reset.
            # simulate = False
            self.reset_bodies()
            elif event.type == KEYDOWN and event.key == K_UP:
            motor_ba1Left.rate = rotationRate
            elif event.type == KEYDOWN and event.key == K_DOWN:
            motor_ba1Left.rate = -rotationRate
            elif event.type == KEYDOWN and event.key == K_LEFT:
            motor_ac1Left.rate = rotationRate
            elif event.type == KEYDOWN and event.key == K_RIGHT:
            motor_ac1Left.rate = -rotationRate
            elif event.type == KEYUP:
            motor_ba1Left.rate = 0
            motor_ac1Left.rate = 0

            self.draw()

            ### Update physics
            fps = 50
            iterations = 25
            dt = 1.0/float(fps)/float(iterations)
            if simulate:
            for x in range(iterations): # 10 iterations to get a more stable simulation
            self.space.step(dt)

            pygame.display.flip()
            clock.tick(fps)

            if __name__ == '__main__':
            sim = Simulator()
            sim.main()


            It can be controlled with the up, left, right and down arrow keys after first pressing the s key to start the simulation. I've also made sure the variables are created properly linked with each other and named well.



            The part about making the joints move to a desired angle is yet to be implemented, but perhaps that could be calculated by taking the x,y positions of the ends of the joints and using a formula to calculate the angle and then move the motor until it reaches a desired angle.



            If there's a better way, do let me know by posting an answer or editing this one.






            share|improve this answer





























              0














              From the servo example I took a hint and implemented this basic leg:



              enter image description here



              import sys

              import pygame
              from pygame.locals import USEREVENT, QUIT, KEYDOWN, KEYUP, K_s, K_r, K_q, K_ESCAPE, K_UP, K_DOWN, K_RIGHT, K_LEFT
              from pygame.color import THECOLORS

              import pymunk
              from pymunk import Vec2d
              import pymunk.pygame_util

              class Simulator(object):

              def __init__(self):
              self.display_flags = 0
              self.display_size = (600, 600)

              self.space = pymunk.Space()
              self.space.gravity = (0.0, -1900.0)
              #self.space.damping = 0.999 # to prevent it from blowing up.

              # Pymunk physics coordinates start from the lower right-hand corner of the screen.
              self.ground_y = 100
              ground = pymunk.Segment(self.space.static_body, (5, self.ground_y), (595, self.ground_y), 1.0)
              ground.friction = 1.0
              self.space.add(ground)

              self.screen = None

              self.draw_options = None

              def reset_bodies(self):
              for body in self.space.bodies:
              if not hasattr(body, 'start_position'):
              continue
              body.position = Vec2d(body.start_position)
              body.force = 0, 0
              body.torque = 0
              body.velocity = 0, 0
              body.angular_velocity = 0
              body.angle = body.start_angle

              def draw(self):
              self.screen.fill(THECOLORS["white"])### Clear the screen
              self.space.debug_draw(self.draw_options)### Draw space
              pygame.display.flip()### All done, lets flip the display

              def main(self):
              pygame.init()
              self.screen = pygame.display.set_mode(self.display_size, self.display_flags)
              width, height = self.screen.get_size()
              self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)

              def to_pygame(p):
              return int(p.x), int(-p.y+height) #Small hack to convert pymunk to pygame coordinates
              def from_pygame(p):
              return to_pygame(p)

              clock = pygame.time.Clock()
              running = True
              font = pygame.font.Font(None, 16)

              # Create the spider
              chassisXY = Vec2d(self.display_size[0]/2, self.ground_y+100)
              chWd = 70; chHt = 50
              chassisMass = 10

              legWd_a = 50; legHt_a = 5
              legWd_b = 100; legHt_b = 5
              legMass = 1
              relativeAnguVel = 0

              #---chassis
              chassis_b = pymunk.Body(chassisMass, pymunk.moment_for_box(chassisMass, (chWd, chHt)))
              chassis_b.position = chassisXY
              chassis_shape = pymunk.Poly.create_box(chassis_b, (chWd, chHt))
              chassis_shape.color = 200, 200, 200
              print("chassis position");print(chassis_b.position)

              #---first left leg a
              leftLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
              leftLeg_1a_body.position = chassisXY - ((chWd/2)+(legWd_a/2), 0)
              leftLeg_1a_shape = pymunk.Poly.create_box(leftLeg_1a_body, (legWd_a, legHt_a))
              leftLeg_1a_shape.color = 255, 0, 0

              #---first left leg b
              leftLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
              leftLeg_1b_body.position = leftLeg_1a_body.position - ((legWd_a/2)+(legWd_b/2), 0)
              leftLeg_1b_shape = pymunk.Poly.create_box(leftLeg_1b_body, (legWd_b, legHt_b))
              leftLeg_1b_shape.color = 0, 255, 0

              #---first right leg a
              rightLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
              rightLeg_1a_body.position = chassisXY + ((chWd/2)+(legWd_a/2), 0)
              rightLeg_1a_shape = pymunk.Poly.create_box(rightLeg_1a_body, (legWd_a, legHt_a))
              rightLeg_1a_shape.color = 255, 0, 0

              #---first right leg b
              rightLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
              rightLeg_1b_body.position = rightLeg_1a_body.position + ((legWd_a/2)+(legWd_b/2), 0)
              rightLeg_1b_shape = pymunk.Poly.create_box(rightLeg_1b_body, (legWd_b, legHt_b))
              rightLeg_1b_shape.color = 0, 255, 0

              #---link left leg b with left leg a
              pj_ba1left = pymunk.PinJoint(leftLeg_1b_body, leftLeg_1a_body, (legWd_b/2,0), (-legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
              motor_ba1Left = pymunk.SimpleMotor(leftLeg_1b_body, leftLeg_1a_body, relativeAnguVel)
              #---link left leg a with chassis
              pj_ac1left = pymunk.PinJoint(leftLeg_1a_body, chassis_b, (legWd_a/2,0), (-chWd/2, 0))
              motor_ac1Left = pymunk.SimpleMotor(leftLeg_1a_body, chassis_b, relativeAnguVel)
              #---link right leg b with right leg a
              pj_ba1Right = pymunk.PinJoint(rightLeg_1b_body, rightLeg_1a_body, (-legWd_b/2,0), (legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
              motor_ba1Right = pymunk.SimpleMotor(rightLeg_1b_body, rightLeg_1a_body, relativeAnguVel)
              #---link right leg a with chassis
              pj_ac1Right = pymunk.PinJoint(rightLeg_1a_body, chassis_b, (-legWd_a/2,0), (chWd/2, 0))
              motor_ac1Right = pymunk.SimpleMotor(rightLeg_1a_body, chassis_b, relativeAnguVel)

              self.space.add(chassis_b, chassis_shape)
              self.space.add(leftLeg_1a_body, leftLeg_1a_shape, rightLeg_1a_body, rightLeg_1a_shape)
              self.space.add(leftLeg_1b_body, leftLeg_1b_shape, rightLeg_1b_body, rightLeg_1b_shape)
              self.space.add(pj_ba1left, motor_ba1Left, pj_ac1left, motor_ac1Left)
              self.space.add(pj_ba1Right, motor_ba1Right, pj_ac1Right, motor_ac1Right)

              #---prevent collisions with ShapeFilter
              shape_filter = pymunk.ShapeFilter(group=1)
              chassis_shape.filter = shape_filter
              leftLeg_1a_shape.filter = shape_filter
              rightLeg_1a_shape.filter = shape_filter
              leftLeg_1b_shape.filter = shape_filter
              rightLeg_1b_shape.filter = shape_filter


              simulate = False
              rotationRate = 2
              while running:
              for event in pygame.event.get():
              if event.type == QUIT or (event.type == KEYDOWN and event.key in (K_q, K_ESCAPE)):
              #running = False
              sys.exit(0)
              elif event.type == KEYDOWN and event.key == K_s:
              # Start/stop simulation.
              simulate = not simulate
              elif event.type == KEYDOWN and event.key == K_r:
              # Reset.
              # simulate = False
              self.reset_bodies()
              elif event.type == KEYDOWN and event.key == K_UP:
              motor_ba1Left.rate = rotationRate
              elif event.type == KEYDOWN and event.key == K_DOWN:
              motor_ba1Left.rate = -rotationRate
              elif event.type == KEYDOWN and event.key == K_LEFT:
              motor_ac1Left.rate = rotationRate
              elif event.type == KEYDOWN and event.key == K_RIGHT:
              motor_ac1Left.rate = -rotationRate
              elif event.type == KEYUP:
              motor_ba1Left.rate = 0
              motor_ac1Left.rate = 0

              self.draw()

              ### Update physics
              fps = 50
              iterations = 25
              dt = 1.0/float(fps)/float(iterations)
              if simulate:
              for x in range(iterations): # 10 iterations to get a more stable simulation
              self.space.step(dt)

              pygame.display.flip()
              clock.tick(fps)

              if __name__ == '__main__':
              sim = Simulator()
              sim.main()


              It can be controlled with the up, left, right and down arrow keys after first pressing the s key to start the simulation. I've also made sure the variables are created properly linked with each other and named well.



              The part about making the joints move to a desired angle is yet to be implemented, but perhaps that could be calculated by taking the x,y positions of the ends of the joints and using a formula to calculate the angle and then move the motor until it reaches a desired angle.



              If there's a better way, do let me know by posting an answer or editing this one.






              share|improve this answer



























                0












                0








                0







                From the servo example I took a hint and implemented this basic leg:



                enter image description here



                import sys

                import pygame
                from pygame.locals import USEREVENT, QUIT, KEYDOWN, KEYUP, K_s, K_r, K_q, K_ESCAPE, K_UP, K_DOWN, K_RIGHT, K_LEFT
                from pygame.color import THECOLORS

                import pymunk
                from pymunk import Vec2d
                import pymunk.pygame_util

                class Simulator(object):

                def __init__(self):
                self.display_flags = 0
                self.display_size = (600, 600)

                self.space = pymunk.Space()
                self.space.gravity = (0.0, -1900.0)
                #self.space.damping = 0.999 # to prevent it from blowing up.

                # Pymunk physics coordinates start from the lower right-hand corner of the screen.
                self.ground_y = 100
                ground = pymunk.Segment(self.space.static_body, (5, self.ground_y), (595, self.ground_y), 1.0)
                ground.friction = 1.0
                self.space.add(ground)

                self.screen = None

                self.draw_options = None

                def reset_bodies(self):
                for body in self.space.bodies:
                if not hasattr(body, 'start_position'):
                continue
                body.position = Vec2d(body.start_position)
                body.force = 0, 0
                body.torque = 0
                body.velocity = 0, 0
                body.angular_velocity = 0
                body.angle = body.start_angle

                def draw(self):
                self.screen.fill(THECOLORS["white"])### Clear the screen
                self.space.debug_draw(self.draw_options)### Draw space
                pygame.display.flip()### All done, lets flip the display

                def main(self):
                pygame.init()
                self.screen = pygame.display.set_mode(self.display_size, self.display_flags)
                width, height = self.screen.get_size()
                self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)

                def to_pygame(p):
                return int(p.x), int(-p.y+height) #Small hack to convert pymunk to pygame coordinates
                def from_pygame(p):
                return to_pygame(p)

                clock = pygame.time.Clock()
                running = True
                font = pygame.font.Font(None, 16)

                # Create the spider
                chassisXY = Vec2d(self.display_size[0]/2, self.ground_y+100)
                chWd = 70; chHt = 50
                chassisMass = 10

                legWd_a = 50; legHt_a = 5
                legWd_b = 100; legHt_b = 5
                legMass = 1
                relativeAnguVel = 0

                #---chassis
                chassis_b = pymunk.Body(chassisMass, pymunk.moment_for_box(chassisMass, (chWd, chHt)))
                chassis_b.position = chassisXY
                chassis_shape = pymunk.Poly.create_box(chassis_b, (chWd, chHt))
                chassis_shape.color = 200, 200, 200
                print("chassis position");print(chassis_b.position)

                #---first left leg a
                leftLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
                leftLeg_1a_body.position = chassisXY - ((chWd/2)+(legWd_a/2), 0)
                leftLeg_1a_shape = pymunk.Poly.create_box(leftLeg_1a_body, (legWd_a, legHt_a))
                leftLeg_1a_shape.color = 255, 0, 0

                #---first left leg b
                leftLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
                leftLeg_1b_body.position = leftLeg_1a_body.position - ((legWd_a/2)+(legWd_b/2), 0)
                leftLeg_1b_shape = pymunk.Poly.create_box(leftLeg_1b_body, (legWd_b, legHt_b))
                leftLeg_1b_shape.color = 0, 255, 0

                #---first right leg a
                rightLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
                rightLeg_1a_body.position = chassisXY + ((chWd/2)+(legWd_a/2), 0)
                rightLeg_1a_shape = pymunk.Poly.create_box(rightLeg_1a_body, (legWd_a, legHt_a))
                rightLeg_1a_shape.color = 255, 0, 0

                #---first right leg b
                rightLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
                rightLeg_1b_body.position = rightLeg_1a_body.position + ((legWd_a/2)+(legWd_b/2), 0)
                rightLeg_1b_shape = pymunk.Poly.create_box(rightLeg_1b_body, (legWd_b, legHt_b))
                rightLeg_1b_shape.color = 0, 255, 0

                #---link left leg b with left leg a
                pj_ba1left = pymunk.PinJoint(leftLeg_1b_body, leftLeg_1a_body, (legWd_b/2,0), (-legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
                motor_ba1Left = pymunk.SimpleMotor(leftLeg_1b_body, leftLeg_1a_body, relativeAnguVel)
                #---link left leg a with chassis
                pj_ac1left = pymunk.PinJoint(leftLeg_1a_body, chassis_b, (legWd_a/2,0), (-chWd/2, 0))
                motor_ac1Left = pymunk.SimpleMotor(leftLeg_1a_body, chassis_b, relativeAnguVel)
                #---link right leg b with right leg a
                pj_ba1Right = pymunk.PinJoint(rightLeg_1b_body, rightLeg_1a_body, (-legWd_b/2,0), (legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
                motor_ba1Right = pymunk.SimpleMotor(rightLeg_1b_body, rightLeg_1a_body, relativeAnguVel)
                #---link right leg a with chassis
                pj_ac1Right = pymunk.PinJoint(rightLeg_1a_body, chassis_b, (-legWd_a/2,0), (chWd/2, 0))
                motor_ac1Right = pymunk.SimpleMotor(rightLeg_1a_body, chassis_b, relativeAnguVel)

                self.space.add(chassis_b, chassis_shape)
                self.space.add(leftLeg_1a_body, leftLeg_1a_shape, rightLeg_1a_body, rightLeg_1a_shape)
                self.space.add(leftLeg_1b_body, leftLeg_1b_shape, rightLeg_1b_body, rightLeg_1b_shape)
                self.space.add(pj_ba1left, motor_ba1Left, pj_ac1left, motor_ac1Left)
                self.space.add(pj_ba1Right, motor_ba1Right, pj_ac1Right, motor_ac1Right)

                #---prevent collisions with ShapeFilter
                shape_filter = pymunk.ShapeFilter(group=1)
                chassis_shape.filter = shape_filter
                leftLeg_1a_shape.filter = shape_filter
                rightLeg_1a_shape.filter = shape_filter
                leftLeg_1b_shape.filter = shape_filter
                rightLeg_1b_shape.filter = shape_filter


                simulate = False
                rotationRate = 2
                while running:
                for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key in (K_q, K_ESCAPE)):
                #running = False
                sys.exit(0)
                elif event.type == KEYDOWN and event.key == K_s:
                # Start/stop simulation.
                simulate = not simulate
                elif event.type == KEYDOWN and event.key == K_r:
                # Reset.
                # simulate = False
                self.reset_bodies()
                elif event.type == KEYDOWN and event.key == K_UP:
                motor_ba1Left.rate = rotationRate
                elif event.type == KEYDOWN and event.key == K_DOWN:
                motor_ba1Left.rate = -rotationRate
                elif event.type == KEYDOWN and event.key == K_LEFT:
                motor_ac1Left.rate = rotationRate
                elif event.type == KEYDOWN and event.key == K_RIGHT:
                motor_ac1Left.rate = -rotationRate
                elif event.type == KEYUP:
                motor_ba1Left.rate = 0
                motor_ac1Left.rate = 0

                self.draw()

                ### Update physics
                fps = 50
                iterations = 25
                dt = 1.0/float(fps)/float(iterations)
                if simulate:
                for x in range(iterations): # 10 iterations to get a more stable simulation
                self.space.step(dt)

                pygame.display.flip()
                clock.tick(fps)

                if __name__ == '__main__':
                sim = Simulator()
                sim.main()


                It can be controlled with the up, left, right and down arrow keys after first pressing the s key to start the simulation. I've also made sure the variables are created properly linked with each other and named well.



                The part about making the joints move to a desired angle is yet to be implemented, but perhaps that could be calculated by taking the x,y positions of the ends of the joints and using a formula to calculate the angle and then move the motor until it reaches a desired angle.



                If there's a better way, do let me know by posting an answer or editing this one.






                share|improve this answer















                From the servo example I took a hint and implemented this basic leg:



                enter image description here



                import sys

                import pygame
                from pygame.locals import USEREVENT, QUIT, KEYDOWN, KEYUP, K_s, K_r, K_q, K_ESCAPE, K_UP, K_DOWN, K_RIGHT, K_LEFT
                from pygame.color import THECOLORS

                import pymunk
                from pymunk import Vec2d
                import pymunk.pygame_util

                class Simulator(object):

                def __init__(self):
                self.display_flags = 0
                self.display_size = (600, 600)

                self.space = pymunk.Space()
                self.space.gravity = (0.0, -1900.0)
                #self.space.damping = 0.999 # to prevent it from blowing up.

                # Pymunk physics coordinates start from the lower right-hand corner of the screen.
                self.ground_y = 100
                ground = pymunk.Segment(self.space.static_body, (5, self.ground_y), (595, self.ground_y), 1.0)
                ground.friction = 1.0
                self.space.add(ground)

                self.screen = None

                self.draw_options = None

                def reset_bodies(self):
                for body in self.space.bodies:
                if not hasattr(body, 'start_position'):
                continue
                body.position = Vec2d(body.start_position)
                body.force = 0, 0
                body.torque = 0
                body.velocity = 0, 0
                body.angular_velocity = 0
                body.angle = body.start_angle

                def draw(self):
                self.screen.fill(THECOLORS["white"])### Clear the screen
                self.space.debug_draw(self.draw_options)### Draw space
                pygame.display.flip()### All done, lets flip the display

                def main(self):
                pygame.init()
                self.screen = pygame.display.set_mode(self.display_size, self.display_flags)
                width, height = self.screen.get_size()
                self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)

                def to_pygame(p):
                return int(p.x), int(-p.y+height) #Small hack to convert pymunk to pygame coordinates
                def from_pygame(p):
                return to_pygame(p)

                clock = pygame.time.Clock()
                running = True
                font = pygame.font.Font(None, 16)

                # Create the spider
                chassisXY = Vec2d(self.display_size[0]/2, self.ground_y+100)
                chWd = 70; chHt = 50
                chassisMass = 10

                legWd_a = 50; legHt_a = 5
                legWd_b = 100; legHt_b = 5
                legMass = 1
                relativeAnguVel = 0

                #---chassis
                chassis_b = pymunk.Body(chassisMass, pymunk.moment_for_box(chassisMass, (chWd, chHt)))
                chassis_b.position = chassisXY
                chassis_shape = pymunk.Poly.create_box(chassis_b, (chWd, chHt))
                chassis_shape.color = 200, 200, 200
                print("chassis position");print(chassis_b.position)

                #---first left leg a
                leftLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
                leftLeg_1a_body.position = chassisXY - ((chWd/2)+(legWd_a/2), 0)
                leftLeg_1a_shape = pymunk.Poly.create_box(leftLeg_1a_body, (legWd_a, legHt_a))
                leftLeg_1a_shape.color = 255, 0, 0

                #---first left leg b
                leftLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
                leftLeg_1b_body.position = leftLeg_1a_body.position - ((legWd_a/2)+(legWd_b/2), 0)
                leftLeg_1b_shape = pymunk.Poly.create_box(leftLeg_1b_body, (legWd_b, legHt_b))
                leftLeg_1b_shape.color = 0, 255, 0

                #---first right leg a
                rightLeg_1a_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_a, legHt_a)))
                rightLeg_1a_body.position = chassisXY + ((chWd/2)+(legWd_a/2), 0)
                rightLeg_1a_shape = pymunk.Poly.create_box(rightLeg_1a_body, (legWd_a, legHt_a))
                rightLeg_1a_shape.color = 255, 0, 0

                #---first right leg b
                rightLeg_1b_body = pymunk.Body(legMass, pymunk.moment_for_box(legMass, (legWd_b, legHt_b)))
                rightLeg_1b_body.position = rightLeg_1a_body.position + ((legWd_a/2)+(legWd_b/2), 0)
                rightLeg_1b_shape = pymunk.Poly.create_box(rightLeg_1b_body, (legWd_b, legHt_b))
                rightLeg_1b_shape.color = 0, 255, 0

                #---link left leg b with left leg a
                pj_ba1left = pymunk.PinJoint(leftLeg_1b_body, leftLeg_1a_body, (legWd_b/2,0), (-legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
                motor_ba1Left = pymunk.SimpleMotor(leftLeg_1b_body, leftLeg_1a_body, relativeAnguVel)
                #---link left leg a with chassis
                pj_ac1left = pymunk.PinJoint(leftLeg_1a_body, chassis_b, (legWd_a/2,0), (-chWd/2, 0))
                motor_ac1Left = pymunk.SimpleMotor(leftLeg_1a_body, chassis_b, relativeAnguVel)
                #---link right leg b with right leg a
                pj_ba1Right = pymunk.PinJoint(rightLeg_1b_body, rightLeg_1a_body, (-legWd_b/2,0), (legWd_a/2,0))#anchor point coordinates are wrt the body; not the space
                motor_ba1Right = pymunk.SimpleMotor(rightLeg_1b_body, rightLeg_1a_body, relativeAnguVel)
                #---link right leg a with chassis
                pj_ac1Right = pymunk.PinJoint(rightLeg_1a_body, chassis_b, (-legWd_a/2,0), (chWd/2, 0))
                motor_ac1Right = pymunk.SimpleMotor(rightLeg_1a_body, chassis_b, relativeAnguVel)

                self.space.add(chassis_b, chassis_shape)
                self.space.add(leftLeg_1a_body, leftLeg_1a_shape, rightLeg_1a_body, rightLeg_1a_shape)
                self.space.add(leftLeg_1b_body, leftLeg_1b_shape, rightLeg_1b_body, rightLeg_1b_shape)
                self.space.add(pj_ba1left, motor_ba1Left, pj_ac1left, motor_ac1Left)
                self.space.add(pj_ba1Right, motor_ba1Right, pj_ac1Right, motor_ac1Right)

                #---prevent collisions with ShapeFilter
                shape_filter = pymunk.ShapeFilter(group=1)
                chassis_shape.filter = shape_filter
                leftLeg_1a_shape.filter = shape_filter
                rightLeg_1a_shape.filter = shape_filter
                leftLeg_1b_shape.filter = shape_filter
                rightLeg_1b_shape.filter = shape_filter


                simulate = False
                rotationRate = 2
                while running:
                for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key in (K_q, K_ESCAPE)):
                #running = False
                sys.exit(0)
                elif event.type == KEYDOWN and event.key == K_s:
                # Start/stop simulation.
                simulate = not simulate
                elif event.type == KEYDOWN and event.key == K_r:
                # Reset.
                # simulate = False
                self.reset_bodies()
                elif event.type == KEYDOWN and event.key == K_UP:
                motor_ba1Left.rate = rotationRate
                elif event.type == KEYDOWN and event.key == K_DOWN:
                motor_ba1Left.rate = -rotationRate
                elif event.type == KEYDOWN and event.key == K_LEFT:
                motor_ac1Left.rate = rotationRate
                elif event.type == KEYDOWN and event.key == K_RIGHT:
                motor_ac1Left.rate = -rotationRate
                elif event.type == KEYUP:
                motor_ba1Left.rate = 0
                motor_ac1Left.rate = 0

                self.draw()

                ### Update physics
                fps = 50
                iterations = 25
                dt = 1.0/float(fps)/float(iterations)
                if simulate:
                for x in range(iterations): # 10 iterations to get a more stable simulation
                self.space.step(dt)

                pygame.display.flip()
                clock.tick(fps)

                if __name__ == '__main__':
                sim = Simulator()
                sim.main()


                It can be controlled with the up, left, right and down arrow keys after first pressing the s key to start the simulation. I've also made sure the variables are created properly linked with each other and named well.



                The part about making the joints move to a desired angle is yet to be implemented, but perhaps that could be calculated by taking the x,y positions of the ends of the joints and using a formula to calculate the angle and then move the motor until it reaches a desired angle.



                If there's a better way, do let me know by posting an answer or editing this one.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 25 at 15:21

























                answered Mar 24 at 8:24









                NavNav

                9,5342264111




                9,5342264111





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302406%2fhow-to-rotate-pymunk-joints-at-will%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현