Processing. Particle sytem - how to make particles come in one by one - java

I am trying to make my particle system generate particles one by one, rather than all at the same time. My code currently will generate all 100 particles instantly.
I have not tried much as I am new to coding.
I have a setup where I call and updated my particle class, and a class that has all my parameters of the particle system.
int num = 100;
Particle[] p = new Particle[num];
void setup() {
size(1080, 720);
colorMode(HSB);
for (int i = 0; i < num; i ++) {
p[i] = new Particle(new PVector(random(width), random(height)), 100, 150);
}
stroke(255);
}
void draw() {
background(0);
for (int i = 0; i < num; i ++) {
p[i].update(p, i);
}
}
class Particle {
PVector pos;
PVector vel;
float r, mr;
float spd = 0.1;
float max = 2;
Particle(PVector pos, float r, float mr) {
this.pos = pos;
this.r = r;
this.mr = mr;
vel = new PVector(random(-1, 1), random(-1, 1));
}
void update(Particle[] p, int i) {
float h = map(mouseX, 0, width, 0, 255);
pos.add(vel);
if (pos.x < -10) pos.x = width;
if (pos.x > width + 10) pos.x = 0;
if (pos.y < -10) pos.y = height;
if (pos.y > height + 10) pos.y = 0;
vel.x = constrain(vel.x + random(-spd, spd), -max, max);
vel.y = constrain(vel.y + random(-spd, spd), -max, max);
for (int j = i + 1; j < p.length; j ++) {
float ang = atan2(pos.y - p[j].pos.y, pos.x - p[j].pos.x);
float dist = pos.dist(p[j].pos);
if (dist < r) {
stroke(h, 255, map(dist, 0, r, 255, 0));
strokeWeight(map(dist, 0, r, 3, 0));
line(pos.x, pos.y, p[j].pos.x, p[j].pos.y);
float force = map(dist, 0, r, 4, 0);
vel.x += force * cos(ang);
vel.y += force * sin(ang);
}
}
float ang = atan2(pos.y - mouseY, pos.x - mouseX);
float dist = pos.dist(new PVector(mouseX, mouseY));
if (dist < r) {
stroke(0, 0, map(dist, 0, r, 255, 0));
strokeWeight(map(dist, 0, r, 3, 0));
line(pos.x, pos.y, mouseX, mouseY);
float force = map(dist, 0, r, 30, 0);
vel.x += force * cos(ang);
vel.y += force * sin(ang);
}
noStroke();
fill(h, 255, 255);
ellipse(pos.x, pos.y, 5, 5);
}
}

Create an ArrayList of particles, but don't add any particle in setup():
ArrayList<Particle> paticles = new ArrayList<Particle>();
void setup() {
size(400, 400);
colorMode(HSB);
stroke(255);
}
Consecutively add the particles in draw(). The function millis() is used to get the time since the program was started:
void draw() {
int num = 100;
int interval = 100; // 0.5 seconds
int time = millis(); // milliseconds since starting the program
if (paticles.size() < num && paticles.size()*interval+5000 < time) {
paticles.add(new Particle(new PVector(random(width), random(height)), 100, 150));
}
background(0);
for (int i = 0; i < paticles.size(); i ++) {
Particle p = paticles.get(i);
p.update(paticles, i);
}
}
Note, the class Particle has to be adapted, because it has to operate with the ArrayList of variable length rather than the array with fixed length:
class Particle {
PVector pos;
PVector vel;
float r, mr;
float spd = 0.1;
float max = 2;
Particle(PVector pos, float r, float mr) {
this.pos = pos;
this.r = r;
this.mr = mr;
vel = new PVector(random(-1, 1), random(-1, 1));
}
void update(ArrayList<Particle> paticles, int i) {
float h = map(mouseX, 0, width, 0, 255);
pos.add(vel);
if (pos.x < -10) pos.x = width;
if (pos.x > width + 10) pos.x = 0;
if (pos.y < -10) pos.y = height;
if (pos.y > height + 10) pos.y = 0;
vel.x = constrain(vel.x + random(-spd, spd), -max, max);
vel.y = constrain(vel.y + random(-spd, spd), -max, max);
for (int j = i + 1; j < paticles.size(); j ++) {
Particle pj = paticles.get(j);
float ang = atan2(pos.y - pj.pos.y, pos.x - pj.pos.x);
float dist = pos.dist(pj.pos);
if (dist < r) {
stroke(h, 255, map(dist, 0, r, 255, 0));
strokeWeight(map(dist, 0, r, 3, 0));
line(pos.x, pos.y, pj.pos.x, pj.pos.y);
float force = map(dist, 0, r, 4, 0);
vel.x += force * cos(ang);
vel.y += force * sin(ang);
}
}
float ang = atan2(pos.y - mouseY, pos.x - mouseX);
float dist = pos.dist(new PVector(mouseX, mouseY));
if (dist < r) {
stroke(0, 0, map(dist, 0, r, 255, 0));
strokeWeight(map(dist, 0, r, 3, 0));
line(pos.x, pos.y, mouseX, mouseY);
float force = map(dist, 0, r, 30, 0);
vel.x += force * cos(ang);
vel.y += force * sin(ang);
}
noStroke();
fill(h, 255, 255);
ellipse(pos.x, pos.y, 5, 5);
}
}

