I have a circle with a box on top:

A circle is a simple engine. I want the box to stay right above the circle. I tried various restrictions, but most of my attempts force the box to stand aside.
My most successful attempt was to set box body.moment to pymunk.inf and snap the field to a circle. This is getting closer, but the box is still moving side to side when I want to right above the center of the circle. I could manually install it there, but it seems to me that I can do this with some kind of restriction.
Any ideas? The following is sample code using the Pymunk and Arcade libraries.
import arcade
import pymunk
import math
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
BOX_SIZE = 45
class MyApplication(arcade.Window):
""" Main application class. """
def __init__(self, width, height):
super().__init__(width, height)
arcade.set_background_color(arcade.color.DARK_SLATE_GRAY)
self.space = pymunk.Space()
self.space.gravity = (0.0, -900.0)
body = pymunk.Body(body_type=pymunk.Body.STATIC)
self.floor = pymunk.Segment(body, [0, 10], [SCREEN_WIDTH, 10], 0.0)
self.floor.friction = 10
self.space.add(self.floor)
player_x = 300
player_y = 300
mass = 2
radius = 25
inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
circle_body = pymunk.Body(mass, inertia)
circle_body.position = pymunk.Vec2d(player_x, player_y)
self.circle_shape = pymunk.Circle(circle_body, radius, pymunk.Vec2d(0, 0))
self.circle_shape.friction = 1
self.space.add(circle_body, self.circle_shape)
size = BOX_SIZE
mass = 5
moment = pymunk.moment_for_box(mass, (size, size))
moment = pymunk.inf
body = pymunk.Body(mass, moment)
body.position = pymunk.Vec2d(player_x, player_y + 49)
self.box_shape = pymunk.Poly.create_box(body, (size, size))
self.box_shape.friction = 0.3
self.space.add(body, self.box_shape)
constraint = pymunk.constraint.PinJoint(self.box_shape.body, self.circle_shape.body)
self.space.add(constraint)
constraint = pymunk.constraint.SimpleMotor(self.circle_shape.body, self.box_shape.body, -3)
self.space.add(constraint)
def on_draw(self):
"""
Render the screen.
"""
arcade.start_render()
arcade.draw_circle_outline(self.circle_shape.body.position[0],
self.circle_shape.body.position[1],
self.circle_shape.radius,
arcade.color.WHITE,
2)
arcade.draw_rectangle_outline(self.box_shape.body.position[0],
self.box_shape.body.position[1],
BOX_SIZE,
BOX_SIZE,
arcade.color.WHITE, 2,
tilt_angle=math.degrees(self.box_shape.body.angle))
pv1 = self.floor.body.position + self.floor.a.rotated(self.floor.body.angle)
pv2 = self.floor.body.position + self.floor.b.rotated(self.floor.body.angle)
arcade.draw_line(pv1.x, pv1.y, pv2.x, pv2.y, arcade.color.WHITE, 2)
def animate(self, delta_time):
self.space.step(1 / 80.0)
window = MyApplication(SCREEN_WIDTH, SCREEN_HEIGHT)
arcade.run()