-
Notifications
You must be signed in to change notification settings - Fork 7
/
SketchAgents_Step07_IntroducingTime.java
executable file
·119 lines (96 loc) · 3.33 KB
/
SketchAgents_Step07_IntroducingTime.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package de.hfkbremen.algorithmiccliches.examples;
import de.hfkbremen.algorithmiccliches.agents.Vector2f;
import processing.core.PApplet;
public class SketchAgents_Step07_IntroducingTime extends PApplet {
/*
* the agent
* step 07 - introducing time.
*
* introducing:
* delta time
*
* import Vector2f
*/
private Agent myAgent;
public void settings() {
size(1024, 768, P3D);
}
public void setup() {
noFill();
ellipseMode(CENTER);
frameRate(60);
myAgent = new Agent();
myAgent.position.set(width / 2.0f, height / 2.0f);
myAgent.velocity.set(0f, 0f);
myAgent.acceleration.set(-4.0f, -3.0f);
myAgent.radius = 15;
myAgent.maxspeed = 50;
myAgent.maxacceleration = 40;
}
public void draw() {
background(255);
goToMouse(myAgent);
final float mDeltaTime = 1.0f / frameRate;
myAgent.loop(mDeltaTime);
myAgent.draw();
}
private void goToMouse(Agent theAgent) {
/*
* this method is just for quickly observing different
* acceleration settings.
* actually the code below already describes a first
* simple behavior. the agent adjust its acceleration
* vector to 'go to' the mouseposition.
* enjoy.
*/
Vector2f myAccelerationDirection = new Vector2f();
myAccelerationDirection.set(mouseX, mouseY);
myAccelerationDirection.sub(myAgent.position);
theAgent.acceleration.set(myAccelerationDirection);
}
private class Agent {
Vector2f position = new Vector2f();
Vector2f velocity = new Vector2f();
Vector2f acceleration = new Vector2f();
float maxspeed = 0;
float maxacceleration = 0;
float radius = 0;
void loop(float theDeltaTime) {
float myAccelerationSpeed = acceleration.length();
if (myAccelerationSpeed > maxacceleration) {
acceleration.normalize();
acceleration.scale(maxacceleration);
}
Vector2f myTimerAcceleration = new Vector2f();
myTimerAcceleration.set(acceleration);
myTimerAcceleration.scale(theDeltaTime);
velocity.add(myTimerAcceleration);
float mySpeed = velocity.length();
if (mySpeed > maxspeed) {
velocity.normalize();
velocity.scale(maxspeed);
}
Vector2f myTimerVelocity = new Vector2f();
myTimerVelocity.set(velocity);
myTimerVelocity.scale(theDeltaTime);
position.add(myTimerVelocity);
}
void draw() {
stroke(0, 0, 0);
ellipse(position.x, position.y, radius * 2, radius * 2);
stroke(255, 0, 0);
line(position.x,
position.y,
position.x + velocity.x,
position.y + velocity.y);
stroke(0, 255, 0);
line(position.x + velocity.x,
position.y + velocity.y,
position.x + velocity.x + acceleration.x,
position.y + velocity.y + acceleration.y);
}
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchAgents_Step07_IntroducingTime.class.getName()});
}
}