Related

Why do these rectangles sometimes show that they are colliding even though they are not?

When I run the code it generates 16 rectangles with a random size, random position, and a random color. It is then supposed to turn white if it is colliding with another rectangle. Most of the time it works fine but every so often rectangles turn white when they are not colliding with anything.
Main
int boxCount = 16;
Box[] boxes = new Box[boxCount];
void setup(){
size(500, 500);
for(int i = 0; i < boxCount; i++){
boxes[i] = new Box(random(50, width - 50), random(50, height - 50), random(20, 50), random(20, 50), color(random(0, 255), random(0, 255), random(0, 255)));
}
}
void draw(){
for(int i = 0; i < boxCount; i++){
boxes[i].create();
for(int x = 0; x < boxCount; x++){
if(boxes[i] != boxes[x]){
boxes[i].collide(boxes[x]);
}
}
}
}
Class
class Box{
float x;
float y;
float w;
float h;
color c;
Box(float _x, float _y, float _w, float _h, color _c){
x = _x;
y = _y;
w = _w;
h = _h;
c = _c;
}
void create(){
fill(c);
rect(x, y, w, h);
}
void collide(Box o){
float right = x + (w / 2);
float left = x - (w / 2);
float top = y - (h / 2);
float bottom = y + (h / 2);
float oRight = o.x + (o.w / 2);
float oLeft = o.x - (o.w / 2);
float oTop = o.y - (o.h / 2);
float oBottom = o.y + (o.h / 2);
if(right > oLeft && left < oRight && bottom > oTop && top < oBottom){
c = color(255, 255, 255);
}
}
}
rect doesn't draw a rectangle around center point, by default the rectangle is drawn at a top left position (x, y) with a size (with, height).
You've 2 possibilities to solve the issue:
Either change the collision detection method:
class Box{
// [...]
void collide(Box o){
if(x < o.x+o.w && o.x < x+w && y < o.y+o.h && o.y < y+h){
c = color(255, 255, 255);
}
}
}
Or set the CENTER rectMode(), which will cause the rectangle to be drawn as you expect it:
class Box{
// [...]
void create(){
fill(c);
rectMode(CENTER);
rect(x, y, w, h);
}
// [...]
}

Dynamic drawing on canvas taking too long

I wanted to draw a grid of quads with an animated colour property but ended up with code that is too slow just to create a basic structure of an image, not to mention to animate it. Also, the image is drawing, not as it finished to compute, but in the process by parts. How can I fix it? Here's onDraw(Canvas canvas) method:
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
final int height = canvas.getHeight();
final int width = canvas.getWidth();
Bitmap.Config conf = Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(width, height, conf);
new Thread(() -> {
int cells_amount = 0;
float cells_vertical = 0;
float cells_horizontal = 0;
cells_vertical = (float) height / CELL_SIZE;
int y = 0;
if (cells_vertical % 1 > 0) {
y = Math.round(CELL_SIZE * -(cells_vertical % 1));
cells_vertical = (int) (cells_vertical + 1);
}
cells_horizontal = (float) width / CELL_SIZE;
int x = 0;
if (cells_horizontal % 1 > 0) {
x = Math.round(CELL_SIZE * -(cells_horizontal % 1));
cells_horizontal = (int) (cells_horizontal + 1);
}
cells_amount = (int) (cells_horizontal * cells_vertical);
Canvas c = new Canvas(bitmap);
c.drawColor(Color.BLACK);
int x_preserved = x;
Paint textPaint = new Paint();
textPaint.setColor(Color.parseColor("#EEEEEE"));
for (int i = 0; i < cells_amount; i++) {
Rect rect = new Rect(x, y, x + CELL_SIZE, y + CELL_SIZE);
Paint framePaint = new Paint();
framePaint.setColor(Color.parseColor("#1F1F1F"));
framePaint.setStyle(Paint.Style.STROKE);
framePaint.setStrokeWidth(1);
c.drawRect(rect, framePaint);
if (random.nextBoolean()) {
String output = String.format(Locale.getDefault(), "%d", "+");
int cx = (int) (x + (CELL_SIZE / 2) - (textPaint.measureText(output) / 2));
int cy = (int) ((y + (CELL_SIZE / 2)) - ((textPaint.descent() + textPaint.ascent()) / 2));
c.drawText(output, cx, cy, textPaint);
}
if (i % cells_horizontal == 0 && i >= cells_horizontal) {
y += CELL_SIZE;
x = x_preserved;
} else {
x += CELL_SIZE;
}
}
}).start();
canvas.drawBitmap(bitmap, 0, 0, null);
}

