Skip to content

Commit

Permalink
Made library work with non-grid envs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoLvs committed Jun 7, 2020
1 parent d48a59c commit 5472d52
Show file tree
Hide file tree
Showing 11 changed files with 2,902 additions and 515 deletions.
269 changes: 269 additions & 0 deletions dev/20200607 - Dev spatial env (not grid).ipynb

Large diffs are not rendered by default.

465 changes: 0 additions & 465 deletions dev/Untitled.ipynb

This file was deleted.

9 changes: 7 additions & 2 deletions dev/dev_js/test_threejs.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );

camera.position.z = 5;
camera.position.z = 20;
camera.position.x = 5;
camera.position.y = 6

var animate = function () {
requestAnimationFrame( animate );
Expand All @@ -36,7 +38,10 @@
};

animate();


var grid = new THREE.GridHelper(100, 10);
scene.add(grid);

// Our Javascript will go here.
</script>
</body>
Expand Down
Binary file added dev/sprites/sprite_arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 29 additions & 18 deletions westworld/agents/grid_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

class BaseGridAgent(BaseRectangle):
def __init__(self,x,y,width = 1,height = 1,color = (255,0,0),circle = False,diagonal = False,
curiosity = 20,search_radius = 2,
curiosity = 20,search_radius = 2,speed = 5,
active_pathfinding = 1.0,
**kwargs,
):
super().__init__(x,y,width,height,color,circle,**kwargs)


# Movement description
self.speed = speed
self.diagonal = diagonal
self.pathfinder = AStar(active_pathfinding)

Expand All @@ -41,18 +42,16 @@ def __repr__(self):
# MOVEMENTS
#=================================================================================

def set_direction(self):
self.direction_angle = np.random.uniform(0,2*np.pi)


def _angle_generator(self):
# Not sure it's the right way to do, control is not easy with generators
i = 0
while True:
if i % self.curiosity == 0:
angle = np.random.uniform(0,2*np.pi)
i += 1
yield angle
def set_direction(self,angle = None):
if angle is None: angle = np.random.uniform(0,360)
self.direction_angle = angle


def turn(self,angle):
self.direction_angle += angle


def explore(self):
pass
Expand All @@ -63,9 +62,13 @@ def when_blocked(self,collisions):

def wander(self):

# Move using an unit movement (adjacent squares only) but more or less in the direction given by the angle
dx,dy = self._sample_unit_movement_from_angle(self.direction_angle)
self.move(dx = dx,dy = dy)
if self.on_grid:
# Move using an unit movement (adjacent squares only) but more or less in the direction given by the angle
dx,dy = self._sample_unit_movement_from_angle(self.direction_angle)
self.move(dx = dx,dy = dy)

else:
self.move(speed = self.speed)

# Change direction based on curiosity value
if self.clock > 1 and self.clock % self.curiosity == 0:
Expand Down Expand Up @@ -209,20 +212,28 @@ def follow_mouse(self,n = None):
self.move_towards(x = x,y = y,n = n)


def move(self,dx = 0,dy = 0,angle = None,dr = None):
def move(self,dx = 0,dy = 0,speed = None,angle = None):

# Store default value for collisions
is_collision = False

if speed is not None:
if angle is None: angle = self.direction_angle
angle = 2* np.pi * angle / 360 - 0.5 * np.pi
dx = int(speed * np.cos(angle))
dy = int(speed * np.sin(angle))

self.move(dx = dx,dy = dy)

# Move using radial movement (with angle and radius)
if angle is not None:
elif angle is not None:

cell_size = self.cell_size

# Compute delta directions with basic trigonometry
# In a grid environment, movement is rounded to the integer to fit in the grid
dx = int(dr * cell_size * np.cos(angle))
dy = int(dr * cell_size * np.sin(angle))
dx = int(speed * cell_size * np.cos(angle))
dy = int(speed * cell_size * np.sin(angle))

return self.move(dx = dx,dy = dy)

Expand Down
3 changes: 2 additions & 1 deletion westworld/assets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .sprites.ball import make_ball
from .sprites.blob import make_blob
from .sprites.blob import make_blob
from .sprites.arrow import make_arrow
Loading

0 comments on commit 5472d52

Please sign in to comment.