We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › rotating an object to the direction it goes
Page Index Toggle Pages: 1
rotating an object to the direction it goes (Read 938 times)
rotating an object to the direction it goes
Jun 2nd, 2007, 4:14pm
 
Hi all,

i'm trying to  push an object (e.g rectangle) with the mouse (code is done already) and make it rotating according to the direction it goes.

i'm using rotateX() and rotateY() but i can't find a simple way to catch the object x/y displacement angle and add it to rotateX() and rotateY().

thanks in advance for any suggestion.
Re: rotating an object to the direction it goes
Reply #1 - Jun 2nd, 2007, 8:10pm
 
This is what you should be playing with..
http://processing.org/reference/atan2_.html

i.e.
targetAngle = atan2(mouseX-rect.x,mouseY-rect.y);
rect.angle += (rect.angle-targetAngle)/30;
rect.x += (rect.x-mouseX)/30;
rect.y += (rect.y-mouseY)/30;

something like that at least, might have to reorder a couple of the variables as this was off the top of my head Smiley
Re: rotating an object to the direction it goes
Reply #2 - Jun 3rd, 2007, 12:10am
 
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".


Re: rotating an object to the direction it goes
Reply #3 - Jun 3rd, 2007, 10:48pm
 
Hi again,

After many unsuccessful modifications, i still haven't found a way to get the direction/angle value.

Saito, if you read this thread can you tell if your OBJ Loader is reporting the x/y direction's angle of an object or if it is possible to get the previous x/y position of an object (like pmouseX/Y)
Re: rotating an object to the direction it goes
Reply #4 - Jun 3rd, 2007, 10:53pm
 
AS far as I know the OBJloadre doesn't ahve any idea of position/direction or anything of an obj, it just draws it plainly. if you want to move it you have to use translate and rotateX/Y/Z yourself, the OBJ just says "draw me" it's up to you to have changed things such that it draws where you want it.
Re: rotating an object to the direction it goes
Reply #5 - Jun 3rd, 2007, 11:01pm
 
Thx John, i almost came to that conclusion Smiley

How about drawing a rect/ellipse/box under the object (using the same x/y value given by Seltar) and using it to control the OBJ's angle ?

Just like if you kick in a box and it moves/flips away.
Re: rotating an object to the direction it goes
Reply #6 - Jun 3rd, 2007, 11:52pm
 
You don;t need to draw any boxes or anything to control the angle, you just need to have your own x/y stuff to do it...

Code:
 import saito.objloader.*;
import processing.opengl.*;

float x,y;
OBJModel cube;

void setup() {
size(1024, 768, OPENGL);
cube = new OBJModel(this);
cube.load("MyOBJ.obj");
}

void draw()
{
//set your X/Y values somehow...
pushMatrix();
translate(x,y,0);
rotateZ(atan2(y-height/2,x-width/2));//the Z is the axis
//to rotate around.
cube.draw();
popMatrix();
}
Re: rotating an object to the direction it goes
Reply #7 - Jun 4th, 2007, 1:22pm
 
Thx again, after adding your suggested code, it is almost what i'm trying to do except that the cube is not rotating the right way (according to its width/height position).

Re: rotating an object to the direction it goes
Reply #8 - Jun 6th, 2007, 1:22pm
 
Hi, i'm still stuck,

let's say, for example, the cube is in the middle of the screen and if it is moved to the top (width/2, height--), i'd like to make it rotate to this direction.

Its rotation speed will be dependent to it's displacement speed.

Anybody has an idea ?

Thanks.
Re: rotating an object to the direction it goes
Reply #9 - Jun 6th, 2007, 1:28pm
 
Store the speed, and do the atan2 on the speed, not the position.
Page Index Toggle Pages: 1