it.unibo.alchemist.model.physics.actions.Spin.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemist-physics Show documentation
Show all versions of alchemist-physics Show documentation
Abstraction for pedestrians capable of interacting physically.
/*
* Copyright (C) 2010-2023, Danilo Pianini and contributors
* listed, for each module, in the respective subproject's build.gradle.kts file.
*
* This file is part of Alchemist, and is distributed under the terms of the
* GNU General Public License, with a linking exception,
* as described in the file LICENSE in the Alchemist distribution's top directory.
*/
package it.unibo.alchemist.model.physics.actions
import it.unibo.alchemist.model.Context
import it.unibo.alchemist.model.Node
import it.unibo.alchemist.model.Reaction
import it.unibo.alchemist.model.actions.AbstractAction
import it.unibo.alchemist.model.physics.environments.Physics2DEnvironment
import org.apache.commons.math3.util.FastMath.toRadians
import kotlin.math.cos
import kotlin.math.sin
/**
* Spins a [node] around itself at [angularSpeedDegrees] normalized according to the speed of the [reaction].
*/
class Spin(
node: Node,
private val reaction: Reaction,
private val environment: Physics2DEnvironment,
private val angularSpeedDegrees: Double,
) : AbstractAction(node) {
private val angularSpeedRadians = toRadians(angularSpeedDegrees)
override fun cloneAction(node: Node, reaction: Reaction) =
Spin(node, reaction, environment, angularSpeedDegrees)
/**
* Spins the node around itself.
*/
override fun execute() {
val realSpeed = angularSpeedRadians / reaction.timeDistribution.rate
val headingAngle = environment.getHeading(node).asAngle + realSpeed
environment.setHeading(node, environment.makePosition(cos(headingAngle), sin(headingAngle)))
}
override fun getContext() = Context.LOCAL
}