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 › Trouble understanding get(x,y)
Page Index Toggle Pages: 1
Trouble understanding get(x,y) (Read 565 times)
Trouble understanding get(x,y)
Jun 17th, 2007, 12:24am
 
Hi,
it may seem strange, that a function as simple as get() causes discordance in my mind, but I think that maybe the problem lies somewhere else.
I'm using processing for two or three days now and in my little first draft of a first program, which I've uploaded here http://fnork.ath.cx/boincview.tar.gz I want the Donuts to know if the cursor is over them. The only idea I have had so far was to check whether the color under the cursor contains any red and if so to calculate which donut is the nearest. From this problem two alternating questions derive:
1. Why does red(get(mouseX,mouseY)) return these (for me) unexpected results (red on black areas, black on red areas or vice versa), or
2. How to solve this problem more elegantly?

Thanks a lot in advance!
Re: Trouble understanding get(x,y)
Reply #1 - Jun 17th, 2007, 1:02pm
 
Could you post the code here please? I'm having trouble opening the archive.

Re: Trouble understanding get(x,y)
Reply #2 - Jun 17th, 2007, 1:24pm
 
boincview.pde:
Code:

import damkjer.ocd.*;
import processing.opengl.*;

Camera cam;
MouseWheelEventDemo wheel;
ArrayList donuts = new ArrayList();

public static void main( String args[] ) {
PApplet.main( new String[] { "--present", "boincview" } );
}

void setup()
{
size(600,600,OPENGL);
colorMode(RGB,255,255,255,255);
frameRate(24);
wheel = new MouseWheelEventDemo();
cam = new Camera(this);
this.donuts.add(new Donut(0.95,10000.0));
this.donuts.add(new Donut(0.75,10000.0));
noStroke();
noSmooth();
}

void draw() {
background(0);
cam.feed();
lights();
for(int i=0; i<this.donuts.size(); i++){
Donut d = (Donut) this.donuts.get(i);
d.draw();
}
println(red(get(mouseX,mouseY))); // Testing
if(mouseX < 32) cam.pan(-(32-mouseX)/1000.0);
if(mouseX > width-32) cam.pan((32-(width-mouseX))/1000.0);
if(mouseY < 32) cam.tilt(-(32-mouseY)/1000.0);
if(mouseY > height-32) cam.tilt((32-(height-mouseY))/1000.0);
}

public class MouseWheelEventDemo implements MouseWheelListener {
int notches = 0;
public MouseWheelEventDemo() {
addMouseWheelListener(this);
}
public void mouseWheelMoved(MouseWheelEvent e) {
cam.zoom(-e.getWheelRotation()/20.0);
}
}


Donut.pde:
Code:

public class Donut {
String computer = "";
float fraction = 0.0;
float remaining = 0.0;
float radius = 0.0;
float posX = 0;
float posY = 0;
float posZ = 0;
boolean isHovering = false;

public Donut(float frac, float remain){
this.fraction = frac;
this.remaining = remain;
this.radius = remaining/(1-fraction)/3600;
this.posX = random(-600,600);
this.posY = random(-600,600);
this.posZ = random(-100,100);
//println(screenX(this.posX,this.posY,this.posZ) + " " + screenY(this.posX,this.posY,this.posZ)); // Testing
}

void draw(){
pushMatrix();
translate(this.posX,this.posY,this.posZ);
float theta;
int full = floor(this.fraction*16);
int transp = 255;
pushMatrix();
for(int i=0;i<16;i++){
if(i==full) transp = 64;
theta = TWO_PI/16*i;
translate(cos(theta)*this.radius,sin(theta)*this.radius,0);
fill(200+round(sin(2*theta)*40),0,0,transp);
sphere(TWO_PI*this.radius/8);
}
popMatrix();
popMatrix();
}

public float getX(){
return posX;
}

public float getY(){
return posY;
}

public float getZ(){
return posZ;
}

public float getRadius(){
return radius;
}

}


Done.
Thanks for your interest.
Re: Trouble understanding get(x,y)
Reply #3 - Jun 17th, 2007, 2:09pm
 
Using loadPixels(); red(pixels[mouseY*width+mouseX]); instead the results are fitting my expectations, but the program is dreadfully slow - even with dri.
Re: Trouble understanding get(x,y)
Reply #4 - Jun 17th, 2007, 2:14pm
 
To speed up the color grabbing use pixels[] with bit shifting instead.

http://processing.org/reference/leftshift.html
Re: Trouble understanding get(x,y)
Reply #5 - Jun 17th, 2007, 2:30pm
 
Do you mean pixels[mouseY*width+mouseX] >> 16 & 0xFF; ?
Still only 2-3 fps...

Edit:
With 1 Donut and without any color detection the framerate is about 14 fps, with 2 Donuts about 8 fps, with color detection and the actual code up there about 2.3 fps, with shifting about 2.6 fps...
Page Index Toggle Pages: 1