Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DEMO
- Agent orange;
- void setup() {
- size(600, 600);
- orange = new Agent();
- }
- void draw() {
- background(255);
- orange.update();
- orange.display();
- }
- void mousePressed() {
- //orange.velocity.mult(1.1);
- orange.turn(PI/6);
- }
- //
- //Agent with rotations
- //
- class Agent {
- PImage sprite;
- PVector position, velocity, acceleration;
- float size;
- float topSpeed;
- Agent(PVector pos, PVector vel, PVector acc, float size, float top, PImage img) {
- position = pos;
- velocity = vel;
- acceleration = acc;
- this.size = size;
- topSpeed = top;
- sprite = img;
- }
- Agent(float x, float y) {
- this(new PVector(x, y), new PVector(0.5, 0.0), new PVector(0, 0), 100, 10, null);
- }
- Agent() {
- this(width/2, height/2);
- }
- float getSpeed() {
- return velocity.mag();
- }
- /**change the speed keep the direction*/
- void setSpeed(float speed) {
- velocity.normalize();
- velocity.mult(speed);
- }
- void arrow() {
- stroke(0);
- line(0, 0, 10*velocity.mag(),0);
- ellipse(10*velocity.mag(), 0, 4, 4);
- }
- void display() {
- //Draw things by following these steps
- //1. push matrix
- pushMatrix();
- //2. translate by the x,y position
- translate(position.x, position.y);
- //debug text before rotate
- fill(0);
- text("Angle: "+degrees(getAngle())+"\nSpeed: "+getSpeed(), 80, -20);
- text("Pos: "+position+" ", 80, 40);
- text("Vel: "+velocity+" ", 80, 60);
- text("Acc: "+acceleration+" ", 80, 80);
- //3. rotate by the -angle
- rotate(getAngle());
- //4. draw the shape at the origin.
- //this can be an image, or primitive shapes.
- fill(255);
- rectMode(CENTER);
- rect(0, 0, size, size);
- arrow();
- //5. popmatrix will move the screen back to
- //the original coordinate system taking the
- //drawn object with it!
- popMatrix();
- }
- void setAngle(float a) {
- float mag = velocity.mag();
- float x = cos(a);
- float y = sin(a);
- velocity = new PVector(x, y);
- velocity.mult(mag);
- }
- void turn(float a) {
- setAngle(getAngle()+a);
- }
- float getAngle() {
- return atan2(velocity.y, velocity.x);
- }
- void keepInWindow() {
- position.x = constrain(position.x, 0, width);
- position.y = constrain(position.y, 0, height);
- }
- void update() {
- position.add(velocity);
- velocity.add(acceleration);
- velocity.limit(topSpeed);
- acceleration.mult(0);
- keepInWindow();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement