diff --git a/animationMario.go b/animationMario.go index cb53655..7a6dbd9 100644 --- a/animationMario.go +++ b/animationMario.go @@ -96,51 +96,19 @@ func (a *Animation) updateMarioPosition() { maxCenterY = centerY } - // parametric angle before any reflection + // advance angle for this frame + delta := 0.05 t := a.mario.angle - - // compute candidate center position on the ellipse - marioX := int(math.Round(a.mario.a*math.Cos(t))) + centerX - 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 + tNext := t + delta + if tNext >= 2*math.Pi { + tNext -= 2 * math.Pi } - // normalize angle into [0, 2π) - for t < 0 { - t += 2 * math.Pi - } - for t >= 2*math.Pi { - t -= 2 * math.Pi - } - a.mario.angle = t + // compute next candidate center from the next parametric position + marioX := int(math.Round(a.mario.a*math.Cos(tNext))) + centerX + marioY := int(math.Round(a.mario.b*math.Sin(tNext))) + centerY - // recompute position from possibly-updated angle so sprite is inside bounds - 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 + // clamp into allowed center ranges so sprite stays fully on panel if marioX < minCenterX { marioX = minCenterX } @@ -154,24 +122,30 @@ func (a *Animation) updateMarioPosition() { marioY = maxCenterY } + // commit position and advance angle a.mario.position.X = marioX a.mario.position.Y = marioY - - // advance angle for next frame - a.mario.angle += 0.05 - if a.mario.angle >= 2*math.Pi { - a.mario.angle -= 2 * math.Pi - } - - // Direction logic (based on param t used for the current frame) - if math.Sin(t) > 0 { - a.mario.dir.X = -1 // moving left + a.mario.angle = tNext + + // Direction logic (based on the motion between t and tNext) + // approximate direction by comparing positions (preferable to trig sign when clamped) + prevX := int(math.Round(a.mario.a*math.Cos(t))) + centerX + if prevX == marioX { + // if we didn't move horizontally, keep previous direction + // (this prevents flicker when clamped) + } else if marioX < prevX { + a.mario.dir.X = -1 } 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 { - a.mario.updown = "marioUp" // moving upward + a.mario.updown = "marioDown" } }