Processing/Java - Having trouble with Class Array - java

This is probably a really stupid question but I am having trouble showing more that one copy of my class on the screen.
I have created an asteroid class that generates and moves an asteroid on the screen. Yet when I try and call multiple versions of this class in my main body, it still only shows one asteroid.
Main
int lrgAsteroids = 4;
Asteroid[] asteroid = new Asteroid[lrgAsteroids];
void setup() {
size(800,800);
for (int i = 0; i < lrgAsteroids; i++) {
asteroid[i] = new Asteroid();
asteroid[i].display();
}
}
void draw() {
background(0);
asteroid[0].move();
asteroid[1].move();
for (int i = 0; i < lrgAsteroids; i++) {
asteroid[i].move();
}
}
asteroid class.
class Asteroid {
PImage lrgAsteroid;
float xpos, ypos;
float yDirection;
float xDirection;
float radians = 0;
Asteroid() {
lrgAsteroid = loadImage("largeAsteroid.png");
xpos = random(0,710);
ypos = random(0,710);
int xDir = (int) random(2);
int yDir = (int) random(2);
if (xDir == 1) {
xDirection = 1;
} else if (xDir == 0) {
xDirection = -1;
}
if (yDir == 1) {
yDirection = 1;
} else if (yDir == 0) {
yDirection = -1;
}
}
void display() {
image(lrgAsteroid, xpos, ypos);
}
void move() {
background(0);
pushMatrix();
imageMode(CENTER);
translate(xpos, ypos);
rotate(radians);
image(lrgAsteroid, 0, 0);
popMatrix();
if (xpos <= 0) {
xpos = random(750,800);
} else if (xpos >= 800) {
xpos = random(0,100);
}
if (ypos <= 0) {
ypos = random(750,800);
} else if (ypos >= 800) {
ypos = random(0,100);
}
radians += 0.02;
xpos += xDirection;
ypos += yDirection;
}
}
Any help would be greatly appreciated.

The bug is very simple. Actually the display is clear, before an asteroid is drawn, because of background(0); in the method move(). It is sufficient to clear the background at the begin of draw().
Remove background(0); from the method move():
Asteroid() {
// [...]
void move() {
// background(0); <---- DELETE
pushMatrix();
imageMode(CENTER);
translate(xpos, ypos);
rotate(radians);
image(lrgAsteroid, 0, 0);
popMatrix();
if (xpos <= 0) {
xpos = random(750,800);
} else if (xpos >= 800) {
xpos = random(0,100);
}
if (ypos <= 0) {
ypos = random(750,800);
} else if (ypos >= 800) {
ypos = random(0,100);
}
radians += 0.02;
xpos += xDirection;
ypos += yDirection;
}
}

