Colliding a ball into a target

edited June 2017 in Arduino

I want to create a programme that shoots a ball into a target, which will then turn on an LED. I think it is something to do with the float dis as nothing is printed in the debug window. Thanks

//import serial library
import processing.serial.*;
//import arduino library
import cc.arduino.*;

float angle = -PI/4;
PVector location = new PVector(50, 300);
boolean shot = false;
Arduino arduino;
int downPin = 8;
int knobPin = 0;
int Red = 13;
boolean isDEAD;
PVector locationG;
PImage img;
CannonBall ball;

void setup() {
  size(640, 360);
locationG = new PVector(int (random(150,640)),int (random(10,350)));
println(location);
img = loadImage("Red.png");
 arduino = new Arduino(this, Arduino.list()[0], 57600);
 arduino.pinMode(downPin, Arduino.INPUT);
 arduino.pinMode(knobPin, Arduino.INPUT);
 arduino.pinMode(Red, Arduino.OUTPUT);

 img.resize(50,50);
 ball = new CannonBall(location.x, location.y);

}

void draw() {
  float dis = dist(location.x, location.y, locationG.x, locationG.y);
 int knobVal = arduino.analogRead(knobPin);
  background(255); 
  noStroke();
  fill(100,100,100, 80);
   if (mousePressed && shot== false){
    shot = true;
    PVector force = PVector.fromAngle(angle);
    force.mult(10);
    ball.applyForce(force);
  }
   if (shot) {
    PVector gravity = new PVector(0, 0.2);
    ball.applyForce(gravity);
    ball.update();
  }
  ball.display();

  if (ball.location.y > height) {
    ball = new CannonBall(location.x, location.y);  
    shot = false;
  }
  if (isDEAD == false){
  pushMatrix();
  translate(locationG.x, locationG.y);
 image(img,0, 10);
 img.resize(50,50);//draw the target

     if (dis <1)  {
       isDEAD=true;
     }

  popMatrix();
  } else if (isDEAD == true) {

    collide();

  }
  //calculate the angle of the cannon based on mouseY position
  angle= map(knobVal, 0, height, -PI/2, PI/2);

  pushMatrix();
  translate(location.x, location.y);
  rotate(angle);
  ellipse(0, -5, 50, 10);//draw the cannon
  popMatrix();
  }

  void collide(){
    float dis = dist(location.x, location.y, locationG.x, locationG.y);//calculate the distance between ball and target
    println(dis) ;
    if (dis <1)  {
      arduino.digitalWrite(Red, Arduino.HIGH);//light turns on
  } else {
    arduino.digitalWrite(Red, Arduino.LOW);
  }
}

This is the Cannon ball class:

class CannonBall { 
  PVector location;
  PVector velocity;
  PVector acceleration;

  // Size
  float r = 10;

  float topspeed = 10;

  CannonBall(float x, float y) {
    location = new PVector(x,y);
    velocity = new PVector();
    acceleration = new PVector();
  } 

  void update() { 
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
    acceleration.mult(0);
  }

  void applyForce(PVector force) {
    acceleration.add(force);
  }

  void display() { 
    noStroke();
    pushMatrix();
    translate(location.x,location.y);
    ellipse(0,0,r*2,r*2);
    popMatrix();
  }
}

Answers

  • if (dis <1) {

    won't work

    try if (dis <45) {

    or so

    we can't run your version because class CannonBall is missing and arduino.

    make a separate version without arduino and with the class to post here

    Best, Chrisir ;-)

Sign In or Register to comment.