I have a game character swinging on a rope. I want him to crawl up when the mouse is above him and down when the mouse is below him.
But I want this to work from the character's point of view, which fails to work when he is swinging to the left because at PI radians you step immediately into -PI radians. This means it works fine doing < or > when swinging to the right but when he swings to the left the mouse always registers as above at -PI radians or less.
Example:
Code:
float theta;
float x,y;
void setup(){
size(200, 200);
smooth();
strokeWeight(2);
x = y = 100;
theta = HALF_PI;
}
void draw(){
background(255);
stroke(0, 0, 255);
line(0, y, x, y);
float dx = mouseX - x;
float dy = mouseY - y;
float mTheta = atan2(dy, dx);
if(mouseBelow(mTheta)){
stroke(0,155,0);
}
else {
stroke(255,0,0);
}
line(x, y, mouseX, mouseY);
// projected normals
float vx = cos(theta) * 50;
float vy = sin(theta) * 50;
// right hand normal projected
float rx = -vy;
float ry = vx;
// left hand normal projected
float lx = vy;
float ly = -vx;
stroke(0);
line(x + lx, y + ly, x + rx, y + ry);
line(x, y, x + vx, y + vy);
if(keyPressed){
switch(keyCode){
case LEFT:
theta -= 0.1;
break;
case RIGHT:
theta += 0.1;
break;
}
}
}
boolean mouseBelow(float mTheta){
float minTheta = theta - HALF_PI;
float maxTheta = theta + HALF_PI;
println("min:"+minTheta+" max:"+maxTheta+" theta:"+mTheta);
return mTheta > minTheta && mTheta < maxTheta;
}
How can I get a relative angle check that overcomes the jump from +PI radians to -PI radians?