I think that somehow all the instances that your create get the same xDir and yDir or same xpos and ypos , can you print it so you can see if that's the problem ?
void setup() {
size(800,800);
for (int i = 0; i < lrgAsteroids; i++) {
asteroid[i] = new Asteroid();
// add these please to see what happens
System.out.println(asteroid[i].xDirection+" "+asteroid[i].yDirection);
}
}
void draw() {
background(0);
asteroid[0].move();
asteroid[1].move();
for (int i = 0; i < lrgAsteroids; i++) {
asteroid[i].move();
// add these please to see what happens
System.out.println(asteroid[i].xpos+" "+asteroid[i].ypos);
}

Related

How can I make two objects of the same class bounce when colliding?

I made a bouncing ball code, and so far as the bouncing itself the code works perfectly. I then created a second ball, and it also does what it's supposed to do. However, when I try to use an if condition to make the two balls bounce off one another as well as the edges, it doesn't work. Either they don't move or they just don't collide, and go through each other. This code was made in processing. Can anyone help me make ball1 and ball2 collide?
Moving ball1;
Moving ball2;
void setup(){
size(600,600);
ball1 = new Moving();
ball2 = new Moving();
}
void draw(){
background(255);
ball1.move();
ball1.display();
ball1.bounce();
ball2.move();
ball2.display();
ball2.bounce();
ball1.clash();
ball2.clash();
}
class Moving {
float speed = 7;
float x = random(0, width);
float y= random(0, height);
float xdirection = 1;
float ydirection = 1;
float ball_size = 50;
float radius = ball_size/2;
Moving() {
}
void move() {
x = x + (xdirection * speed);
y = y + (ydirection* speed);
}
void display() {
noStroke();
fill(50, 0, 50);
circle(x, y, ball_size);
}
void bounce() {
if ((x >= width - radius) || (x <= radius)) {
xdirection = xdirection * -1;
}
if ((y >= height - radius)|| (y<=radius)) {
ydirection = ydirection * -1;
}
}
void clash() {
if ((ball1.y+radius == ball2.y+radius) && (ball1.x+radius == ball2.x+radius)) {
ball1.ydirection = ball1.ydirection * -1;
ball2.ydirection = ball2.ydirection * -1;
ball1.xdirection = ball1.xdirection * -1;
ball2.xdirection = ball2.xdirection * -1;
x = x + (xdirection * speed);
y = y + (ydirection* speed);
if (ball1.x+radius == ball2.x+radius) {
xdirection = xdirection * -1;
}
}
}
}

Processing - How to resize shapes

Using the mouse, I would like to change the size of the shapes that I have in my code (which is circle and rectangle) by clicking on the corners and dragging it to change the shape's width and height.
Here's my main code:
circleShape [] objs;
int numObjs = 1;
int pickedItem = -1;
rectShape [] objss;
int numObjss = 1;
int itemPicked = -1;
void setup() {
size(600, 600);
objs = new circleShape[numObjs];
for (int i = 0; i < numObjs; i++) {
int radius = (int)(random(20, 70));
color c = (int)(random(0, 255));
objs[i] = new circleShape(radius, c);
}
objss = new rectShape[numObjss];
for (int i = 0; i < numObjss; i++) {
int Radius = (int)(random(20, 70));
color C = (int)(random(0, 255));
objss[i] = new rectShape(Radius, C);
}
}
void draw() {
background(255);
for (int i = 0; i < numObjs; i++) {
objs[i].drawShape();
}
for (int i =0; i < numObjss; i++) {
objss[i].shapeDraw();
}
}
// return the index number of the picked item from the array
// return -1 otherwise
int getPickedObj(int x, int y) {
for (int i = 0; i < numObjs; i++) {
if (objs[i].isOnShape(x, y)) {
return i;
}
}
return -1;
}
int pickedObj(int X, int Y) {
for (int i = 0; i < numObjss; i++) {
if (objss[i].onShape(X, Y)) {
return i;
}
}
return -1;
}
void mousePressed() {
pickedItem = getPickedObj(mouseX, mouseY);
itemPicked = pickedObj(mouseX, mouseY);
}
void mouseDragged() {
if (pickedItem >= 0) {
objs[pickedItem].move(mouseX, mouseY);
objs[pickedItem].drawShape();
}
if (itemPicked >= 0) {
objss[itemPicked].move(mouseX, mouseY);
objss[itemPicked].shapeDraw();
}
}
And here's my shape code:
// a shape is simply as a circle
class circleShape {
int posX, posY, radius;
color c;
circleShape(int radius, color c) {
this.radius = radius;
this.c = c;
posX = (int)random(0, 200);
posY = (int)random(0, 200);
}
// return true if the position x, y is on the shape
boolean isOnShape(int x, int y) {
float d = (posX - x)*(posX - x) + (posY - y)*(posY - y);
d = sqrt(d);
if (d < radius) return true;
else return false;
}
void move (int newPosX, int newPosY) {
this.posX = newPosX;
this.posY = newPosY;
}
void drawShape() {
noStroke();
fill(c);
ellipse(posX, posY, 2*radius, 2*radius);
}
}
//Rectangle Shape
class rectShape {
int xPos, yPos, Radius;
color C;
rectShape(int Radius, color C) {
this.Radius = Radius;
this.C = C;
xPos = (int)random(0, 200);
yPos = (int)random(0, 200);
}
boolean onShape(int X, int Y) {
float D = (xPos - X)*(xPos - X) + (yPos - Y)*(yPos - Y);
D = sqrt(D);
if (D < Radius) return true;
else return false;
}
void move (int posXnew, int posYnew) {
this.xPos = posXnew;
this.yPos = posYnew;
}
void shapeDraw() {
noStroke();
fill(C);
rect(xPos, yPos, 2*Radius+60, 2*Radius);
}
}
My code allows the mouse to move the shapes from one place to another. It just needs to be able to change the size of them.
I'll give you an example for a rectangular shape.
A rectangle is defined by a position, the width and the height. Change the attributes and the constructor of the rectangle:
class rectShape {
int xPos, yPos, Width, Height;
color C;
rectShape(int X, int Y, int Width, int Height, color C) {
this.xPos = X;
this.yPos = Y;
this.Width = Width;
this.Height = Height;
this.C = C;
}
// [...]
void shapeDraw() {
noStroke();
fill(C);
rect(xPos, yPos, this.Width, this.Height);
}
}
Create a random rectangle:
void setup() {
size(600, 600);
// [...]
objss = new rectShape[numObjss];
for (int i = 0; i < numObjss; i++) {
int xPos = (int)random(0, 200);
int yPos = (int)random(0, 200);
int Width = (int)(random(20, 70));
int Height = (int)(random(20, 70));
color C = (int)(random(0, 255));
objss[i] = new rectShape(xPos, yPos, Width, Height, C);
}
}
Create a method in the class rectShape, which determines if the mouse is on the rectangle and if it is on the left, right, top, bottom or center:
String onShape(int X, int Y) {
String where = "";
Boolean isOn = X > xPos && X < xPos + Width && Y > yPos && Y < yPos + Height;
if (isOn) {
int left = abs(X - xPos);
int right = abs(X - (xPos + Width));
int top = abs(Y - yPos);
int bottom = abs(Y - (yPos + Height));
if (min (left, right) < min (top, bottom)) {
if (left < 10)
where = "left";
else if (right < 10)
where = "right";
else
where = "center";
}
else {
if (top < 10)
where = "top";
else if (bottom < 10)
where = "bottom";
else
where = "center";
}
}
return where;
}
Create a method which can change the rectangle dependent on an action command:
void update(String what, int posXnew, int posYnew) {
if (what == "left") {
if (posXnew < this.xPos + this.Width) {
this.Width = this.xPos + this.Width - posXnew;
this.xPos = posXnew;
}
}
else if (what == "right") {
if (posXnew > this.xPos) {
this.Width = posXnew - this.xPos;
}
}
else if (what == "top") {
if (posYnew < this.yPos + this.Height) {
this.Height = this.yPos + this.Height - posYnew;
this.yPos = posYnew;
}
}
else if (what == "bottom") {
if (posYnew > this.yPos) {
this.Height = posYnew - this.yPos;
}
}
else {
this.xPos = posXnew - this.Width / 2;
this.yPos = posYnew - this.Height / 2;
}
}
Determine the action when the rectangle is clicked on:
String itemAction = "";
int pickedObj(int X, int Y) {
for (int i = 0; i < numObjss; i++) {
itemAction = objss[i].onShape(X, Y);
if (itemAction != "") {
return i;
}
}
return -1;
}
void mousePressed() {
// [...]
itemPicked = pickedObj(mouseX, mouseY);
}
Pass the action to the update method when the mouse is dragged:
void mouseDragged() {
if (itemPicked >= 0) {
objss[itemPicked].update(itemAction, mouseX, mouseY);
}
}
Complete class rectShape:
//Rectangle Shape
class rectShape {
int xPos, yPos, Width, Height;
color C;
rectShape(int X, int Y, int Width, int Height, color C) {
this.xPos = X;
this.yPos = Y;
this.Width = Width;
this.Height = Height;
this.C = C;
}
String onShape(int X, int Y) {
String where = "";
Boolean isOn = X > xPos && X < xPos + Width && Y > yPos && Y < yPos + Height;
if (isOn) {
int left = abs(X - xPos);
int right = abs(X - (xPos + Width));
int top = abs(Y - yPos);
int bottom = abs(Y - (yPos + Height));
if (min (left, right) < min (top, bottom)) {
if (left < 10)
where = "left";
else if (right < 10)
where = "right";
else
where = "center";
}
else {
if (top < 10)
where = "top";
else if (bottom < 10)
where = "bottom";
else
where = "center";
}
}
return where;
}
void update(String what, int posXnew, int posYnew) {
if (what == "left") {
if (posXnew < this.xPos + this.Width) {
this.Width = this.xPos + this.Width - posXnew;
this.xPos = posXnew;
}
}
else if (what == "right") {
if (posXnew > this.xPos) {
this.Width = posXnew - this.xPos;
}
}
else if (what == "top") {
if (posYnew < this.yPos + this.Height) {
this.Height = this.yPos + this.Height - posYnew;
this.yPos = posYnew;
}
}
else if (what == "bottom") {
if (posYnew > this.yPos) {
this.Height = posYnew - this.yPos;
}
}
else {
this.xPos = posXnew - this.Width / 2;
this.yPos = posYnew - this.Height / 2;
}
}
void shapeDraw() {
noStroke();
fill(C);
rect(xPos, yPos, this.Width, this.Height);
}
}

Libgdx Accelerometer not working?

for some reason the block is not moving when I tilt the screen, and I don't know what's wrong. Just to clarify, I set the cfg.accelerometer to true, but the cfg.compass to false. Here's the source code-
public void update()
{
x += velX;
y += velY;
//movement
//left
if (Gdx.input.isKeyPressed(Keys.LEFT))
{
velX = -speed;
}
//right
if (Gdx.input.isKeyPressed(Keys.RIGHT))
{
velX = speed;
}
//up
if (Gdx.input.isKeyPressed(Keys.UP))
{
velY = -speed;
}
//down
if (Gdx.input.isKeyPressed(Keys.DOWN))
{
velY = speed;
}
if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer))
{
velX = Gdx.input.getAccelerometerX();
velY = Gdx.input.getAccelerometerY();
}
//stop
if (!Gdx.input.isKeyPressed(Keys.LEFT) && !Gdx.input.isKeyPressed(Keys.RIGHT))
{
velX = 0;
}
if (!Gdx.input.isKeyPressed(Keys.UP) && !Gdx.input.isKeyPressed(Keys.DOWN))
{
velY = 0;
}
//collision with edges of screen
if (x <= 0)
{
x = 0;
}
if (x >= 1920 - width)
{
x = 1920 - width;
}
if (y <= 0)
{
y = 0;
}
if (y >= 1080 - height)
{
y = 1080 - height;
}
long recoveryElapsed = (System.nanoTime() - recoveryTimer)/1000000;
if (recoveryElapsed > 2000)
{
recovering = false;
recoveryTimer = 0;
}
System.out.println(lives+ " lives, recovering, "+recovering);
}
Help would be much appreciated, thanks. There are no tutorials that I have found with a working example shown, so I don't really know if what I am doing is correct, but I can't see anything wrong with it.
You are setting the Accel values, and then if the keys are not pressed you are setting them to 0 again. Do it like this:
//stop
if (!Gdx.input.isKeyPressed(Keys.LEFT) && !Gdx.input.isKeyPressed(Keys.RIGHT))
{
velX = 0;
}
if (!Gdx.input.isKeyPressed(Keys.UP) && !Gdx.input.isKeyPressed(Keys.DOWN))
{
velY = 0;
}
if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer))
{
velX = Gdx.input.getAccelerometerX();
velY = Gdx.input.getAccelerometerY();
}

