How to 'shoot' points and 'rotate' wheels?

edited April 2014 in Questions about Code

Hi I'm very new to processing and unfortunately I am very ambitious. I've made this ridiculous mouse/animal thing and would really like to know (if possible!) how to make the wheels spin and/or if(mousePressed)... make multiple dots 'shoot' out of the animal's nose and bounce off the walls. I know this sounds fairly complicated but I would really like to know how to do this with detail! The code is here and you can add/edit it if you want:

void setup() {
  size(720,480);
}


void draw() {
   background(68,80,110);

   stroke(255);
  fill(185,11,11);
  rect(-7, 380, 727, 170);

  stroke(0);
  fill(0);
  ellipse(35,460,180,70);

  stroke(255);
  fill(128);
 ellipse(mouseX-55, mouseY+26, 100, 48);//body

  stroke(255);
  fill(128);
  ellipse(mouseX, mouseY, 50, 45);//head

  stroke(255);
  fill(128);
 triangle(mouseX, mouseY, mouseX+5, mouseY+5, mouseX+40, mouseY);//nose


 stroke(0);
 fill(0);
 ellipse(mouseX-3, mouseY-7, 5, 3);//left eye


 stroke(0);
 fill(0);
 ellipse(mouseX+8, mouseY-7, 5, 3);//right eye

 stroke(255);
  fill(128);
 triangle(mouseX-95, mouseY+12, mouseX-90, mouseY+8, mouseX-110, mouseY-20);//tail

 stroke(255);
  fill(128);
 triangle(mouseX-13, mouseY-20, mouseX-20, mouseY-14, mouseX-30, mouseY-35);//left ear

 stroke(255);
  fill(128);
 triangle(mouseX+8, mouseY-22, mouseX+16, mouseY-19, mouseX+23, mouseY-33);//right ear

  stroke(255);
  fill(128);
 ellipse(mouseX-30, mouseY+48, 25, 25);//wheel right

  stroke(255);
  fill(128);
 ellipse(mouseX-80, mouseY+48, 25, 25);//wheel left

   if(mousePressed); {
    stroke(0);
 fill(231,100,20);
 ellipse(mouseX+40, mouseY, 5, 5);//nosepoint



  }

}

Thanks in advance! tephy2

