minor modification of the animation

This commit is contained in:
hneemann 2018-11-26 19:53:56 +01:00
parent 3527de0839
commit bac805fbea
4 changed files with 45 additions and 12 deletions

View File

@ -131,6 +131,12 @@ public class VectorFloat implements VectorInterface {
return Objects.hash(x, y); return Objects.hash(x, y);
} }
@Override
public String toString() {
return "(x=" + x
+ ", y=" + y
+ ')';
}
@Override @Override
public Vector round() { public Vector round() {

View File

@ -13,6 +13,20 @@ public final class FSMDemos {
private FSMDemos() { private FSMDemos() {
} }
/**
* Blink
*
* @return the fsm
*/
public static FSM blink() {
State off = new State("off");
State on = new State("on");
return new FSM(off, on)
.transition(on, off, null)
.transition(off, on, null);
}
/** /**
* Creates a debounced rotary switch decoder * Creates a debounced rotary switch decoder
* *

View File

@ -13,6 +13,11 @@ import de.neemann.digital.draw.graphics.VectorFloat;
* @param <A> the type of the implementing class * @param <A> the type of the implementing class
*/ */
public class Movable<A extends Movable> { public class Movable<A extends Movable> {
private static final float MASS = 50f;
private static final float FRICTION = 0.8f;
private static final float MAX_FORCE = 100000f;
private static final float MAX_FORCE_CHECK = (float) (MAX_FORCE / Math.sqrt(2));
private String values = ""; private String values = "";
private VectorFloat position; private VectorFloat position;
private transient VectorFloat speed; private transient VectorFloat speed;
@ -25,7 +30,7 @@ public class Movable<A extends Movable> {
public Movable() { public Movable() {
force = new VectorFloat(0, 0); force = new VectorFloat(0, 0);
speed = new VectorFloat(0, 0); speed = new VectorFloat(0, 0);
position = new VectorFloat((float) Math.random() - 0.5f, (float) Math.random() - 0.5f).mul(100); position = new VectorFloat(0, 0);
} }
/** /**
@ -129,15 +134,20 @@ public class Movable<A extends Movable> {
/** /**
* Moves the element * Moves the element
* *
* @param dt the time step * @param dt the time step in ms
*/ */
public void move(int dt) { public void move(int dt) {
if (Math.abs(force.getXFloat()) > MAX_FORCE_CHECK || Math.abs(force.getYFloat()) > MAX_FORCE_CHECK) {
double len = force.len();
if (len > MAX_FORCE)
force = force.norm().mul(MAX_FORCE);
}
if (speed == null) if (speed == null)
speed = force.mul(dt / 200f); speed = force.mul(dt / MASS);
else else
speed = speed.add(force.mul(dt / 200f)); speed = speed.add(force.mul(dt / MASS));
setPos(position.add(speed.mul(dt / 1000f))); setPos(position.add(speed.mul(dt / 1000f)));
speed = speed.mul(0.7f); speed = speed.mul(FRICTION);
} }
void setFSM(FSM fsm) { void setFSM(FSM fsm) {

View File

@ -93,11 +93,14 @@ public class Transition extends Movable<Transition> {
public void setPos(VectorFloat position) { public void setPos(VectorFloat position) {
if (fromState != toState) { if (fromState != toState) {
VectorFloat dist = fromState.getPos().sub(toState.getPos()); VectorFloat dist = fromState.getPos().sub(toState.getPos());
if (dist.getXFloat() != 0 || dist.getYFloat() != 0) {
VectorFloat p = position.sub(fromState.getPos()); VectorFloat p = position.sub(fromState.getPos());
VectorFloat n = dist.getOrthogonal().norm(); VectorFloat n = dist.getOrthogonal().norm();
float l = p.mul(n); float l = p.mul(n);
super.setPos(fromState.getPos().sub(dist.mul(0.5f)).add(n.mul(l))); super.setPos(fromState.getPos().sub(dist.mul(0.5f)).add(n.mul(l)));
} else return;
}
}
super.setPos(position); super.setPos(position);
} }
@ -152,7 +155,7 @@ public class Transition extends Movable<Transition> {
*/ */
void initPos() { void initPos() {
setPos(fromState.getPos().add(toState.getPos()).mul(0.5f) setPos(fromState.getPos().add(toState.getPos()).mul(0.5f)
.add(new VectorFloat((float) Math.random() - 0.5f, (float) Math.random() - 0.5f).mul(30))); .add(new VectorFloat((float) Math.random() - 0.5f, (float) Math.random() - 0.5f).mul(2)));
} }
/** /**