Skip to content

Commit

Permalink
Finished MathUtils.changeLineLength
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Aug 13, 2023
1 parent 7c8cbf0 commit 5000c70
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "sgine"
ThisBuild / organization := "org.sgine"
ThisBuild / version := "2.0.0b3"
ThisBuild / version := "2.0.0b4-SNAPSHOT"

val scala213 = "2.13.11"

Expand Down
28 changes: 15 additions & 13 deletions core/src/main/scala/org/sgine/util/MathUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import com.badlogic.gdx.math.{Intersector, Vector2}
import org.sgine.component.{Component, DimensionedSupport}

object MathUtils {
private lazy val t1: Vector2 = new Vector2()
private lazy val t2: Vector2 = new Vector2()
private lazy val t3: Vector2 = new Vector2()

def distance(x1: Double, y1: Double, x2: Double, y2: Double): Double =
math.sqrt(math.pow(x2 - x1, 2.0) + math.pow(y2 - y1, 2.0))

Expand All @@ -17,27 +21,25 @@ object MathUtils {
}

def changeLineLength(x1: Double, y1: Double, x2: Double, y2: Double, length: Double): (Double, Double) = {
// TODO: keep the trajectory, but change the length of the line's endpoint
(x2, y2)
val currentLength = math.sqrt(math.pow(x2 - x1, 2.0) + math.pow(y2 - y1, 2.0))
val nx = x2 + (x2 - x1) / currentLength * length
val ny = y2 + (y2 - y1) / currentLength * length
(nx, ny)
}

private lazy val start: Vector2 = new Vector2()
private lazy val end: Vector2 = new Vector2()
private lazy val center: Vector2 = new Vector2()

def intersectSegmentCircle(x1: Double,
y1: Double,
x2: Double,
y2: Double,
circleX: Double,
circleY: Double,
circleRadius: Double): Boolean = {
start.x = x1.toFloat
start.y = y1.toFloat
end.x = x2.toFloat
end.y = y2.toFloat
center.x = circleX.toFloat
center.y = circleY.toFloat
Intersector.intersectSegmentCircle(start, end, center, circleRadius.toFloat)
t1.x = x1.toFloat
t1.y = y1.toFloat
t2.x = x2.toFloat
t2.y = y2.toFloat
t3.x = circleX.toFloat
t3.y = circleY.toFloat
Intersector.intersectSegmentCircle(t1, t2, t3, math.pow(circleRadius, 2.0).toFloat)
}
}
27 changes: 25 additions & 2 deletions core/src/test/scala/examples/TargetingExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ object TargetingExample extends Example {
val y = global.localizeY(target.global.middle)
drawer.color = Color.Green
drawer.line(110.0, 110.0, x, y, 6.0)
val (fx, fy) = MathUtils.changeLineLength(110.0, 110.0, x, y, 1000.0)
drawer.color = Color.Blue
drawer.line(110.0, 110.0, fx, fy, 6.0)
enemies.foreach { e =>
val gx1 = global.center
val gy1 = global.middle
val gx2 = global.x + fx
val gy2 = global.y + fy
val intersecting = MathUtils.intersectSegmentCircle(
x1 = gx1,
y1 = gy1,
x2 = gx2,
y2 = gy2,
circleX = e.global.center,
circleY = e.global.middle,
circleRadius = 110.0
)
e.intersecting @= intersecting
}
}
}

Expand All @@ -61,6 +80,8 @@ object TargetingExample extends Example {
private val r1 = Random.nextDouble()
private val r2 = Random.nextDouble()

val intersecting: Var[Boolean] = Var(false)

center := r1 * screen.width
middle := r2 * screen.height
width @= 200.0
Expand All @@ -78,19 +99,21 @@ object TargetingExample extends Example {
} else {
Color.Red
}
} else if (intersecting()) {
Color.Blue
} else {
Color.White
}
}
}

private lazy val text = new Label("Testing") {
private lazy val text = new Label {
font @= Fonts.OpenSans.Regular.small
color @= Color.White
center @= 100.0
middle @= 100.0

this.text := s"d: ${distance().f()}"
this.text := distance().f()
}

override lazy val children: Children[Component] = Children(
Expand Down

0 comments on commit 5000c70

Please sign in to comment.