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 › Unexpected acceleration... :S
Page Index Toggle Pages: 1
Unexpected acceleration... :S (Read 903 times)
Unexpected acceleration... :S
Sep 10th, 2009, 1:49am
 
I'm making this new sketch and I want to make the camera keyboard-controlled.

So trying to put it toguether I wanted to make a camera easing algorithm to make it move softly when the key is pressed and also when it is released.

But the damn thing does not work! :S once the key is pressed the camera does not stop spinning and, even worse, only accelerates from that point on.

Here is the code for the camera movement:

Quote:
float camPositionX = width/2,camPositionY = height/2,camPositionZ = height/2;
float camRotationX = 0, camRotationY = 0, camRotationZ = 0;
float camAngleX = 0, camAngleY = 0, camAngleZ = 0;
boolean rotationRightOn = false;
boolean rotationLeftOn = false;



void setup() {
  size(600,600,P3D);
  frameRate(30);
}



void draw() {
  getPOV();
  background(40,127,255);
}


void keyPressed() {
  switch(keyCode){
    case(LEFT):
    rotationLeftOn = true;
    break;
    case(RIGHT):
    rotationRightOn = true;
    break;
  }
}

void keyReleased() {
  switch(keyCode){
    case(LEFT):
    rotationLeftOn = false;
    break;
    case(RIGHT):
    rotationRightOn = false;
    break;
  }
}

void getPOV() {
  if (rotationLeftOn  && camRotationZ > -2 ) {
    camRotationX -= 0.1;
  }
  if (!rotationLeftOn && camRotationZ <= 0 ) {
    camRotationZ += 0.1;
  }
  //the same from above for the rest of coordinates.
  camAngleX += camRotationX;;
  beginCamera();
  translate(camPositionX,camPositionY,camPositionZ);
  rotateX(radians(camAngleX));
  rotateY(radians(camAngleY));
  rotateZ(radians(camAngleZ));
  endCamera();
}



So... WHAT THE HELL I'M I DOING WRONG?!?!?! Tongue

Thank you for your help Smiley
Re: Unexpected acceleration... :S
Reply #1 - Sep 10th, 2009, 3:45am
 
First, I see a potential inconsistency:
Code:
  if (rotationLeftOn  && camRotationZ > -2 ) {
camRotationX -= 0.1;
}
if (!rotationLeftOn && camRotationZ <= 0 ) {
camRotationZ += 0.1;
}

You do most operations on camRotationZ but you have one on camRotationX. Is it intended?

Beside, if rotationLeftOn is true you do one rotation, and if false you do another (the opposite?). So it never ends.
Re: Unexpected acceleration... :S
Reply #2 - Sep 10th, 2009, 7:06am
 
Aye, you don't use the right rotation boolean at all here...but the constant acceleration is happening because you're incrementing a "rotation" number by .01, and then *adding* that entire number to the current angle.  So, the formula you are ending up with is:

angle = angle*2 + .01

when you just want: if (rotating to the right) angleX += .01;

You don't appear to use the CamRotationZ variable at all once it's incremented, either...

--Ben
Re: Unexpected acceleration... :S
Reply #3 - Sep 12th, 2009, 2:54pm
 
I managed to try some approaches to the calculation and found the error.

I did'nt know that the camera enters it's own matrix mode when using beginCamera(). when you use beginCamera() rotation is expressed just in terms of adding a little value, if you add something more it accelerates. that's kinda useful but not for my app.

the existence of camRotationX is to have a easing variable to add to the rotation value of the camera, I got aout of beginCamera approach, because it doesn't allow to put something in fromt of the camera. Is there a method to get the camera's position and view angle?

Another thing, while I was investigating the camera stuff, I wondered if I could make a 3D battleship game out of this.

do you BenHem have any thoughts on this? I saw you made your own the other day. So I think some Vector math could be of very good help when changing the sgip and cam position at the same time.

Any advice?
Re: Unexpected acceleration... :S
Reply #4 - Sep 12th, 2009, 5:43pm
 
Hey,
more power to you if you have solved the problem some other way.  But if it's persisting, I truly would suspect these lines:

if (rotationLeftOn  && camRotationZ > -2 ) {
   camRotationX -= 0.1;
// ^ this is not held in check, because the "if" checks camRotZ, but camRotX is being incremented
 }
 camAngleX += camRotationX;
// ^ this can run rampant, because camRotX is not held in check

Sorry, I'm not sure what you're doing, or what project of mine you mean, but a few things that may help:
angle = (angle+incrementAmount) % TWO_PI; is a good way to increment an angle while keeping its value in the 0-to-TWO_PI range.
I recommend staying with radians all the way, to streamline the code.
I usually shift the entire scene instead of shifting the camera, where possible, and only render the objects that correspond to the visible scene at that position.
Check out the "Camera" section of reference for all the camera commands.

--Ben
Re: Unexpected acceleration... :S
Reply #5 - Sep 12th, 2009, 7:48pm
 
Oh man. My code sucks, it's comepletely diferent now, nevermind that crap haha! Cheesy

Yeah! I found out thet's quite easier to move the scene rather than the camera.

I saw your Chrystal Cave game and got inspired. I wanted to do something more dynamic than that, like some dogfighting and aerobatics.

Something that may require a good Physic library.

I already programed the ground and it's drawing rules, like distance from your own ship and that kind of stuff.

Howerver I do not know how to make a good movement of the scenario, because I want to give control of all three dimensions for the ship movement.

To get And Idea of the control level I wanto to get check this out: http://homepage3.nifty.com/ysflight/aircraftsoft/aircraftsofte.html#YSFLIGHT

That's the kind of simulation I want to achieve, even better if I can, but there is the sad thing of me not knowing how to operate vectors to set the movement, angle and rotation for the ships and the ground an everything Tongue

Is there a library to make movement easier, so I do not have to build the whole thing myself?

Any advice will be very welcome Smiley
Page Index Toggle Pages: 1