Restarting millis in processing error

Having a massive problem with this piece of code. I'm working with Java in processing.
I've created a game where users must guide a character away from objects.
All the objects, health system and score system are based on mills().
Once the game ends we need to reset millis(), resetting the objects, score and health system.
I have searched implemented advice from friends and previously asked questions on here but the advice differs very slightly. I'm assuming it's something that I can't see.
I would really appreciate your help with this, I only ever use this site as a last resort not just when I'm feeling lazy.
//these are used to set the times which the games increases difficulty
//int timeDelay = 30000;
int delayOne = 2000;
int delayTwo = 5000;
int delayThree = 80000;
int delayFour = 90000;
int display = 2000;
//for collisions
float[] xpos = new float[6];
float[] ypos = new float[6];
//timer counts how many millis() each game lasts for
int timeStamp = 5000;
int timer;
int timer2 = millis() - timer;
//always at zero
//outputting score at the end of a game
int score;
int start;
//trying to get lives working
boolean lost = false;
//variable to store & output gale force when giving score
int Gale = 0;
//Changing length rectangle
float rX = 350.0;
float x1 = 20;
float y1 = 20;
float w1 = 100;
float h1 = 30;
//DECLARE OBJECTS JELLY CLASS
Jelly myObject;
Jelly myObject1;
Jelly myObject2;
Jelly myObject3;
//GENTLEMAN CLASS
gentleMan mygentleMan;
//LOLLY CLASS
Lolly myCow;
Lolly myCow1;
//PImages
PImage loader;
PImage bg;
PImage uh;
PImage bg1;
PImage bolt;
PImage over;
void setup()
{
bg=loadImage("backy1.png");
bg1 = loadImage("backy.png");
over = loadImage("over.png");
PFont L = loadFont("Lobster1.3-48.vlw");
textFont( L, 16);
size(400, 600);
smooth();
//begin = millis();
imageMode(CENTER);
//INITIALISE
myObject = new Jelly(320, 500);
myObject1 = new Jelly(150, 200);
// myObject2 = new Jelly(550, 500);
//myObject3 = new Jelly(300, 100);
mygentleMan = new gentleMan(200, 300);
//myObject.run();
//myObject1.run();
//myObject2.run();
myCow = new Lolly(400, 250);
myCow1 = new Lolly(150, 350);
timer = millis();
}
void draw()
{
start = 0;
//because we have image mode set to center for collisions
//we have to divide the height & width of the screen by 2 for hte image to fit
image(bg, 200, 300);
if (millis() >= start + delayOne)
{
image(bg1, 200, 300);
}
//CALL FUNCTIONALITY
myObject.run();
myObject.put_in_array(0);
myObject1.run(); // this one is going top to bottom
myObject1.put_in_array(1);
// myObject2.run();
//myObject2.put_in_array(2);
// myObject3.run();
//myObject3.put_in_array(3);
myCow.run();
myCow.put_in_array(4);
myCow1.run();
myCow1.put_in_array(5);
mygentleMan.run();
//health bar
fill(161, 221, 16);
noStroke();
rect(10, 24, rX, 10);
if(rX <= 100)
{
fill(221, 59, 16);
rect(10, 24, rX, 10);
}
else
if(rX <= 200)
{
fill(221, 137, 16);
rect(10, 24, rX, 10);
}
if(rX == 5.0)
{
lost = true;
noLoop();
// lives = lives - 1;
image(over, width/2, height/3);
fill(255);
text("Your Score Is: " + timer, width/2.7, height/2);
text("Gale Force Is; " + Gale, width/2.7, height/1.8);
score = timer;
}
//For Loop detecting collisions between mygentleMan & objects
for (int i=0; i < 6; i++) {
if (xpos[i] > 150 && xpos[i] < 250 && ypos[i] > (mygentleMan.y-58) && ypos[i] < (mygentleMan.y+58))
{
// text("collision", 200, 300);
bolt = loadImage("bolt.png");
image(bolt, xpos[i], ypos[i]);
rX = rX - 1;
}
//outputting score on screen # at all times
fill(255);
text("Score: " + timer, 320, 20);
}
//timer which will be score counter essentially
timer = millis();
//text(timer, 20, 20);
//moving the man up the screen if button is pressed, if not he levitates downward
if (keyPressed)
{
mygentleMan.y -= mygentleMan.moveY;
mygentleMan.moveY += 0.4;
}
else
{
mygentleMan.y += mygentleMan.moveY;
mygentleMan.moveY += 0.2;
}
fill(255);
text("Health", 20, 20);
if(mousePressed)
{
if(timer2 > timeStamp)
{
println("tit");
mygentleMan.y = height/2;
loop();
}
}
}
//class for first objects that move into the screen
class Jelly
{
//GLOBAL VARIABLES
float x = 0;
float y = 0;
float speedX = 1.8;
float speedY = 1.8;
float speedX2 = 2.1;
float speedY2 = 2.1;
float speedX3 = 2.2;
float speedY3 = 2.2;
PImage jelly = loadImage("jelly.png");
PImage hat = loadImage("hat.png");
PImage gale = loadImage("g1.png");
PImage force = loadImage("force.png");
PImage news = loadImage("news.png");
//CONSTRUCTOR
Jelly(float _x, float _y)
{
x = _x;
y = _y;
}
//FUNCTIONS
void run()
{
display();
move();
bounce();
image(force, 330, 550);
if (millis() >= start + delayOne)
{
display();
moveFast();
bounceFast();
image(gale, 280, 560);
Gale = 1;
if (start + delayOne + display >= millis())
{
image(news, 200, 300);
}
}
if (millis() >= start +delayTwo)
{
display();
moveFaster();
bounceFaster();
image(gale, 310, 560);
Gale = 2;
if (start + delayTwo + display >= millis())
{
image(news, 200, 300);
}
}
}
void bounce()
{
if ( x > width)
{
speedX = speedX * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX = speedX * -1;
}
if ( y > height)
{
speedY = speedY * -1;
}
if ( y < 0)
{
speedY = speedY * -1;
}
}
void bounceFast()
{
if ( x > width)
{
speedX2 = speedX2 * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX2 = speedX2 * -1;
}
if ( y > height)
{
speedY2 = speedY2 * -1;
}
if ( y < 0)
{
speedY2 = speedY2 * -1;
}
}
void bounceFaster()
{
if ( x > width)
{
speedX3 = speedX3 * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX3 = speedX3 * -1;
}
if ( y > height)
{
speedY3 = speedY3 * -1;
}
if ( y < 0)
{
speedY3 = speedY3 * -1;
}
}
void move()
{
x = x + speedX;
y = y + speedY;
}
void moveFast()
{
x = x + speedX2;
y = y + speedY2;
}
void moveFaster()
{
x = x + speedX3;
y = y + speedY3;
}
void put_in_array(int a)
{
xpos[a] = x;
ypos[a] = y;
}
void display()
{
image(hat, x, y);
}
}
//class for gentleman that floats
class gentleMan
{
//GLOBAL VARIABLES
float y = 400;
float x = 400;
float moveY;
//PImage umbrella;
PImage umbrella = loadImage("dafuq.png");
PImage over = loadImage("over.png");
//CONSTRCUTOR --- PIECES OF INFO PROVDE TO BUILD CLASS -- INTIIALIZE VARIBALE
gentleMan(float _x, float _y)
{
y = _y;
x = _x;
moveY = 2;
}
//FUNCTIONS
void run()
{
display();
keyReleased();
bounce();
// collision();
}
void display()
{
image(umbrella, x, y);
}
void keyReleased()
{
mygentleMan.moveY = 4;
}
void bounce()
{
if ( y < 0)
{
y = 0;
}
if (y > height)
{
//score = millis();
lost = true;
noLoop();
// lives = lives - 1;
image(over, width/2, height/3);
text("Your Score Is: " + timer, width/2.7, height/2);
text("Gale Force Is; " + Gale, width/2.7, height/1.8);
}
}
}
class Lolly
{
//GLOBAL VARIABLES
float x = 0;
float y = 0;
float speedX = 2;
float speedY = 2;
float speedX1 = 2.1;
float speedY1 = 2.1;
float speedX2 = 2.3;
float speedY2 = 2.3;
PImage cow = loadImage("cow.png");
//CONSTRUCTOR
Lolly(float _x, float _y)
{
x = _x;
y = _y;
}
//FUNCTIONS
void run()
{
// display();
//move();
//bounce();
if (millis() >= start + delayThree)
{
display();
moveFast();
bounceFast();
}
if (millis() >= start +delayFour)
{
display();
moveFaster();
bounceFaster();
}
}
void put_in_array(int a)
{
xpos[a] = x;
ypos[a] = y;
}
void bounce()
{
if ( x > width)
{
speedX = speedX * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX = speedX * -1;
}
if ( y > height)
{
speedY = speedY * -1;
}
if ( y < 0)
{
speedY = speedY * -1;
}
}
void bounceFast()
{
if ( x > width)
{
speedX1 = speedX1 * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX1 = speedX1 * -1;
}
if ( y > height)
{
speedY1 = speedY1 * -1;
}
if ( y < 0)
{
speedY1 = speedY1 * -1;
}
}
void bounceFaster()
{
if ( x > width)
{
speedX2 = speedX2 * -1; //multiply by -1 to make it bounce
}
if ( x < 0)
{
speedX2 = speedX2 * -1;
}
if ( y > height)
{
speedY2 = speedY2 * -1;
}
if ( y < 0)
{
speedY2 = speedY2 * -1;
}
}
void move()
{
x = x + speedX;
y = y + speedY;
}
void moveFast()
{
x = x + speedX1;
y = y + speedY1;
}
void moveFaster()
{
x = x + speedX2;
y = y + speedY2;
}
void display()
{
image(cow, x, y);
}
}//end of cow class
void mousePressed()
{
}
Your question is not at all clear, but it sounds like you should be able to wrap System.currentTimeMillis() in an object that maintains a starting point and returns the offset. Something like
public class Millis
{
long start;
public Millis() { this.reset(); }
public void reset() { this.start = System.currentTimeMillis(); }
public long getMillis() { return System.currentTimeMillis() - start; }
}
You create an instance of this class at startup
Millis timer = new Millis();
then call reset() to set it back to zero at the beginning of each game. Everywhere in your code you currently have millis() you would have timer.getMillis()
Use a custom timer class
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}//end class
Then to call
firstTimer = new Timer(50000);
And then in the draw to check
if (firstTimer.isFinished()) {
//do sopemthing
//then restart timer
firstTimer.start();
}
else
{
// do soemthing else for the rest of the time....
}

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);
}
}
}

Categories

Resources