Skip to content

Commit ed6921f

Browse files
committed
Created forward_backward function, which calculates the next position the agent will move. forward function, which moves the agent forward by specified amount. back function, which moves the agent backwards by specified amount. right function, turns the agent right by specified degree. left function, turns the agent left by specified degree.
1 parent 1e4e46f commit ed6921f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

mesa/agent.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ def step(self) -> None:
3737
def advance(self) -> None:
3838
pass
3939

40+
def forward_backward(self, amount, sign):
41+
"""Does the calculation to find the agent's next move and is used within the forward and backward functions"""
42+
new_x = float(self.pos[0]) + sign * math.cos(self.heading * math.pi / 180) * amount
43+
new_y = float(self.pos[1]) + sign * math.sin(self.heading * math.pi / -180) * amount
44+
next_move = (new_x, new_y)
45+
self.model.space.move_agent(self, next_move)
46+
47+
def forward(self, amount):
48+
"""Moves the agent forward by the amount given"""
49+
self.forward_backward(amount, 1)
50+
51+
def back(self, amount):
52+
"""Moves the agent backwards from where its facing by the given amount"""
53+
self.forward_backward(amount, -1)
54+
55+
def right(self, degree):
56+
"""Turns the agent right by the given degree"""
57+
self.heading = (self.heading - degree) % 360
58+
59+
def left(self, degree):
60+
"""Turns the agent left by the given degree"""
61+
self.heading = (self.heading + degree) % 360
62+
4063
def setxy(self, x, y):
4164
"""Sets the current position to the specified x,y parameters"""
4265
self.pos = (x, y)

0 commit comments

Comments
 (0)