| 383 | } |
| 384 | |
| 385 | func separationOfAxes(sc, other SpaceComponent, hb, otherHB Shape) (bool, engo.Point) { |
| 386 | sin, cos := math.Sincos(sc.Rotation * math.Pi / 180) |
| 387 | othersin, othercos := math.Sincos(other.Rotation * math.Pi / 180) |
| 388 | overlap := float32(math.MaxFloat32) |
| 389 | smallestAxis := engo.Point{} |
| 390 | axes := []engo.Point{} |
| 391 | for _, axis := range hb.Lines { |
| 392 | pt := engo.Point{ |
| 393 | X: axis.P2.X*cos - axis.P2.Y*sin - axis.P1.X*cos + axis.P1.Y*sin, |
| 394 | Y: axis.P2.Y*cos + axis.P2.X*sin - axis.P1.Y*cos - axis.P1.X*sin, |
| 395 | } |
| 396 | norm, _ := pt.Normalize() |
| 397 | axes = append(axes, engo.Point{ |
| 398 | X: norm.Y * -1, |
| 399 | Y: norm.X, |
| 400 | }) |
| 401 | } |
| 402 | otherAxes := []engo.Point{} |
| 403 | for _, axis := range otherHB.Lines { |
| 404 | pt := engo.Point{ |
| 405 | X: axis.P2.X*othercos - axis.P2.Y*othersin - axis.P1.X*othercos + axis.P1.Y*othersin, |
| 406 | Y: axis.P2.Y*othercos + axis.P2.X*othersin - axis.P1.Y*othercos - axis.P1.X*othersin, |
| 407 | } |
| 408 | norm, _ := pt.Normalize() |
| 409 | otherAxes = append(otherAxes, engo.Point{ |
| 410 | X: norm.Y * -1, |
| 411 | Y: norm.X, |
| 412 | }) |
| 413 | } |
| 414 | for _, axis := range axes { |
| 415 | p1min, p1max := hb.Project(axis, sc) |
| 416 | p2min, p2max := otherHB.Project(axis, other) |
| 417 | if p2min > p1max { |
| 418 | return false, engo.Point{} |
| 419 | } |
| 420 | var o float32 |
| 421 | if p1min < p2min { |
| 422 | if p1max < p2max { |
| 423 | o = p1max - p2min |
| 424 | } else { |
| 425 | o = p1max - p1min |
| 426 | } |
| 427 | } else { |
| 428 | if p1max < p2max { |
| 429 | o = p2max - p1min |
| 430 | } else { |
| 431 | o = p1max - p1min |
| 432 | } |
| 433 | } |
| 434 | if o < overlap { |
| 435 | overlap = o |
| 436 | smallestAxis = axis |
| 437 | } |
| 438 | } |
| 439 | for _, axis := range otherAxes { |
| 440 | p1min, p1max := hb.Project(axis, sc) |
| 441 | p2min, p2max := otherHB.Project(axis, other) |
| 442 | if p2min > p1max { |