bad ai
build rgb-board / build (push) Successful in 4m43s Details

main
Nathan Wagner 3 days ago
parent 10cafdc209
commit bab2f64cac

@ -96,51 +96,19 @@ func (a *Animation) updateMarioPosition() {
maxCenterY = centerY maxCenterY = centerY
} }
// parametric angle before any reflection // advance angle for this frame
delta := 0.05
t := a.mario.angle t := a.mario.angle
tNext := t + delta
// compute candidate center position on the ellipse if tNext >= 2*math.Pi {
marioX := int(math.Round(a.mario.a*math.Cos(t))) + centerX tNext -= 2 * math.Pi
marioY := int(math.Round(a.mario.b*math.Sin(t))) + centerY
// detect collisions against allowed center ranges
collidedX := marioX < minCenterX || marioX > maxCenterX
collidedY := marioY < minCenterY || marioY > maxCenterY
// If allowed range collapsed to the center, don't reflect on that axis
// because reflection would keep flipping every frame. Treat that as
// non-colliding for reflection purposes (we'll clamp position later).
if minCenterX == maxCenterX {
collidedX = false
}
if minCenterY == maxCenterY {
collidedY = false
}
// Reflect the parametric angle correctly:
// - For horizontal collision we want cos(t_new) = -cos(t) => t_new = Pi - t
// - For vertical collision we want sin(t_new) = -sin(t) => t_new = -t
if collidedX {
t = math.Pi - t
}
if collidedY {
t = -t
} }
// normalize angle into [0, 2π) // compute next candidate center from the next parametric position
for t < 0 { marioX := int(math.Round(a.mario.a*math.Cos(tNext))) + centerX
t += 2 * math.Pi marioY := int(math.Round(a.mario.b*math.Sin(tNext))) + centerY
}
for t >= 2*math.Pi {
t -= 2 * math.Pi
}
a.mario.angle = t
// recompute position from possibly-updated angle so sprite is inside bounds // clamp into allowed center ranges so sprite stays fully on panel
marioX = int(math.Round(a.mario.a*math.Cos(t))) + centerX
marioY = int(math.Round(a.mario.b*math.Sin(t))) + centerY
// clamp as a safety net
if marioX < minCenterX { if marioX < minCenterX {
marioX = minCenterX marioX = minCenterX
} }
@ -154,24 +122,30 @@ func (a *Animation) updateMarioPosition() {
marioY = maxCenterY marioY = maxCenterY
} }
// commit position and advance angle
a.mario.position.X = marioX a.mario.position.X = marioX
a.mario.position.Y = marioY a.mario.position.Y = marioY
a.mario.angle = tNext
// advance angle for next frame
a.mario.angle += 0.05 // Direction logic (based on the motion between t and tNext)
if a.mario.angle >= 2*math.Pi { // approximate direction by comparing positions (preferable to trig sign when clamped)
a.mario.angle -= 2 * math.Pi prevX := int(math.Round(a.mario.a*math.Cos(t))) + centerX
} if prevX == marioX {
// if we didn't move horizontally, keep previous direction
// Direction logic (based on param t used for the current frame) // (this prevents flicker when clamped)
if math.Sin(t) > 0 { } else if marioX < prevX {
a.mario.dir.X = -1 // moving left a.mario.dir.X = -1
} else { } else {
a.mario.dir.X = 1 // moving right a.mario.dir.X = 1
} }
if math.Cos(t) > 0 {
a.mario.updown = "marioDown" // moving downward // up/down based on vertical movement
prevY := int(math.Round(a.mario.b*math.Sin(t))) + centerY
if prevY == marioY {
// keep previous up/down state
} else if marioY < prevY {
a.mario.updown = "marioUp"
} else { } else {
a.mario.updown = "marioUp" // moving upward a.mario.updown = "marioDown"
} }
} }

Loading…
Cancel
Save