using fft in processing

I was wondering if someone could help me. I am currently working on a project using processing to use FFT for an aesthetics project. What I wanna do is allow the user to import a song, and have the fft act as a visualizer to react and display different colors based on the song. The problem I am having is I can not get the fft itself to work. How can I get processing to recognize the fft?
Here is the fft code I'm using.
fft = new FFT(song.bufferSize(), song.sampleRate());
//DRAW FFT
fft.forward(song.mix);
colorMode(HSB, 255);
void wavewave() {
float spread = map(450, 0, width, 1, 21.5);
float x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 - fft.getFreq(i) * 4);
}
//map(value, minimum1, maximum1, minimum2, maximum2);
x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 + fft.getFreq(i) * 4);
}
}
I keep getting unidentified token fft, as the error
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
import java.io.File;
import java.io.FilenameFilter;
color waveColor;
int waveIncr = 0;
int counter = 0;
int songCounter = 0;
int fadeLevel = 10;
float buttonX;
float buttonY;
float buttonW;
float buttonH;
Minim minim;
AudioPlayer player;
FFT fft;
ArrayList<Songs> s;
int k;
String filename;
boolean isSelected = false;
void setup() {
s = new ArrayList();
textSize(24);
frame.setResizable(false);
background(255);
size(600, 600);
fill(0);
stroke(0);
noFill();
buttonW = 200;
buttonH = 50;
buttonX = width - width/2 - buttonW/2;
buttonY = height/2 - buttonH/2;
// Minim stuff
minim = new Minim(this);
}
void draw() {
background(255);
fill(0);
rectMode(CORNER);
rect(buttonX, buttonY, buttonW, buttonH);
fill(255);
textAlign(LEFT);
text("Import File", buttonX+35, buttonY+30);
if (isSelected) {
// s.get(k).waveform();
s.get(k).wavewave();
}
}
void mouseClicked() {
if (mouseX>buttonX && mouseX < buttonX+buttonW && mouseY > buttonY && mouseY < buttonY+buttonH) {
selectInput("Import music file", "fileSelected");
}
}
/* Taken from Processing.org */
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or user hit cancel");
}
else {
filename = selection.getAbsolutePath();
s.add(new Songs(player, filename, "Filename"));
isSelected = true;
}
}
// stop minim and the player.
void stop() {
player.close();
minim.stop();
super.stop();
}
class Songs {
AudioPlayer song;
String directory;
String songName;
Songs(AudioPlayer song, String directory, String songName) {
song=minim.loadFile(directory);
this.song=song;
this.songName=songName;
song.play();
}
fft = new FFT(song.bufferSize(), song.sampleRate());
//DRAW FFT
fft.forward(song.mix);
colorMode(HSB, 255);
void wavewave() {
float spread = map(450, 0, width, 1, 21.5);
float x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 - fft.getFreq(i) * 4);
}
//map(value, minimum1, maximum1, minimum2, maximum2);
x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 + fft.getFreq(i) * 4);
}
}
}
You seem to have some syntax errors. Here's your code moved about a bit:
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
import java.io.File;
import java.io.FilenameFilter;
color waveColor;
int waveIncr = 0;
int counter = 0;
int songCounter = 0;
int fadeLevel = 10;
float buttonX;
float buttonY;
float buttonW;
float buttonH;
Minim minim;
AudioPlayer player;
FFT fft;
ArrayList<Songs> s;
int k;
String filename;
boolean isSelected = false;
void setup() {
s = new ArrayList();
textSize(24);
frame.setResizable(false);
background(255);
size(600, 600);
fill(0);
stroke(0);
noFill();
buttonW = 200;
buttonH = 50;
buttonX = width - width/2 - buttonW/2;
buttonY = height/2 - buttonH/2;
// Minim stuff
minim = new Minim(this);
}
void draw() {
background(255);
fill(0);
rectMode(CORNER);
rect(buttonX, buttonY, buttonW, buttonH);
fill(255);
textAlign(LEFT);
text("Import File", buttonX+35, buttonY+30);
if (isSelected) {
// s.get(k).waveform();
s.get(k).wavewave();
}
}
void mouseClicked() {
if (mouseX>buttonX && mouseX < buttonX+buttonW && mouseY > buttonY && mouseY < buttonY+buttonH) {
selectInput("Import music file", "fileSelected");
}
}
/* Taken from Processing.org */
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or user hit cancel");
}
else {
filename = selection.getAbsolutePath();
s.add(new Songs(player, filename, "Filename"));
isSelected = true;
}
}
// stop minim and the player.
void stop() {
player.close();
minim.stop();
super.stop();
}
class Songs {
AudioPlayer song;
String directory;
String songName;
Songs(AudioPlayer song, String directory, String songName) {
song=minim.loadFile(directory);
this.song=song;
this.songName=songName;
song.play();
fft = new FFT(song.bufferSize(), song.sampleRate());
}
void wavewave() {
//DRAW FFT
fft.forward(song.mix);
colorMode(HSB, 255);
float spread = map(450, 0, width, 1, 21.5);
float x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 - fft.getFreq(i) * 4);
}
//map(value, minimum1, maximum1, minimum2, maximum2);
x = 0;
for (int i = 0; i < song.sampleRate() && x < width; i += spread)
{
x = i/spread;
stroke(map(fft.getFreq(i), 0, 256, 0, 360) * 2, //Hue
255, //Saturation
255); //Brightness
line(x, 512, x, 512 + fft.getFreq(i) * 4);
}
}
}
You should try the PDE X mode, might help you with errors in the future, plus it's awesome!

