Skip to content

move() actual coordinates is wrong - #11

Open
drzraf wants to merge 2 commits into
miniBloq:masterfrom
drzraf:pygame-no-float
Open

move() actual coordinates is wrong#11
drzraf wants to merge 2 commits into
miniBloq:masterfrom
drzraf:pygame-no-float

Conversation

@drzraf

@drzraf drzraf commented Jun 14, 2024

Copy link
Copy Markdown

Add a small fix for Python3 / latest pygame version which require an integer to be passed to move() but more importantly, fixes a (dx/dy) computation error in move()

Example:

        DISTANCE = 100
        miniSim.resetRobot(robot)
        robot.penColor = (0,0,255)
        robot.penDown()
        robot.rotate(45)
        robot.move(DISTANCE)
        
        robot.rotate(-90)
        robot.move(DISTANCE)

        robot.rotate(-135)
        # Moving back from sqrt(2) should get us **exactly** at the starting point!
        robot.move(DISTANCE * math.sqrt(2))

        robot.penUp()
        robot.move(300)

Green points added for convenience to show the true place the robot should have moved to (based on correct math.cos(math.radians(self.heading)) * distance calculation between start and end points) [omit the left-most green point]

Actual:
Screenshot from 2024-06-16 22-31-07

As you can see, move() is shorter than what it should be due to a confusion between rect's (x, y) and (centerx, centery). The fact that move() does trigonometric computation in a loop adds up inaccuracy to the error.

Expected (and using present PR):
Screenshot from 2024-06-16 23-28-45

drzraf added 2 commits June 14, 2024 17:07
- By doing relative move() step after step, trigonometric computation would accumulate floating-point inaccuracies along the loop
- x/centerx were conflated leading to move() being rect.width/2 inferior to the expectation (same for y/centery)
- This was encounterd (and can be reproduced) using:
```
robot.penDown()
robot.rotate(45)
robot.move(DISTANCE)
robot.rotate(-90)
robot.move(DISTANCE)
robot.rotate(-135)
robot.move(DISTANCE * math.sqrt(2))
```

Expected: The robot is back exactly its starting point.
Actual: It's not because both move(DISTANCE) were actually less than DISTANCE.
@drzraf drzraf changed the title pygame expects move() as integer move() actual coordinates is wrong Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant