attempt to fix collision
All checks were successful
build rgb-board / build (push) Successful in 5m0s
All checks were successful
build rgb-board / build (push) Successful in 5m0s
This commit is contained in:
@@ -60,45 +60,96 @@ func (a *Animation) updateMarioPosition() {
|
|||||||
centerX := a.ctx.Width() / 2
|
centerX := a.ctx.Width() / 2
|
||||||
centerY := a.ctx.Height() / 2
|
centerY := a.ctx.Height() / 2
|
||||||
|
|
||||||
t := a.mario.angle
|
// Determine sprite size (use current updown image if available)
|
||||||
marioX := int(math.Round(float64(a.mario.a * math.Cos(t))))
|
var sprite image.Image
|
||||||
marioY := int(math.Round(float64(a.mario.b * math.Sin(t))))
|
if img, ok := a.mario.images[a.mario.updown]; ok && img != nil {
|
||||||
|
sprite = img
|
||||||
// Adjust position relative to the center of the panel
|
} else {
|
||||||
marioX += centerX
|
for _, im := range a.mario.images {
|
||||||
marioY += centerY
|
sprite = im
|
||||||
|
break
|
||||||
// Check for edge collision and change direction if necessary
|
}
|
||||||
if marioX < 0 {
|
|
||||||
a.mario.angle = -a.mario.angle + math.Pi // Reflect horizontally and adjust angle
|
|
||||||
} else if marioX >= a.ctx.Width() {
|
|
||||||
a.mario.angle = -a.mario.angle + math.Pi // Reflect horizontally and adjust angle
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if marioY < 0 {
|
// default half sizes if sprite missing
|
||||||
a.mario.angle = math.Pi - a.mario.angle // Flip vertically and adjust angle
|
halfW, halfH := 8, 8
|
||||||
} else if marioY >= a.ctx.Height() {
|
if sprite != nil {
|
||||||
a.mario.angle = math.Pi - a.mario.angle // Flip vertically and adjust angle
|
halfW = sprite.Bounds().Dx() / 2
|
||||||
|
halfH = sprite.Bounds().Dy() / 2
|
||||||
|
}
|
||||||
|
|
||||||
|
// allowable center range so the sprite stays fully on the panel
|
||||||
|
minCenterX := halfW
|
||||||
|
maxCenterX := a.ctx.Width() - 1 - halfW
|
||||||
|
minCenterY := halfH
|
||||||
|
maxCenterY := a.ctx.Height() - 1 - halfH
|
||||||
|
|
||||||
|
// parametric angle before any reflection
|
||||||
|
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
|
||||||
|
|
||||||
|
// 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π)
|
||||||
|
for t < 0 {
|
||||||
|
t += 2 * math.Pi
|
||||||
|
}
|
||||||
|
for t >= 2*math.Pi {
|
||||||
|
t -= 2 * math.Pi
|
||||||
|
}
|
||||||
|
a.mario.angle = t
|
||||||
|
|
||||||
|
// 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
|
||||||
|
if marioX < minCenterX {
|
||||||
|
marioX = minCenterX
|
||||||
|
}
|
||||||
|
if marioX > maxCenterX {
|
||||||
|
marioX = maxCenterX
|
||||||
|
}
|
||||||
|
if marioY < minCenterY {
|
||||||
|
marioY = minCenterY
|
||||||
|
}
|
||||||
|
if marioY > maxCenterY {
|
||||||
|
marioY = maxCenterY
|
||||||
}
|
}
|
||||||
|
|
||||||
a.mario.position.X = marioX
|
a.mario.position.X = marioX
|
||||||
a.mario.position.Y = marioY
|
a.mario.position.Y = marioY
|
||||||
|
|
||||||
// Update angle to move along the ellipse (slower by half)
|
// advance angle for next frame
|
||||||
a.mario.angle += 0.05 // Slow down Mario's movement by half
|
a.mario.angle += 0.05
|
||||||
if a.mario.angle >= 2*math.Pi {
|
if a.mario.angle >= 2*math.Pi {
|
||||||
a.mario.angle -= 2 * math.Pi
|
a.mario.angle -= 2 * math.Pi
|
||||||
}
|
}
|
||||||
|
|
||||||
// Direction logic for up and down movement
|
// Direction logic (based on param t used for the current frame)
|
||||||
if math.Sin(t) > 0 {
|
if math.Sin(t) > 0 {
|
||||||
a.mario.dir.X = -1 // Moving left
|
a.mario.dir.X = -1 // moving left
|
||||||
} else {
|
} else {
|
||||||
a.mario.dir.X = 1 // Moving right
|
a.mario.dir.X = 1 // moving right
|
||||||
}
|
}
|
||||||
if math.Cos(t) > 0 {
|
if math.Cos(t) > 0 {
|
||||||
a.mario.updown = "marioDown" // Moving downward
|
a.mario.updown = "marioDown" // moving downward
|
||||||
} else {
|
} else {
|
||||||
a.mario.updown = "marioUp" // Moving upward
|
a.mario.updown = "marioUp" // moving upward
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user