Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
skript:bewegungen [2017/05/11 11:13] d.golovko |
skript:bewegungen [2018/05/09 18:12] (aktuell) d.golovko |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== Simulation von Bewegungen ====== | ====== Simulation von Bewegungen ====== | ||
+ | Wir machen eine Zeichnung, die unseren Roboter darstellen soll, und lassen sie bewegen. Unser "Roboter" sieht so aus: | ||
- | Programm, welches den Roboter vorwärts bewegen und drehen lässt. Realisierung mit globalen Variablen ohne Klassen: | + | |
+ | {{:skript:robot-sketch.png?70|}} | ||
+ | |||
+ | |||
+ | Programm, welches den Roboter vorwärts bewegen und drehen lässt. Version mit globalen Variablen ohne Klassen: | ||
<code java> | <code java> | ||
- | // current x coordinate of the robot center: | + | int radius = 25; // Radius des Roboters |
- | float myPosX = 100; | + | float xPos = 300; // x-Position des Roboters |
- | // current y coordinate of the robot center: | + | float yPos = 200; // y-Position des Roboters |
- | float myPosY = 100; | + | float angle = 0; // Richtung des Roboters |
- | // robot radius: | + | |
- | float myRadius = 25; | + | |
- | // current angle between the robot's pointer and the x axis, in radian: | + | |
- | float myAngle = 0; | + | |
void setup() { | void setup() { | ||
size(600, 400); | size(600, 400); | ||
- | drawRobot(25, 100, 100, 0); | ||
} | } | ||
void draw() { | void draw() { | ||
- | background(255); | + | clear(); |
+ | drawRobot(); | ||
+ | move(4); | ||
turn(0.1); | turn(0.1); | ||
- | run(10); | ||
- | drawRobot(myRadius, myPosX, myPosY, myAngle); | ||
} | } | ||
- | /** | + | /* |
- | * Moves the robot forward | + | * Zeichnet den Roboter |
- | * float distance: distance the robot should travel | + | */ |
- | */ | + | void drawRobot() { |
- | void run(float distance) { | + | ellipse(xPos, yPos, radius*2, radius*2); |
- | myPosX = myPosX + distance*cos(myAngle); | + | // *2, weil die ellipse-Funktion den Diameter verwendet |
- | myPosY = myPosY + distance*sin(myAngle); | + | line(xPos, yPos, xPos + radius * cos(angle), yPos + radius * sin(angle)); |
+ | // Linie zwischen dem Roboterzentrum und dem Roboterrand in die Bewegungsrichtung | ||
} | } | ||
- | /** | + | /* |
- | * Changes the robot's direction | + | * Bewegt den Roboter nach vorne in seine Richtung |
- | * float angleChange: angle by which the robot's direction should be changed in radians | + | */ |
- | */ | + | void move(float distance) { |
- | void turn(float angleChange) { | + | xPos = xPos + distance * cos(angle); |
- | myAngle = myAngle + angleChange; | + | yPos = yPos + distance * sin(angle); |
} | } | ||
- | /** | + | /* |
- | * Draws a robot on the canvas. | + | * Rotiert den Roboter |
- | * float radius: radius of the robot | + | |
- | * float posX: x coordinate of the robot's center | + | |
- | * float posY: y coordinate of the robot's center | + | |
- | * float direction: angle between the robot's pointer and canvas x axis in radians | + | |
*/ | */ | ||
- | void drawRobot(float radius, float posX, float posY, float direction) { | + | void turn(float angleDiff) { |
- | + | angle = angle + angleDiff; | |
- | // position the circle: | + | |
- | ellipse(posX, posY, 2*radius, 2*radius); | + | |
- | + | ||
- | // calculate the position of the pointer line (the line is +20% longer than the radius): | + | |
- | float endOfLineX = posX + radius * 1.2 * cos(direction); | + | |
- | float endOfLineY = posY + radius * 1.2 * sin(direction); | + | |
- | + | ||
- | line(posX, posY, endOfLineX, endOfLineY); | + | |
} | } | ||
- | |||
</code> | </code> |