Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to implement an AI to solve a simple task: move from A to B, while avoiding obstacles.
So far I used
pymunk
and
pygame
to build the enviroment and this works quite fine. But now I am facing the next step: to get rewards for my reinforcement learning algorithm I need to detect the collision between the player and, for example, a wall. Or simply to restart the enviroment when a wall/obstacle gets hit.
Setting the
c_handler.begin
function equals the
Game.restart
fuctions helped me to print out that the player actually hit something.
But except from
print()
I can't access any other function concerning the player position and I don't really know what to do next.
So how can i use the pymunk collision to restart the environment? Or are there other ways for resetting or even other libraries to build a proper enviroment?
def restart(self, arbiter, data):
car.body.position = 50, 50
return True
def main(self):
[...]
c_handler = space.add_collision_handler(1,2)
c_handler.begin = Game.restart
[...]
In general it seems like it would be useful for you to read up a bit on how classes works in python, particularly how class instance variables works.
Anyway, if you already know you want to manipulate the car variable, you can store it in the class itself. Then since you have self available in the restart method you can just do whatever there.
Or, the other option is to find out the body that you want to change from the arbiter that is passed into the callback.
option 1:
class MyClass:
def restart(self, space, arbiter, data):
self.car.body.position = 50,50
return True
def main(self):
[...]
self.car = car
c_handler = space.add_collision_handler(1,2)
c_handler.begin = self.restart
[...]
option 2:
def restart(space, arbiter, data):
arbiter.shapes[0].body.position = 50,50
# or maybe its the other shape, in that case you should do this instead
# arbiter.shapes[1].body.position = 50,50
–
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.