Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions robots/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions robots/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions robots/.idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions robots/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions robots/.idea/google-java-format.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions robots/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions robots/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions robots/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions robots/Robots.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
</component>
</module>
1 change: 1 addition & 0 deletions robots/resources/text.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
topic = Style
1 change: 1 addition & 0 deletions robots/resources/text_ru_RU.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
topic = Как это Звать?
151 changes: 151 additions & 0 deletions robots/src/gui/GameDataModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package gui;

import java.awt.*;
import java.util.Observable;

public class GameDataModel extends Observable {
double angularVelocity = 0;
int counter = 0;
private volatile double m_robotPositionX = 100;
public static String KEY_COORDINATE_ROBOT = "robot changed";
public static String KEY_COORDINATE_TARGET = "target changed";
private volatile double m_robotPositionY = 100;
private volatile double m_robotDirection = 0;

private volatile int m_targetPositionX = 250;
private volatile int m_targetPositionY = 100;

private static final double maxVelocity = 0.1;
private static final double maxAngularVelocity = 0.001;

public double getM_robotPositionX() {
return m_robotPositionX;
}

public double getM_robotPositionY() {
return m_robotPositionY;
}

public double getM_robotDirection() {
return m_robotDirection;
}

public int getM_targetPositionX() {
return m_targetPositionX;
}

public int getM_targetPositionY() {
return m_targetPositionY;
}


protected void setTargetPosition(Point p) {
m_targetPositionX = p.x;
m_targetPositionY = p.y;
setChanged();
notifyObservers(KEY_COORDINATE_TARGET);
clearChanged();
}


private static double distance(double x1, double y1, double x2, double y2) {
double diffX = x1 - x2;
double diffY = y1 - y2;
return Math.sqrt(diffX * diffX + diffY * diffY);
}

private static double angleTo(double fromX, double fromY, double toX, double toY) {
double diffX = toX - fromX;
double diffY = toY - fromY;
return asNormalizedRadians(Math.atan2(diffY, diffX));
}

protected void onModelUpdateEvent() {

double distance = distance(m_targetPositionX, m_targetPositionY,
m_robotPositionX, m_robotPositionY);
if (distance < 0.5) {
counter = 0;
return;
}
double velocity = maxVelocity;
double angleToTarget = angleTo(m_robotPositionX, m_robotPositionY, m_targetPositionX, m_targetPositionY);


// System.out.println();
System.out.println(counter);
if (distance>=angularVelocity*maxVelocity&&angleToTarget!=m_robotDirection) {
if (counter<2) {
if (angleToTarget > m_robotDirection && angularVelocity != maxAngularVelocity) {
counter++;
angularVelocity = maxAngularVelocity;
}
if (angleToTarget < m_robotDirection && angularVelocity != -maxAngularVelocity) {
counter++;
angularVelocity = -maxAngularVelocity;
}
}else {
if (Math.abs(angleToTarget - m_robotDirection)<0.1) {
angularVelocity = 0;
if (angleToTarget > m_robotDirection)
angularVelocity = maxAngularVelocity;
if (angleToTarget < m_robotDirection)
angularVelocity = -maxAngularVelocity;
}
}
}

moveRobot(velocity, angularVelocity, 10);
}

private static double asNormalizedRadians(double angle) {

if (angle < 0) {
// System.out.println("<");
angle += 2 * Math.PI;
}
if (angle >= 2* Math.PI) {
// System.out.println(">");
angle -= 2 * Math.PI;
}
return angle;
}

private static double applyLimits(double value, double min, double max) {
if (value < min)
return min;
if (value > max)
return max;
return value;
}

private void moveRobot(double velocity, double angularVelocity, double duration) {
velocity = applyLimits(velocity, 0, maxVelocity);
angularVelocity = applyLimits(angularVelocity, -maxAngularVelocity, maxAngularVelocity);
double newX = m_robotPositionX + velocity / angularVelocity *
(Math.sin(m_robotDirection + angularVelocity * duration) -
Math.sin(m_robotDirection));
if (!Double.isFinite(newX)) {
newX = m_robotPositionX + velocity * duration * Math.cos(m_robotDirection);
}
double newY = m_robotPositionY - velocity / angularVelocity *
(Math.cos(m_robotDirection + angularVelocity * duration) -
Math.cos(m_robotDirection));
if (!Double.isFinite(newY)) {
newY = m_robotPositionY + velocity * duration * Math.sin(m_robotDirection);
}
m_robotPositionX = newX;
m_robotPositionY = newY;
double newDirection = asNormalizedRadians(m_robotDirection + angularVelocity * duration );
m_robotDirection = newDirection;
setChanged();
notifyObservers(KEY_COORDINATE_ROBOT);
clearChanged();
}



static int round(double value) {
return (int) (value + 0.5);
}
}
Loading