Skip to content

Commit

Permalink
improve velocity application
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Dec 15, 2024
1 parent 9d1faf9 commit 7c06c8e
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions 2024/14p1.odin
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import "core:fmt"
import "core:log"
import "core:math"
import "core:strconv"
import "core:strings"

Expand Down Expand Up @@ -83,26 +82,9 @@ parse_robots :: proc(input: []string) -> (robots: [dynamic]robot) {
}

apply_robot_velocity :: proc(r: ^robot, seconds: int, bounds: vec2) {
for _ in 0 ..< seconds {
r.pos.x += r.vel.x
r.pos.y += r.vel.y

// if a wall is hit, teleport to the other side
if r.pos.y < 0 {
diff := math.abs(r.pos.y)
r.pos.y = bounds.y - diff + 1
}
if r.pos.y > bounds.y {
diff := r.pos.y - bounds.y
r.pos.y = diff - 1
}
if r.pos.x < 0 {
diff := math.abs(r.pos.x)
r.pos.x = bounds.x - diff + 1
}
if r.pos.x > bounds.x {
diff := r.pos.x - bounds.x
r.pos.x = diff - 1
}
}
r.pos.x += r.vel.x * seconds
r.pos.y += r.vel.y * seconds

r.pos.x = ((r.pos.x % (bounds.x + 1)) + (bounds.x + 1)) % (bounds.x + 1)
r.pos.y = ((r.pos.y % (bounds.y + 1)) + (bounds.y + 1)) % (bounds.y + 1)
}

0 comments on commit 7c06c8e

Please sign in to comment.