#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# projectiles

edited June 2014

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;
ypos = new float;
distance = new float;

}
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);
}
}
}
``````
Tagged:

• Note 1: Avoid "magical" numbers, like 126 spread over all your code. Make a constant out of it, allowing to adjust it only at one place.
• Note 2: I don't see the point of having arrays of 126 entries to display only one circle... They all have the same value.

"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:

``````float theta=0, step=0.05;
float x, y,x_,y_;
float timer=0;
float r=2.5;
float[] xpos, ypos;
float[] distance;
float min;
line snake;
float speedX,speedY;
float angle;

void setup() {
size(500, 500);
y=-50;
x=0;
smooth(32);
xpos = new float;
ypos = new float;
distance = new float;
textAlign(CENTER, CENTER);
snake = new line();
snake.initialize();
}

void draw() {
background(255);
translate(width/2, height/2);
fill(0);
snake.display();

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], snake.x, snake.y);
min=min(distance);
}
for (int i=0; i<126; i++)
{
if (dist(xpos[i], ypos[i], snake.x, snake.y)==min)
{
x=xpos[i];
y=ypos[i];
display();
}
}
x_=x;
y_=y;
angle=atan2(snake.x,snake.y);
ellipse(x_+speedX,y_+speedY,10,10);
speedX=4.2*cos(angle);
speedY=4.2*sin(angle);
textAlign(CENTER);
text(degrees(angle),0,0);

}
void display()
{
fill(0);
ellipse(0,0, 100, 100);
fill(255);
ellipse(x, y, 25, 25);
}
``````

snake class: class line {// -----------------------------------// float x=200, y=200; float[] xpos= new float, ypos= new float; float speed=1; float faster=5; int c=10; int direction=0; boolean flagfast=false; //the thing that makes the game actually fun

``````  // *-----------------------------------*//
// most of this you already know
void initialize()
{
for (int i=0; i<100; i++)
{
xpos[i]=0;
ypos[i]=0;
}
}
// *-----------------------------------*//
// most of this you already know
void display()
{
for (int i=0; i<c-1; i++)
{
fill(0);
stroke(0);
ellipse(xpos[i], ypos[i], 10, 10);
}
for (int i=0; i<c; i++)
{
xpos[i]=xpos[i+1];
ypos[i]=ypos[i+1];
}
xpos[c]=x;
ypos[c]=y;
reorient();
direction();
//    collisionDetection();
}
// *-----------------------------------*//
// most of this you already know
void reorient() //level boundries
{
if (x>250) {
x=-250;
} else if (x<-250) {
x=250;
}
if (y>250) {
y=-250;
} else if (y<-250) {
y=250;
}
}
// *-----------------------------------*//
// HEADS UP... NEW SHIT DOWN HERE
void direction() //Prevents snake from reversing direction
{
if (keyCode==UP) {
if (direction != 2) {
direction=1;
}
}
if (keyCode==DOWN) {
if (direction != 1) {
direction=2;
}
}
if (keyCode==RIGHT) {
if (direction != 4) {
direction=3;
}
}
if (keyCode==LEFT) {
if (direction != 3) {
direction=4;
}
}
switch (direction) {
case 1:
y-=int(speed);
break;
case 2:
y+=int(speed);
break;
case 3:
x+=int(speed);
break;
case 4:
x-=int(speed);
break;
}
key();
}
void key()//the golden keyfunction, allows you to get a speed boost until you switch direction
{
if (keyCode==CONTROL)
{
flagfast = !flagfast;
if (flagfast==true) {
switch (direction) {
case 1:
y-=int(faster);
break;
case 2:
y+=int(faster);
break;
case 3:
x+=int(faster);
break;
case 4:
x-=int(faster);
break;
}
}
}
}

//  void collisionDetection() //self collision detection
{
for (int i=0; i<c; i++)
{
if (x==xpos[i] && y==ypos[i]) {
speed=0;
gameOver();
}
}
}
void gameOver()// no explination needed
{
background(100,100,100,50);
textAlign(CENTER);
text("Game over, Press R to restart", 200, 250);
}
}
``````