Thx Seltar,
Sometimes things are so obvious that you can't see it when you are close to. :)
I've tried atan2() before without any success but i was sure it was the right direction, it failed maybe because there isn't any x/y coordinates support in Saito's Object loader.
The commented code is what i've tried and failed to make it work.
Quote:import saito.objloader.*;
import processing.opengl.*;
OBJModel cube;
Style S;
int WIDTH = 1024;
int HEIGHT = 768;
int ShapeS = 2;
int MINSIZE = 10;
int MAXSIZE = 10;
float GRAV = 0.0;
int viewmode = 0;
int targetAngle;
PImage Surface;
float distance(float x1, float y1, float x2, float y2) {
return sqrt(sq(x1-x2) + sq(y1-y2));
}
void setup() {
size(1024, 768, OPENGL);
frameRate(30);
smooth();
noStroke();
S = new Style();
cube = new OBJModel(this);
cube.load("MyOBJ.obj");
Surface = loadImage("Surface2.png");
}
void draw() {
background(0);
image(Surface,0,0,1024,768);
lights();
S.update();
S.cube();
}
class Shape {
float x, y, xvel, yvel, radius;
int contact[] = new int[ShapeS];
int vomf = 0;
Shape(float xIn, float yIn, float radiusIn) {
x = xIn;
y = yIn;
radius = radiusIn;
}
void incontact(int obj) {
contact[vomf] = obj;
vomf++;
}
void update() {
x += xvel;
y += yvel;
xvel *= 0.99;
yvel *= 0.99;
yvel += GRAV;
if(y+32 > (HEIGHT)-radius) {
y = HEIGHT-radius-33;
if(yvel > 0) yvel = -yvel*0.9;
}
else if(y-35 <= (radius)) {
y = radius+36;
if(yvel < 0) {
yvel = -yvel*0.9;
}}
if(x+25 > (WIDTH)-radius) {
x = WIDTH-radius-26;
if(xvel > 0) xvel = -xvel*0.9;
}
else if(x-20<= (radius)) {
x = radius+21;
if(xvel < 0) {
xvel = -xvel*0.9;
}
}
}
}
class Style {
Shape b[] = new Shape[ShapeS];
Style() {
createShapes();
}
void createShapes() {
b[0] = new Shape(0,0,25);
for(int i = 1; i < ShapeS; i++) {
b[i] = new Shape(random(WIDTH), random(HEIGHT), random(MINSIZE+1, MAXSIZE+1));
}
}
void update() {
float dir;
float dist;
for(int i = 1; i < ShapeS; i++) {
for(int u = 0; u < ShapeS; u++) {
if(i != u) {
dist = distance(b[i].x, b[i].y, b[u].x, b[u].y) / (b[i].radius + b[u].radius);
if(dist < 1) {
b[i].incontact(u);
b[i].xvel *= 0.95;
b[i].yvel *= 0.95;
dir = atan2((int)(b[u].y - b[i].y), (int)(b[u].x - b[i].x));
b[i].xvel += -15*(1-dist)*cos(dir);
b[i].yvel += -15*(1-dist)*sin(dir);
}
}
}
}
for(int i = 1; i < ShapeS; i++) {
b[i].update();
}
b[0].x = mouseX;
b[0].y = mouseY;
}
void cube() {
//targetAngle = atan2(mouseX-cube.x,mouseY-cube.y);
//cube.angle += (cube.angle-targetAngle)/100;
//cube.x += (cube.x-mouseX)/100;
//cube.y += (cube.y-mouseY)/100;
for(int i = 1; i < ShapeS; i++) {
pushMatrix();
translate(b[i].x, b[i].y,0);
// rotateX(cube.x);
// rotateY(cube.y);
scale(10.0);
cube.drawMode(POLYGON);
cube.draw();
popMatrix();
b[i].vomf = 0;
}
}
}
if i comment out the lines, i'm getting this error :
/tmp/build31132.tmp/Temporary_4780_6717.java:134:17:134:17: Semantic Error: No accessible field named "y" was found in type "saito.objloader.OBJModel".
/tmp/build31132.tmp/Temporary_4780_6717.java:138:16:138:16: Semantic Error: No accessible field named "x" was found in type "saito.objloader.OBJModel".