Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Main
- Animal a1;
- void setup() {
- size(500, 400);
- background(0, 0, 0);
- a1 = new Animal(new PVector(3, 6));
- a1.applyGravity(new PVector(0, 9));
- }
- void draw() {
- clear();
- a1.update();
- }
- void mouseClicked() {
- a1.applyVecodity(new PVector(mouseX, mouseY));
- }
- // Animal (class)
- class Animal {
- PVector position;
- PVector speed;
- PVector acceleration;
- // TODO add mass
- void applyVecodity(PVector mouseClickPosition) {
- PVector newVelocity = mouseClickPosition.sub(position);
- speed.x += newVelocity.x;
- speed.y += newVelocity.y;
- }
- void checkBounds() {
- if (position.x > width) {
- speed.x *= -1f;
- position.x = width-1;
- }
- if (position.x < 0) {
- speed.x *= -1f;
- position.x = 0;
- }
- if (position.y > height) {
- speed.y *= -1f;
- position.y = height-1;
- }
- if (position.y < 0) {
- speed.y *= -1f;
- position.y = 0;
- }
- }
- void applyGravity(PVector g) {
- acceleration.x = g.x;
- acceleration.y = g.y;
- }
- void update() {
- checkBounds();
- speed = speed.add(acceleration);
- position = position.add(speed);
- pushMatrix();
- translate(position.x, position.y);
- stroke(255, 0, 0);
- ellipse(0, 0, 30, 30);
- popMatrix();
- }
- Animal(PVector initialSpeed) {
- speed = new PVector();
- acceleration = new PVector();
- position = new PVector();
- speed.x = initialSpeed.x;
- speed.y = initialSpeed.y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement