We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Question: Here's the code for a small ball that follows the mouse while bound to a small circle in the center. what i'd like to is every time i press a button on the mouse, a projectile is produced (a small ellipse for example) that is directed at the mouse's location. I'm kinda lost as to how this can be done. should i look into PVectors or is there some other. simpler alternative. Thank you.
code:
float theta=0, step=0.05;
float x, y;
float r=2.5;
float[] xpos, ypos;
float[] distance;
float min;
void setup()
{
size(500, 500);
y=height/2-50;
x=250;
smooth(32);
xpos = new float[126];
ypos = new float[126];
distance = new float[126];
}
void draw()
{
background(100);
if (theta<2*PI)
{
for (int i=0; i<126; i++)
{
x+=r*cos(theta);
y+=r*sin(theta);
theta+=step;
xpos[i]=x;
ypos[i]=y;
}
}
for (int i=0; i<126; i++)
{
distance[i]=dist(xpos[i], ypos[i], mouseX, mouseY);
min=min(distance);
}
for (int i=0; i<126; i++)
{
if (dist(xpos[i], ypos[i], mouseX, mouseY)==min)
{
x=xpos[i];
y=ypos[i];
fill(0);
ellipse(250, height/2, 100, 100);
fill(255);
ellipse(x, y, 25, 25);
}
}
}
Answers
Dunno much. Perhaps something like this:
http://studio.processingtogether.com/sp/pad/export/ro.91nVQMnL$v06L/latest
or this 1:
http://studio.processingtogether.com/sp/pad/export/ro.91kLmk61vAOZp/latest
"a projectile is produced (a small ellipse for example) that is directed at the mouse's location"
I fail to see the connection with the current code... And the requirements are fuzzy. Where the projectile comes from? Should it follow mouse or still aim at original location? What does it do once it reaches the destination?
"should i look into PVectors or is there some other. simpler alternative"
PVector can be a good idea. There is rarely "simpler alternatives" in Processing... :-)
here
http://openprocessing.org/sketch/77863
it might be more complicate than you think
because: you can see many bullets fly at the same time and they all have a different position and a different direction. Thus we need to store all individually in arrays. I used OOP here, see #6 of the tutorials please.
When you shoot, a new bullet comes to life but the others still fly.
When a bullet hits something, bullet dies.
In the sketch I've shown, a few tabs / files are used. Each has some parts of the code. See menu code there. Download it and read it.
;-)
Phil.ho The array is used to record all the positions of the circle after one rotation since the circle will have a finite number of positions from the begging of the rotation (at theta =0) and at the end of the rotation (at theta = 2PI). I got the 125 from making a float list and adding to it every value of X and Y what i did was let the animation run and added a counter, from 0 until 2PI all the X and Y values of the circle were .appended to the float list(i had two for each coordinate by the way) i assumed that i at the end was the number of positions (the number of times X and Y have been calculated) so i made a normal array with a size of 126 (125+ the 0 position).... Most of this wasn't planned by the way :D
What exactly do i want? I want a small sphere (bullet) to emerge from the rotating sphere (the one that follows the mouse) and for it to move in a straight line towards the location of the mouse (if the mouse moves the direction of motion should remain unchanged). This event will be controlled by a timer.
here's the code i came up with: bear in mind that i only want one bullet flying at time, so i can just re position the same bullet when i want. the problem is..... nothing happens :D. the bullet just doesn't move. Ignore the comments in the snake class, they were meant for a friend of mine. Main:
snake class: class line {// -----------------------------------// float x=200, y=200; float[] xpos= new float[100], ypos= new float[100]; float speed=1; float faster=5; int c=10; int direction=0; boolean flagfast=false; //the thing that makes the game actually fun