dottified
dottified

Reputation: 45

Pymunk - setting position at every timepoint and getting no collisions

I am using pymunk to simulate bodies moving. Because I want the bodies to move in a certain way I am setting their positions every time point (I know that this is not recommended for Chipmunk/Pymunk). Doing this gives me good results for individual bodies moving, but the problem is that when I do this the bodies seem to not be able to detect collisions anymore and they just pass through each other instead of colliding. I have tried making the time step smaller, but that doesn't seem to be helping. Does anyone have any tips for how I could fix this, or is this inescapable given I am setting the positions every time point?

Thanks.

Edited to show example code:

So I'm setting the position and angle like so every time step:

body.position = (body.position[0] + speed*cos(body.angle)*dt + (random term), \
                body.position[1] + speed*sin(body.angle)*dt + (random term))
body.angle = body.angle + body.angular_velocity*dt + (random term)

I want to be able to do this because I have rectangular bodies and this code allows the rectangles to move along their long axes, and it works great for just one body, but when I have multiple bodies I also want collisions to work and this seems to make them not work at all (or if they're working, they're working very badly).

Upvotes: 0

Views: 1133

Answers (2)

SliceofBread
SliceofBread

Reputation: 11

In your question you already hint towards the reason why your sprites are not reacting to eachother (colliding and imparting movement on eachother when colliding).

It is because you are setting their positions every timepoint.

When you set the position of a sprite you instantly teleport it to that location, even if this means your sprite will overlap with, or pass through another sprite. Even if the sprites would be influenced by the collision, they would still be teleported to your predefined location in the next timepoint.

If you want your sprites to react with eachother while moving, you would have to use the pymunk engine to move them by changing the velocity of the object. The velocity of the object will be a tuple containing the horizontal and vertical velocity.

Upvotes: 1

viblo
viblo

Reputation: 4603

So, if you set the position of a body that body will move to exactly that position instantly, just like if you teleported it there. If there is some other object already there, then that object should be pushed away over the next couple of time steps. You will also be able to get collision data, for example by using a collision callback, for example like in this example

import pymunk

s = p.Space()
b1 = p.Body(1, 1)
c1 = p.Circle(b1, 10)
b2 = p.Body(1, 1)
c2 = p.Circle(b2, 10)
s.add(b1, c1, b2, c2)

self.hits = 0
def begin(space, arb, data):
    self.hits += h.data["test"]
    return True

h = s.add_collision_handler(0, 0)
h.data["test"] = 1
h.begin = begin

for x in range(10):
    s.step(0.1)

print(self.hits)

Upvotes: 0

Related Questions