# Help a newbie create a particle system that follows my player movement

**URL:** <https://discourse.processing.org/t/help-a-newbie-create-a-particle-system-that-follows-my-player-movement/25905>\
**Category:** Coding Questions\
**Created:** [December 2, 2020, 10:43pm UTC](https://discourse.processing.org/t/help-a-newbie-create-a-particle-system-that-follows-my-player-movement/25905 "2020-12-02T22:43:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cal\_codes](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cal\_codes](https://discourse.processing.org/u/Cal_codes)\
**Post date:** [December 2, 2020, 10:43pm UTC](https://discourse.processing.org/t/help-a-newbie-create-a-particle-system-that-follows-my-player-movement/25905/1 "2020-12-02T22:43:32Z")

</div>

Hey guys, I am making a geometrical game for some fun right now and have been pondering for a while how to make sure my “users” would be able to see the direction they are moving in. Right now I have a line that shows the direction, but I don’t think this will work long term. My idea is to create a particle system that trails behind the playable character that gives an indication of where the player is moving, but I’ve been having a lot of trouble trying to make this work.

If anyone can help me do this, it would be great!

This is all my code right now:

(Also if anyone can help me with the post formatting please do!!)

//variables  
final int KEY\_LIMIT = 1024;  
boolean[] keysPressed = new boolean[KEY\_LIMIT];  
final float DIAGONAL\_FACTOR = (0.5 \* sqrt(2));  
final int PLAYER\_SPEED = 4;  
final int PLAYER\_DIAM = 50;  
final color PLAYER\_COLOUR = 0;  
final float LAZER\_DIRECTION = 0;  
final float LAZER\_LENGTH = 300;  
final float LAZER\_WIDTH = 8;  
final color LAZER\_COLOUR = #FF6200;  
final color BULLET\_COLOUR = #0700F7;  
final int PLAYER\_STROKE\_WEIGHT = 4;  
final int WALL\_START\_X = 0;  
final int WALL\_START\_Y = 0;  
final float ARCADE\_HEIGHT = 80;  
final float ARCADE\_WIDTH = 1400;  
final float ARCADE\_COLOUR = #434343;  
final float ARCADE\_X = 0;  
final float ARCADE\_Y = 0;  
final float ARCADE\_X\_BOTTOM = 0;  
final float ARCADE\_Y\_BOTTOM = 900 - ARCADE\_HEIGHT;  
final float GAME\_NAME\_X = 500;  
final float GAME\_NAME\_Y = 60;  
final float SCORE\_TEXT\_X = 50;  
final float SCORE\_TEXT\_Y = 880;  
final color SCORE\_COLOUR = 255;  
final float BULLET\_SPEED\_MULTIPLIER = 10;  
final float BULLET\_DIAM = 12;  
float lookingDirectionY;  
float lookingDirectionX;  
int lastFired = millis();

PFont arcadeFont;

ArrayList bullets;

Player player;  
Background bg;  
ArcadeMachine machine;

void setup() {  
size(1400, 900);  
player = new Player(width/2, height/2, PLAYER\_DIAM);  
bg = new Background();  
lazer = new Lazer(player.xPos, player.yPos, lookingDirectionX, lookingDirectionY);  
machine = new ArcadeMachine();  
bullets = new ArrayList();  
arcadeFont = createFont(“arcade.TTF”, 60);  
bg.setLines();  
}

void draw() {  
background(0);  
bg.display();  
displayBullet();  
player.display();  
player.move();  
player.collision();  
machine.display();  
}

void keyPressed() {  
if (keyCode \>= KEY\_LIMIT) return; //if keycode exceeds limit, exit function (‘return’).  
keysPressed[keyCode] = true; // sets its boolean to true  
}

void keyReleased() {  
if (keyCode \>= KEY\_LIMIT) return;  
keysPressed[keyCode] = false;  
}

class Player {

float xPos, yPos, diameter, xVelocity, yVelocity;

Player(float xPos, float yPos, float diameter) {  
this.xPos = xPos;  
this.yPos = yPos;  
this.diameter = diameter;  
}

void display() {  
fill(PLAYER\_COLOUR);  
stroke(#9EFF0F);  
strokeWeight(PLAYER\_STROKE\_WEIGHT);  
circle(xPos, yPos, diameter);  
stroke(255);  
}

void move() {  
if (keysPressed[UP] && !keysPressed[RIGHT] && !keysPressed[LEFT] && !keysPressed[DOWN]) {  
yVelocity = -PLAYER\_SPEED;  
lookingDirectionY = -1;  
lookingDirectionX = 0;  
line(xPos, yPos, xPos, yPos - diameter/2);  
}  
if (keysPressed[DOWN] && !keysPressed[RIGHT] && !keysPressed[LEFT] && !keysPressed[UP]) {  
yVelocity = PLAYER\_SPEED;  
lookingDirectionY = 1;  
lookingDirectionX = 0;  
line(xPos, yPos, xPos, yPos + diameter/2);  
}  
if (keysPressed[LEFT] && !keysPressed[UP] && !keysPressed[DOWN] && !keysPressed[RIGHT]) {  
xVelocity = -PLAYER\_SPEED;  
lookingDirectionY = 0;  
lookingDirectionX = -1;  
line(xPos, yPos, xPos - diameter/2, yPos);  
}  
if (keysPressed[RIGHT] && !keysPressed[UP] && !keysPressed[DOWN] && !keysPressed[LEFT]) {  
xVelocity = PLAYER\_SPEED;  
lookingDirectionY = 0;  
lookingDirectionX = 1;  
line(xPos, yPos, xPos + diameter/2, yPos);  
}  
if (keysPressed[UP] && keysPressed[RIGHT] && !keysPressed[DOWN] && !keysPressed[LEFT]) {  
xVelocity = PLAYER\_SPEED \* DIAGONAL\_FACTOR;  
yVelocity = -PLAYER\_SPEED \* DIAGONAL\_FACTOR;  
lookingDirectionY = -1 \* DIAGONAL\_FACTOR;  
lookingDirectionX = 1 \* DIAGONAL\_FACTOR;  
line(xPos, yPos, xPos + diameter/2 \* DIAGONAL\_FACTOR, yPos - diameter/2 \* DIAGONAL\_FACTOR);  
}  
if (keysPressed[UP] && keysPressed[LEFT] && !keysPressed[DOWN] && !keysPressed[RIGHT]) {  
xVelocity = -PLAYER\_SPEED \* DIAGONAL\_FACTOR;  
yVelocity = -PLAYER\_SPEED \* DIAGONAL\_FACTOR;  
lookingDirectionY = -1 \* DIAGONAL\_FACTOR;  
lookingDirectionX = -1 \* DIAGONAL\_FACTOR;  
line(xPos, yPos, xPos - diameter/2 \* DIAGONAL\_FACTOR, yPos - diameter/2 \* DIAGONAL\_FACTOR);  
}  
if (keysPressed[DOWN] && keysPressed[RIGHT] && !keysPressed[LEFT] && !keysPressed[UP]) {  
xVelocity = PLAYER\_SPEED \* DIAGONAL\_FACTOR;  
yVelocity = PLAYER\_SPEED \* DIAGONAL\_FACTOR;  
lookingDirectionY = 1 \* DIAGONAL\_FACTOR;  
lookingDirectionX = 1 \* DIAGONAL\_FACTOR;  
line(xPos, yPos, xPos + diameter/2 \* DIAGONAL\_FACTOR, yPos + diameter/2 \* DIAGONAL\_FACTOR);  
}  
if (keysPressed[DOWN] && keysPressed[LEFT] && !keysPressed[RIGHT] && !keysPressed[UP]) {  
xVelocity = -PLAYER\_SPEED \* DIAGONAL\_FACTOR;  
yVelocity = PLAYER\_SPEED \* DIAGONAL\_FACTOR;  
lookingDirectionY = 1 \* DIAGONAL\_FACTOR;  
lookingDirectionX = -1 \* DIAGONAL\_FACTOR;  
line(xPos, yPos, xPos - diameter/2 \* DIAGONAL\_FACTOR, yPos + diameter/2 \* DIAGONAL\_FACTOR);  
}  
xPos += xVelocity;  
yPos += yVelocity;

```
// Adds new bullet to ArrayList when "S" or "s" is pressed - ascii table nummbers
if (keysPressed[83] || keysPressed[115]) {
  if (millis() > lastFired + 150)
  {
    bullets.add(new Bullet(xPos, yPos, lookingDirectionX * BULLET_SPEED_MULTIPLIER, lookingDirectionY * BULLET_SPEED_MULTIPLIER));
    lastFired = millis();
  }
}

//resets the x and the y Velocity 
xVelocity = 0;
yVelocity = 0;

//pushMatrix();
//popMatrix();

```

}

void collision() {  
xPos = constrain(xPos, WALL\_START\_X + diameter/2, width-diameter/2);  
yPos = constrain(yPos, WALL\_START\_Y + ARCADE\_HEIGHT + diameter/2, height-diameter/2 - ARCADE\_HEIGHT);  
}  
}

class ArcadeMachine {

ArcadeMachine() {  
}

void display() {  
fill(ARCADE\_COLOUR);  
rect(ARCADE\_X, ARCADE\_Y, ARCADE\_WIDTH, ARCADE\_HEIGHT);  
rect(ARCADE\_X\_BOTTOM, ARCADE\_Y\_BOTTOM, ARCADE\_WIDTH, ARCADE\_HEIGHT);  
fill(#FEFF00);  
textFont(arcadeFont);  
text(“SHAPE SKIRMISH”, GAME\_NAME\_X, GAME\_NAME\_Y);  
fill(SCORE\_COLOUR);  
text(“SCORE 0000”, SCORE\_TEXT\_X, SCORE\_TEXT\_Y);  
text(“HEALTH” , 1000, SCORE\_TEXT\_Y);  
}  
}

class Bullet {

float xPos, yPos, speed, diameter, xVelocity, yVelocity, colour;

Bullet(float xPos, float yPos, float xVelocity, float yVelocity) {  
this.xPos = xPos;  
this.yPos = yPos;  
this.xVelocity = xVelocity;  
this.yVelocity = yVelocity;  
diameter = BULLET\_DIAM;  
colour = 255;  
}

void display() {  
fill(colour);  
noStroke();  
circle(xPos, yPos, diameter);  
xPos += xVelocity;  
yPos += yVelocity;  
}  
}

void displayBullet() {  
for (int i = 0; i \< bullets.size(); i ++) {  
Bullet aBullet = bullets.get(i);  
aBullet.display();  
// When bullets are beyond screen they are removed  
if ( ( aBullet.xPos \>= width || aBullet.xPos \< 0 ) || ( aBullet.yPos \>= height || aBullet.yPos \< 0 ) ) {  
bullets.remove(i);  
}  
}  
}

class Background {  
int[] lines;  
int[] lines2;  
int multiplyRate = 75;

final int AMOUNT\_OF\_LINES = 30;

Background() {  
}

// Sets the value of every printed line  
void setLines() {  
lines = new int[AMOUNT\_OF\_LINES];  
lines2 = new int[lines.length];

```
for ( int i=0; i < lines.length; i++) {
  lines[i] = height - (i * multiplyRate);
}
for (int i = 0; i < lines2.length; i++) {
  lines2[i] = width - (i * multiplyRate);
}

```

}

// Displays the lines on the background  
void display() {  
for ( int i = 0; i \< lines.length; i++ ) {  
// Give each line a random color each time it’s drawn.  
stroke(#FE00FF);  
strokeWeight(1);  
line( i - 10, lines[i], (i) + width, lines[i] );  
}  
for ( int i = 0; i \< lines2.length; i++ ) {  
// Give each line a random color each time it’s drawn.  
stroke(#1CE9FA);  
line(lines2[i], i - 10, lines2[i], (i) + height );  
}  
}  
}