Answers

  • for shoot: there are a lot of examples around in this forum - just search shoot

    for rotate wheels - look at rotate in the reference pls

  • How to post code

    when you post your code:

    • in the editor hit ctrl-t to auto format

    • copy it

    • paste it in the browser

    • leave 2 empty lines before it

    • mark the code (without the 2 empty lines)

    • press C in the small command bar.

  • edited April 2014

    this is the idea...

    int positionY ;
    float angleWheel; 
    float  bulletVel = 1.4;
    PVector positionForBulletStart ;
    
    void setup() { 
      size(720, 480);
      positionY = height-161;
      noCursor(); // hide mouse cursor
    }
    
    void draw() { 
      background(68, 80, 110);
    
      // positionY=mouseY;
    
      positionForBulletStart = new PVector(mouseX+37, positionY);
    
      stroke(255); 
      fill(185, 11, 11); 
      rect(-7, 380, 727, 170);
    
      stroke(0); 
      fill(0); 
      ellipse(35, 460, 180, 70); // spot corner 
    
      stroke(255); 
      fill(128); 
      ellipse(mouseX-55, positionY+26, 100, 48);//body
    
      stroke(255); 
      fill(128); 
      ellipse(mouseX, positionY, 50, 45);//head
    
      stroke(255); 
      fill(128); 
      triangle(mouseX, positionY, mouseX+5, positionY+5, mouseX+40, positionY);//nose
    
      stroke(0); 
      fill(0); 
      ellipse(mouseX-3, positionY-7, 5, 3);//left eye
    
      stroke(0); 
      fill(0); 
      ellipse(mouseX+8, positionY-7, 5, 3);//right eye
    
      stroke(255); 
      fill(128); 
      triangle(mouseX-95, positionY+12, mouseX-90, positionY+8, mouseX-110, positionY-20);//tail
    
      stroke(255); 
      fill(128); 
      triangle(mouseX-13, positionY-20, mouseX-20, positionY-14, mouseX-30, positionY-35);//left ear
    
      stroke(255); 
      fill(128); 
      triangle(mouseX+8, positionY-22, mouseX+16, positionY-19, mouseX+23, positionY-33);//right ear
    
      //  stroke(255); 
      //  fill(128); 
      //  ellipse(mouseX-30, positionY+48, 25, 25);//wheel right
    
      wheel(mouseX-30, positionY+48);
    
      //  stroke(255); 
      //  fill(128); 
      //  ellipse(mouseX-80, positionY+48, 25, 25);//wheel left
    
      wheel(mouseX-80, positionY+48);
    
      angleWheel++;
      shoot();
      handleBullets();
    
      if (mousePressed) 
      { 
        stroke(0); 
        fill(231, 100, 20); 
        ellipse(mouseX+40, positionY, 5, 5); //nosepoint
      }
      else
      {
        // do nothing
      }
    }
    //
    void wheel( float posX, float posY ) {
      stroke(255); 
      fill(128); 
      pushMatrix();
      //translate (mouseX-30, positionY+48);
      translate (posX, posY);
      rotate(radians(angleWheel));
    
      ellipse(0, 0, 25, 25);//wheel right
      //line(0, 0, 0, 0-25/2);
      //line(0, 0, 0, 0+25/2);
      line(0, 0-25/2, 0, 0+25/2);
    
      //line(0, 0, 0-25/2, 0);
      //line(0, 0, 0+25/2, 0);
      line( 0-25/2, 0, 0+25/2, 0);
    
      rotate(radians(45));
      line( 0-25/2, 0, 0+25/2, 0);
    
      rotate(radians(90));
      line( 0-25/2, 0, 0+25/2, 0);
    
      popMatrix();
    }
    //
    /**
     * Multiple Bullets (v2.5)
     * by Ammon.Owed (2013/Jan)
     *
    
     from <a href="http://forum.processing.org/one/topic/shoot-multiple-bullets" target="_blank" rel="nofollow">http://forum.processing.org/one/topic/shoot-multiple-bullets</a>;
         **/
    
    ArrayList<Bullet> bullets = new ArrayList();  // class Bullet
    
    // other functions -----------------------------------------
    
    void handleBullets() {
      //for (Bullet b: bullets)  b.run(); //unremovable bullets!
      for ( int b = bullets.size(); b != 0; )
        if ( bullets.get(--b).run() ) 
          bullets.remove(b);
      print(bullets.size() + "\t");
    }
    
    // ==============================
    
    void shoot() {
      final byte bulletGap = 3;
      //  if ((mousePressed && frameCount % bulletGap == 0) ||
      //    (keyPressed && key ==' ' && frameCount % bulletGap == 0))
      if ((keyPressed && key ==' ' && frameCount % bulletGap == 0))
      {
        addBullet();
      }
    }
    
    void addBullet() {
      final PVector bulletSpd = new PVector();
      bulletSpd.set(3, 0);
      //bulletSpd.sub(position);
      bulletSpd.normalize();
      bulletSpd.mult(bulletVel);
      bullets.add( new Bullet(positionForBulletStart, bulletSpd) );
    }
    
    // ====================================
    
    class Bullet extends PVector {
    
      PVector vel;
      final static color bulletColor = #0000FF;
    
      Bullet(PVector loc, PVector vel) {
        super(loc.x, loc.y);
        this.vel = vel.get();
      }
    
      void display() {
        final byte bulletSize = 4;
        fill(bulletColor);
        ellipse(x, y, bulletSize, bulletSize);
      }
    
      boolean update() {
        add(vel);
        return x > width || x < 0 || y > height || y < 0;
      }
    
      boolean run() {
        display();
        return update();
      }
      //
    } // class Bullet
    
    // ========================
    
  • edited April 2014

    explanation

    code for shoot by Ammon.Owed

    shoot with space key

    I use noCursor() to hide mouse cursor

    mouse nose not red ?

    there was a small issue with your mouse nose:

    if(mousePressed); 
    
    { stroke(0);
    

    Problem: you mustn't have a ; after

    if(mousePressed) 
    

    because then all in the brackets { } is separated from the if-clause and thus executed always.

    (the execute part (what is executed / done when condition is true) of the if-clause is only between if(......) and the ; so this part is empty. That's why you need to delete the ; so the execute part of the if-clause is all between { and } - the stroke and ellipse etc. )

    This works now.

    wheels

    for the wheels I invented a function that gets the wheel position so we can use the same function for both wheels with different positions. Good.

    unfortunately they always rotate and at the same speed. Bad.

    shooting

    for shooting I used a class - see OOP in the tutorials (object oriented programming)

    Greetings, Chrisir ;-)

  • I also made the mouse roll on the floor, so mouseY is not in use anymore

    • see line 15
Sign In or Register to comment.