0

so im trying to make my car to stay in the start of the screen while moving every thing else to the left but im not ver succesfull at that. my car dont need any inputs from a player it always run with the same speed. here is what ive maid so far -

def update(dt):
    global car_x
    global CanIStart

    space.step(0.02)
    for shape in space.shapes:
        if(shape.id == 1):
            carRunNow = int(shape.body.position[0]) - car_x
            car_x = int(shape.body.position[0])
            CanIStart = True


    if(CanIStart):
        for shape in space.shapes:
            if(shape.id == 15):
                space.remove(shape)
                shape.body.position = (int(shape.body.position.x)-carRunNow, int(shape.body.position.y))
                space.add(shape) 

Here is my road code -

def make_road(space, size, posOne, run):
    if(run == 10):
        part_shape = pymunk.Segment(space.static_body, (0, 150), (300, 150), 2)
        part_shape.body.position = 0, 0      # Set the position of the body
        part_shape.elasticity = 0.62
        part_shape.friction = 0.62
        part_shape.id = 15
        space.add(part_shape)
        make_road(space, size, (300, 150), run-1)
        return
    elif(run > 0):
        modifier = random.randint(-80,80)
        while(posOne[1] + modifier < 0):
            modifier = random.randint(-80,80)
        part_shape = pymunk.Segment(space.static_body, posOne, (posOne[0] + size, posOne[1] + modifier), 2)
        part_shape.elasticity = 0.62
        part_shape.friction = 0.62
        part_shape.id = 15
        space.add(part_shape)
        print(posOne)
        make_road(space, size, (posOne[0] + size, posOne[1] + modifier), run - 1)
        return
    else:
        return

the shape id 15 is the road. this code makes some problams - the first one the car's wheels go through the road and the second is that the movment isnt very smoothly.

thanks for the help :)

  • Im unusre what you need help with? In the headline you wrote camera, but in the end of your question you writer about car wheels and not smooth movement? – viblo Jan 04 '20 at 18:53
  • i want a camera that follows the car but the way im doing it creates problems with my car. but the way im updating my camere basiclly that im removing and adding the shape and body over and over makes the car go through it – Daniel Shinar Jan 04 '20 at 18:57

1 Answers1

0

It is usually not a good idea to remove - add objects all the time. Pymunk (actually the underlying c library, chipmunk) keeps a buffer or cache between updates, which get lost if you do what you do, resulting in a less stable, smooth and performant simulation.

Instead I suggest you update your camera logic so that you do not need to remove - add all the road all the time. Exactly how to do that is a bit difficult to answer without knowing how your camera/drawing code works.

viblo
  • 4,159
  • 4
  • 20
  • 28