Triangles with texture in processing

I have a code in processing which contains points moving randomly. Those points seek the ball and start triangulate (shaping triangles). I want to display on them a folder of sequences images with opacity. So every time that an triangle is shaped will be textured with an image.
I have tried to use texture and loadimage function but it seems that the problem is that triangles is rendered rapidly with various points and texture can not be seen properly.
import processing.opengl.*;
float r1, r2;
void setup()
{
size(800, 800, OPENGL);
for (int j=0; j<numAgents; j++)
agents[j]=new agent();
for (int i=0; i<numMovers; i++)
movers[i]=new Mover();
smooth();
}
void draw()
{
background(0);
for (int i=0; i<numMovers; i++)
{
movers[i].update();
movers[i].checkEdges();
movers[i].display();
int closestAgentNumber=-1;
for (int j=0; j<numAgents; j++)
{
agents[j].checkEdges();
agents[j].display();
agents[j].update();
agents[j].repel();
float d = dist(agents[j].location.x, agents[j].location.y, movers[i].location.x, movers[i].location.y );
if (d < 100) {
closestAgentNumber=j;
}
if (d<200)
{
agents[j].behaviour=1;
agents[j].follow(movers[i].location.x, movers[i].location.y);
movers[i].hit = true;
}
else if (d>100) {
movers[i].hit = false;
agents[j].behaviour=0;
}
}
}
}
int numAgents= 100;
agent[]agents =new agent[numAgents];
class agent
{
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
float r1, r2;
// boolean connect=false;
int behaviour;
boolean follow=false;
//boolean follow= false;
agent()
{
float speed=800;
if (behaviour==0)
{
location= new PVector(random(0, speed), random(0, speed));
velocity= new PVector(1, 1);
acceleration= new PVector( random(-0.01, 0.01), random(-0.01, 0.01));
acceleration.normalize();
topspeed=5;
}
else if (behaviour==1)
{
// connect=true;
}
// Set to acceleration
}
void follow(float x, float y)
{
if (follow==true)
{
// follow==true;
// Our algorithm for calculating acceleration:
PVector moverPos = new PVector(x, y);
PVector dir = PVector.sub(moverPos, location); // Find vector pointing towards mouse
dir.normalize(); // Normalize
dir.mult(4); // Scale
acceleration = dir;
}
}
void update()
{
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display()
{
pushMatrix();
translate(location.x, location.y);
if (behaviour==1)
{
fill(255, 0, 0);
}
else if ( behaviour==0)
{
fill(255, 255, 255);
}
ellipse(0, 0, 4, 4);
strokeWeight(1);
popMatrix();
}
void checkEdges()
{
if (location.x < 0) {
location.x = 0;
velocity.x *= -1;
acceleration.mult(0.001);
}
if (location.x > 800) {
location.x = 800;
velocity.x *= -1;
acceleration.mult(0.001);
}
if (location.y < 0) {
location.y = 0;
velocity.y *= -1;
acceleration.mult(0.001);
}
if (location.y > 800) {
location.y = 800;
velocity.y *= -1;
acceleration.mult(0.001);
}
}
void repel()
{
for (int i=0; i<numAgents; i++) {
if ((agents[i].behaviour==1))
{
int k;
k=1;
for (int j=0; j<numAgents; j++)
{
//--------------
// float dm = agents[i].location.dist(agents[k].location);
float dm = dist(agents[j].location.x, agents[j].location.y, agents[i].location.x, agents[i].location.y);
if (dm < 50) {
agents[i].velocity.mult(-1);
agents[i].acceleration.mult(0.5);
agents[j].velocity.mult(-1);
agents[j].acceleration.mult(0.5);
}
else if (behaviour==0)
{
agents[i].velocity.mult(1);
agents[j].velocity.mult(1);
}
//float dm = dist(agents[i].location.x, agents[i].location.y, agents[j].location.x, agents[j].location.y);
// float dm = agents[i].location.dist(agents[j].location);
// int clr=(int) map (dm,0,100,200,0);
if (dm<180 && dm>100)
{
if ( k<2)
{
//println(dm);
//stroke(255,0,0);
r1 = agents[j].location.x;
r2 = agents[j].location.y;
k=k+1;
}
else
{
//fill(random(255),random(255),random(255),random(0,20));
//stroke(0,50);
// texture(A);
//fill(random(0,255),random(0,255),random(0,255),random(0, 40));
// stroke(140, 50);
fill(random(0, 255), random(0, 255), random(0, 255), random(0, 10));
stroke(140, 50);
beginShape();
//texture();
// image(A,agents[i].location.x, agents[i].location.y);
vertex(agents[i].location.x, agents[i].location.y);
vertex(r1, r2);
vertex(agents[j].location.x, agents[j].location.y);
endShape(CLOSE);
j=300;
}
}
}
/*void triangulate()
{
fill(random(0, 255), random(0, 255), random(0, 255), random(0, 40));
stroke(140, 50);
for (int i=0; i<numAgents; i++)
{
//gens[i].connect==true)
//if(! agents[i].connect)
if (agents[i].behaviour==1)
{
//println(agents[i]);
int k;
k=1;
for (int j=0; j<numAgents; j++)
{
float dm = dist(agents[i].location.x, agents[i].location.y, agents[j].location.x, agents[j].location.y);
// float dm = agents[i].location.dist(agents[j].location);
// int clr=(int) map (dm,0,100,200,0);
if (dm<180 && dm>100)
{
if ( k<2)
{
//println(dm);
//stroke(255,0,0);
}
}
}
}*/
}
}
}
}
int numMovers= 1;
Mover[]movers= new Mover[numMovers];
class Mover
{
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
boolean hit = false;
Mover()
{
float spead = 800;
location = new PVector(random(0,spead),random(0,spead));
velocity = new PVector(0,0);
acceleration = new PVector(random(-1,1),random(-1,1));//random initial acceleration
topspeed = 4;
}
void update()
{
// Motion 101! Velocity changes by acceleration. Location changes by velocity.
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display()
{
pushMatrix();
translate(location.x,location.y);
//fill(102,0,155,random(120,160));
//fill(32,225,245,127 + sin(frameCount*.01) * 127);
if(hit) fill(255,10,96,120);
else
fill(3,225,190,random(50,127));
ellipse(0,0,30,30);
noStroke();
popMatrix();
}
void checkEdges()
{
if(location.x < 0){
location.x = 0;
velocity.x *= -1;
acceleration.mult(0.001);
}
if(location.x > 800){
location.x = 800;
velocity.x *= -1;
acceleration.mult(0.001);
}
if(location.y < 0){
location.y = 0;
velocity.y *=- 1;
acceleration.mult(0.001);
}
if(location.y > 800){
location.y = 800;
velocity.y *= -1;
acceleration.mult(0.001);
}
}
}
You will need to use 3 Processing functions for this:
tint() - which you'll use for transparency (e.g. tint(255,127); will make the image/texture 50% transparent (127 of 255) )
texture() - this sets texture to a shape(beginShape(),endShape() block). You pass call it after beginShape() and then you pass two additional arguments when calling vertex(), which are your texture coordinates
textureMode() - If you always know the size of your texture image, this is optional, but it's handy to know about this. By default the texture mode is set to IMAGE meaning that for a 100x100 px image that you will map to a 300 units long quad for example, you will map the vertex corner positions with the texture coordinates as in the example bellow.
default texture mode (textureMode(IMAGE))
//assumes tex is already initialized/loaded PImage
beginShape();
texture(tex);
vertex(-150,-150,0, 0, 0);//top-left
vertex( 150,-150,0, 100, 0);//top-right
vertex( 150, 150,0, 100,100);//bottom-right
vertex(-150, 150,0, 0,100);//bottom-left
endShape();
The other option is to use NORMALIZED mode, which instead of actual pixel values, takes normalized values (between 0.0 and 1.0), so the above example in NORMALIZED mode is:
beginShape();
texture(tex);
vertex(-150,-150,0, 0.0,0.0);//top-left
vertex( 150,-150,0, 1.0,0.0);//top-right
vertex( 150, 150,0, 1.0,1.0);//bottom-right
vertex(-150, 150,0, 0.0,1.0);//bottom-left
endShape();
textureMode() is called usually once in setup() and the advantage is you won't have to worry about knowing all the dimensions of all your textures, especially when you use more than one texture in your sketch.
So, back to your code, you need to initialize the texture like so:
tex = loadImage("yourTextureImage.png");
textureMode(NORMALIZED);
and later in repel() function when you draw your shape, you would do this:
beginShape();
tint(255,127);
texture(tex);
vertex(agents[i].location.x, agents[i].location.y,0.0,0.0);//aditional u,v coordinates 0,0 - top left of texture
vertex(r1, r2,1.0,0.0);//top right texture coordinate
vertex(agents[j].location.x, agents[j].location.y,1.0,1.0);//bottom right texture coordinat
endShape(CLOSE);
So the full code would look like this:
import processing.opengl.*;
float r1, r2;
PImage tex;
void setup()
{
size(800, 800, OPENGL);
for (int j=0; j<numAgents; j++)
agents[j]=new agent();
for (int i=0; i<numMovers; i++)
movers[i]=new Mover();
tex = loadImage("yourImage.png");
textureMode(NORMALIZED);
smooth();
}
void draw()
{
background(0);
for (int i=0; i<numMovers; i++)
{
movers[i].update();
movers[i].checkEdges();
movers[i].display();
int closestAgentNumber=-1;
for (int j=0; j<numAgents; j++)
{
agents[j].checkEdges();
agents[j].display();
agents[j].update();
agents[j].repel();
float d = dist(agents[j].location.x, agents[j].location.y, movers[i].location.x, movers[i].location.y );
if (d < 100) {
closestAgentNumber=j;
}
if (d<200)
{
agents[j].behaviour=1;
agents[j].follow(movers[i].location.x, movers[i].location.y);
movers[i].hit = true;
}
else if (d>100) {
movers[i].hit = false;
agents[j].behaviour=0;
}
}
}
}
int numAgents= 100;
agent[]agents =new agent[numAgents];
class agent
{
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
float r1, r2;
// boolean connect=false;
int behaviour;
boolean follow=false;
//boolean follow= false;
agent()
{
float speed=800;
if (behaviour==0)
{
location= new PVector(random(0, speed), random(0, speed));
velocity= new PVector(1, 1);
acceleration= new PVector( random(-0.01, 0.01), random(-0.01, 0.01));
acceleration.normalize();
topspeed=5;
}
else if (behaviour==1)
{
// connect=true;
}
// Set to acceleration
}
void follow(float x, float y)
{
if (follow==true)
{
// follow==true;
// Our algorithm for calculating acceleration:
PVector moverPos = new PVector(x, y);
PVector dir = PVector.sub(moverPos, location); // Find vector pointing towards mouse
dir.normalize(); // Normalize
dir.mult(4); // Scale
acceleration = dir;
}
}
void update()
{
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display()
{
pushMatrix();
translate(location.x, location.y);
if (behaviour==1)
{
fill(255, 0, 0);
}
else if ( behaviour==0)
{
fill(255, 255, 255);
}
ellipse(0, 0, 4, 4);
strokeWeight(1);
popMatrix();
}
void checkEdges()
{
if (location.x < 0) {
location.x = 0;
velocity.x *= -1;
acceleration.mult(0.001);
}
if (location.x > 800) {
location.x = 800;
velocity.x *= -1;
acceleration.mult(0.001);
}
if (location.y < 0) {
location.y = 0;
velocity.y *= -1;
acceleration.mult(0.001);
}
if (location.y > 800) {
location.y = 800;
velocity.y *= -1;
acceleration.mult(0.001);
}
}
void repel()
{
for (int i=0; i<numAgents; i++) {
if ((agents[i].behaviour==1))
{
int k;
k=1;
for (int j=0; j<numAgents; j++)
{
//--------------
// float dm = agents[i].location.dist(agents[k].location);
float dm = dist(agents[j].location.x, agents[j].location.y, agents[i].location.x, agents[i].location.y);
if (dm < 50) {
agents[i].velocity.mult(-1);
agents[i].acceleration.mult(0.5);
agents[j].velocity.mult(-1);
agents[j].acceleration.mult(0.5);
}
else if (behaviour==0)
{
agents[i].velocity.mult(1);
agents[j].velocity.mult(1);
}
//float dm = dist(agents[i].location.x, agents[i].location.y, agents[j].location.x, agents[j].location.y);
// float dm = agents[i].location.dist(agents[j].location);
// int clr=(int) map (dm,0,100,200,0);
if (dm<180 && dm>100)
{
if ( k<2)
{
//println(dm);
//stroke(255,0,0);
r1 = agents[j].location.x;
r2 = agents[j].location.y;
k=k+1;
}
else
{
//fill(random(255),random(255),random(255),random(0,20));
//stroke(0,50);
// texture(A);
//fill(random(0,255),random(0,255),random(0,255),random(0, 40));
// stroke(140, 50);
fill(random(0, 255), random(0, 255), random(0, 255), random(0, 10));
stroke(140, 50);
beginShape();
tint(255,127);
texture(tex);
vertex(agents[i].location.x, agents[i].location.y,0.0,0.0);//aditional u,v coordinates 0,0 - top left of texture
vertex(r1, r2,1.0,0.0);//top right texture coordinate
vertex(agents[j].location.x, agents[j].location.y,1.0,1.0);//bottom right texture coordinat
endShape(CLOSE);
j=300;
}
}
}
/*void triangulate()
{
fill(random(0, 255), random(0, 255), random(0, 255), random(0, 40));
stroke(140, 50);
for (int i=0; i<numAgents; i++)
{
//gens[i].connect==true)
//if(! agents[i].connect)
if (agents[i].behaviour==1)
{
//println(agents[i]);
int k;
k=1;
for (int j=0; j<numAgents; j++)
{
float dm = dist(agents[i].location.x, agents[i].location.y, agents[j].location.x, agents[j].location.y);
// float dm = agents[i].location.dist(agents[j].location);
// int clr=(int) map (dm,0,100,200,0);
if (dm<180 && dm>100)
{
if ( k<2)
{
//println(dm);
//stroke(255,0,0);
}
}
}
}*/
}
}
}
}
int numMovers= 1;
Mover[]movers= new Mover[numMovers];
class Mover
{
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
boolean hit = false;
Mover()
{
float spead = 800;
location = new PVector(random(0,spead),random(0,spead));
velocity = new PVector(0,0);
acceleration = new PVector(random(-1,1),random(-1,1));//random initial acceleration
topspeed = 4;
}
void update()
{
// Motion 101! Velocity changes by acceleration. Location changes by velocity.
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display()
{
pushMatrix();
translate(location.x,location.y);
//fill(102,0,155,random(120,160));
//fill(32,225,245,127 + sin(frameCount*.01) * 127);
if(hit) fill(255,10,96,120);
else
fill(3,225,190,random(50,127));
ellipse(0,0,30,30);
noStroke();
popMatrix();
}
void checkEdges()
{
if(location.x < 0){
location.x = 0;
velocity.x *= -1;
acceleration.mult(0.001);
}
if(location.x > 800){
location.x = 800;
velocity.x *= -1;
acceleration.mult(0.001);
}
if(location.y < 0){
location.y = 0;
velocity.y *=- 1;
acceleration.mult(0.001);
}
if(location.y > 800){
location.y = 800;
velocity.y *= -1;
acceleration.mult(0.001);
}
}
}

Quick and dirty platformer with Physics has errors with moving platforms

Alright, so I'm trying to make a quick and dirty platformer engine, and I am having some problems with my collision detection and moving platforms. for one thing the "player" seems to bounce on top of moving platforms ever so slightly, and when he hits the right side errors happen as well. I'll upload a jnlp demo so you can try to find more errors and see whats happening, but here is the source:
import java.awt.Rectangle;
import java.util.Vector;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
public class Platformer extends BasicGame{
boolean keys[];
int ALL_KEYS = 0xFF;
Player player;
Vector<Vector<Thing> > things;
int level = 0;
public Platformer() {
super("You've met with a terrible fate, haven't you?");
}
public void init(GameContainer gc) throws SlickException {
keys = new boolean[ALL_KEYS];
for(int i = 0; i < ALL_KEYS; i++){
keys[i] = false;
}
player = new Player();
things = new Vector<Vector<Thing> >();
Vector<Thing> temp = new Vector<Thing>();
temp.add(new Thing(0, 440, 640, 40, 1));
temp.add(new Thing(200, 300, 240, 50, 1));
temp.add(new Thing(500, 200, 240, 50, 1));
things.add(temp);
}
public void update(GameContainer gc, int delta) throws SlickException{
if(keys[Input.KEY_UP]){
player.velo = player.maxJump;
keys[Input.KEY_UP] = false;
}
if(keys[Input.KEY_DOWN]){
keys[Input.KEY_DOWN] = false;
}
if(keys[Input.KEY_LEFT]){
player.delta -= player.speed;
if(player.delta < -player.maxSpeed)
player.delta = -player.maxSpeed;
}
else if(keys[Input.KEY_RIGHT]){
player.delta += player.speed;
if(player.delta > player.maxSpeed)
player.delta = player.maxSpeed;
}
else{
if(player.delta < -0.5){
player.delta += player.speed;
}
else if(player.delta > 0.5){
player.delta -= player.speed;
}
else if(player.delta > -0.5 && player.delta < 0.5){
player.delta = 0;
}
}
if(player.delta < 0)
player.moveLeft(things.get(level));
else if(player.delta > 0)
player.moveRight(things.get(level));
if(player.velo < 0)
player.moveUp(things.get(level));
else
player.moveDown(things.get(level));
things.get(level).get(1).moveRight(player, things.get(level));
}
public void render(GameContainer gc, Graphics g) throws SlickException{
g.setColor(new Color(0,55,55));
g.fillRect(0, 0, 640, 480);
g.setColor(new Color(255,0,0));
g.fillRect(player.x, player.y, player.width, player.height);
for(int i = 0; i < things.get(level).size(); i++){
if(things.get(level).get(i).type == 1)
g.setColor(new Color(0,100,100));
g.fillRect(things.get(level).get(i).x, things.get(level).get(i).y,things.get(level).get(i).width, things.get(level).get(i).height);
}
}
public void keyPressed(int key, char c) {
keys[key] = true;
}
public void keyReleased(int key, char c) {
keys[key] = false;
}
public static void main(String[] args) throws SlickException{
AppGameContainer app =
new AppGameContainer( new Platformer() );
app.setShowFPS(false);
app.setAlwaysRender(true);
app.setTargetFrameRate(60);
app.setDisplayMode(640, 480, false);
app.start();
}
class Player{
float x = 50;
float y = 50;
float delta = 0; // x momentum
float velo = 0;
int height = 50;
int width = 30;
float speed = 0.2f;
int maxSpeed = 6;
int maxFallSpeed = 5;
int maxJump = -8;
public void moveLeft(Vector<Thing> things){
x += delta;
if(x < 0)
x = 0;
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
x += (things.get(i).x + things.get(i).width) - x;
delta = 0;
}
}
}
public void moveRight(Vector<Thing> things){
x += delta;
if(x + width > 640)
x = (640 - width);
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
x -= (x + width) - things.get(i).x;
delta = 0;
}
}
}
public void moveLeftWithThing(Vector<Thing> things, float thingSpeed){
x -= thingSpeed;
if(x < 0)
x = 0;
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
x += (things.get(i).x + things.get(i).width) - x;
delta = 0;
}
}
}
public void moveRightWithThing(Vector<Thing> things, float thingSpeed){
x += thingSpeed;
if(x + width > 640)
x = (640 - width);
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
x -= (x + width) - things.get(i).x;
delta = 0;
}
}
}
public void moveUp(Vector<Thing> things){
y += velo;
velo += speed;
if(velo > maxFallSpeed)
velo = maxFallSpeed;
for(int i = 0; i < things.size(); i++){
if(new Rectangle((int) x, (int) y, width, height/2).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
y += (things.get(i).y + things.get(i).height) - y;
velo = 0;
}
}
}
public void moveDown(Vector<Thing> things){
y += velo;
velo += speed;
if(velo > maxFallSpeed)
velo = maxFallSpeed;
boolean b = false;
for(int i = 0; i < things.size(); i++){
if(!b && new Rectangle((int) x, (int) y + (height/2), width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
y -= (y + height) - things.get(i).y;
velo = 0;
}
}
}
}
class Thing{
float x = 50;
float y = 50;
int height = 50;
int width = 30;
int type = -1;
float speed = 0.5f;
public Thing(float x, float y, int width, int height, int type){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.type = type;
}
public void moveUp(Player player){
y -= 0.5f;
if(new Rectangle((int) x,(int) y, width, height).intersects(new Rectangle((int) player.x, (int) player.y, player.width, player.height))){
player.y -= (player.y + player.height) - y;
player.velo = 0;
}
}
public void moveRight(Player player, Vector<Thing> things){
x += speed;
if(new Rectangle((int) x,(int) y - 1, width, height).intersects(new Rectangle((int) player.x, (int) player.y, player.width, player.height))){
player.moveRightWithThing(things, speed);
}
}
}
}
And here is the demo: http://prime.programming-designs.com/java/platformer_demo/platdemo.jnlp
The reason why you are having trouble finding the error is that the code is ghastly.
Vast amounts of duplication of code: moveLeft, moveRight, moveUp and moveDown are all very similar.
Logic for acceleration due to gravity appears in two places.
Collision logic repeated all over the place.
Desperately poor choice of names. velo for velocity downwards, but delta for velocity rightwards (mislabelled as "momentum"): why not vy and vx? Also maxSpeed for maximum horizontal velocity but maxFallSpeed for maximum vertical velocity.
Acceleration/deceleration called speed.
Wasteful allocation, e.g. calling new Rectangle every time you want to test whether something has hit something else.
Positions are floating-point, but collision is based on integers.
Duplication of code has the effect of introducing bugs, because when functionality is duplicated there's the possibility of getting it wrong in one of the places, and also of hiding bugs, because the volume of code makes it harder to spot them.
Anyway, the bouncing on platforms may be something to do with the fact that in Player:moveDown you are using a rectangle that's offset by half the player's height, whereas in Thing:moveRight you are colliding with a rectangle that's not offset. (But that's just a guess.)

Categories

Resources