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 › need help with joystick
Page Index Toggle Pages: 1
need help with joystick (Read 391 times)
need help with joystick
Jan 28th, 2008, 4:47pm
 
hello everybody,
i'm newbie in processing and need some help with my project.
i want to controll the directions of a litte triangle with a joystick by proControll lib.
the triangle should have some trig but right now its just twiggling round in the upper left corner and not following the joystick and i don't know what i made wrong.

I would be pleased about some help.


------ Code ------

import procontroll.*;
import java.io.*;

ControllIO controll;
ControllDevice device;
ControllStick stick;
ControllButton button;

// triangle
float predCntrX, predCntrY;
float predX[] = new float[3];
float predY[] = new float[3];
float predLen = 8.0;
float predAng, predRot;
float easing = .05;

void setup(){
 size(400,400);
 predCntrX = width/2;
 predCntrY = height/2;
 smooth();
 
 //get controller instance
 controll = ControllIO.getInstance(this);

 //list controller
 controll.printDevices();

 //controller id 2
 device = controll.getDevice(2);
 println("STICKS:");
 device.printSticks();
 println("SLIDERS:");
 device.printSliders();
 
 
 device.setTolerance(0.05f);
 
 ControllSlider sliderX = device.getSlider(0);
 ControllSlider sliderY = device.getSlider(1);
 
 stick = new ControllStick(sliderX,sliderY);

 
 //repaint background
  fill(255, 40);
  rect(0, 0, width, height);
}

float totalX = (width/2);
float totalY = (height/2);

void draw(){
 background(255);
 
 //get x/y distance between stick and triangle
 float deltaX = (stick.getX()-predCntrX);
 float deltaY = (stick.getY()-predCntrY);
 
 //decelerate
 deltaX *= easing;
 deltaY *= easing;
 
 predCntrX += deltaX;
 predCntrY += deltaY;
 
 // orient triangle
 predRot = atan2(deltaY, deltaX);
 
 totalX = constrain(deltaX + stick.getX(),10,width-10);
 totalY = constrain(deltaY + stick.getY(),10,height-10);
 

  // draw triangle
 createPredatoryTriangle();
 
 //println("X: "+stick.getX());
 
}
void createPredatoryTriangle(){
 // draw triangle with some trig
 fill(0);
 beginShape();
 for (int i=0; i<3; i++){
   predX[i] = predCntrX+cos(radians(predAng)+predRot)*predLen;
   predY[i] = predCntrY+sin(radians(predAng)+predRot)*predLen;
   vertex(predX[i], predY[i]);
   predAng += 120;
 }
 endShape(CLOSE);
}
Page Index Toggle Pages: 1