Move a triangle to point while pointing at it

edited August 2015 in Questions about Code

Hello,

I have tried to make a programm where a red cirlce (the leader as I call it) moves to the mouse position. Additionally to that several small traingles are supposed to follow the leader to a certain point next to the leader, which functions as I wanted but I tried to make the triangles point in the direction where they are heading. But i didnt manage to achieve that so i wanted to ask if someone can help me with that. Here is the follower class:

    class Follower
    {

      PVector followerSupposedPos = new PVector();            //Position of the follower which it should reach
      PVector followerPos         = new PVector();            // actual position of the follower
      PVector distance            = new PVector();            //distance (magnitude will be changed) between the followerPos and  followerSupposedPos
      PVector preFollower         = new PVector();            //position of the follower next to it(left)
      PVector leaderPos           = new PVector();            // position of the leader
      PVector realDistance        = new PVector();            //the real distance between the followerPos and  followerSupposedPos

      boolean numberOne;

      float speed;

      float angle;

      Follower(float speed_)
      {
        speed = speed_;
      }

      void run(PVector leaderPos_, boolean numberOne_, PVector preFollower_)
      {
        preFollower = preFollower_;
        numberOne   = numberOne_;
        leaderPos   = leaderPos_;

        if (numberOne_ == true)
        {
          followerSupposedPos.x = leaderPos.x - 100;
          followerSupposedPos.y = leaderPos.y + 20;
          fill(200, 100, 50);
        } else if (numberOne_ == false)
        {
          followerSupposedPos.x = preFollower.x + 13;
          followerSupposedPos.y = preFollower.y;
        }

        //points(); //the direction the follower points

        display();

        move();

      }


      PVector impart()  // needed for the preFollower position
      {
        return followerSupposedPos;
      }

      void display()
      {      
        fill(255, 0, 0, 100);
        ellipse(followerPos.x, followerPos.y, 5, 5);
        fill(200, 150, 10);
        triangle(followerPos.x, followerPos.y, followerPos.x - 5, 
        followerPos.y + 10, followerPos.x+ 5, followerPos.y + 10);
      }

      void move()
      {
        PVector distancePrint = new PVector(distance.x * 1000, distance.y * 1000);

        realDistance.x = followerSupposedPos.x - followerPos.x;
        realDistance.y = followerSupposedPos.y - followerPos.y;

        distance.x = realDistance.x * 0.007;
        distance.y = realDistance.y * 0.007;


        if (realDistance.mag() >= 4)
        {
          distance.setMag(5);

          followerPos.x += distance.x + speed;
          followerPos.y += distance.y + speed;
        } else
        {
          distance.setMag(0);
        }

        println(realDistance.mag());
      }

      void points()
      {
        angle = atan2(realDistance.y, realDistance.x);
      }

    }

and a programm where I managed to make a triangle point at the mouse without moving (didnt work in the programm above) :

float angle;
PVector d = new PVector();


void draw()
{  
  background(0);

  translate(width/2, height/2);

  d.x = mouseX - width/2;
  d.y = mouseY - height/2;

  angle = atan2(d.y, d.x);



  triangle_(angle);
}

void triangle_(float ang)
{
  pushMatrix();

  rotate(ang);
  triangle(0, 0, -50, -20, - 50, 20);
  popMatrix();
}

does somebody know how I can connect these two programms or a another way to achieve what i what?

Answers

Sign In or Register to comment.