Rotating with an object

I have a question that is bugging me at the moment and I’m too tired to go find all the answers on the web, so here I go…

I’m making a third person type game and I have almost the whole behavior done for movement all I need is to get the rotation done. I can rotate my model just fine, but I want to rotate the camera with the model, so the camera is always looking and the back of the model. I already have the camera positioned, I just need the right code to give me the camera rotation.

Any help? ;D

here it goes:


Transform3D t = new Transform3D();
t.lookAt(new Point3d(0, parent.arena.length, parent.arena.length*viewIncidence), new Point3d(), new Vector3d(0, 0, 1));
t.invert();
u.getViewingPlatform().getViewPlatformTransform().setTransform(t);

in the code t.lookAt(…) is used, this is an intuitive way to get the right position and right view direction, but of course you can change the transform how you prefer.

Another thing that could maybe be usefull for you is the WakeupOnTransformChange for a behavior (if the man falls, you camera must move with the guy even without pressing on keys). On the other hand, if movements are fully foreseeable, moving body and camera together is of course better, and in that case, they could share a common TG.

cheers.

oh yeah yeah, a common TG would be very usefull:
A TG for the body (pos+rot), and attached to it a TG with a relative pos from body center to head, directed in th front. And if it is possible to attach to this one the viewing platform, you’ve done it! (And second TG will be static, the camera (view platform) will be “fixed” on the body’s head!)
…but i don’t know how to attach the view platform there, i think it can be made.

I did something this a little while ago- the code here is part of a behaviour that rotates a scene when a user presses the left or right buttons on the interface- this isn’t completely transparent but hopefully you can get some idea what is going on. I have only included the relevant bits of code and it’s kind of a bit of a draft version.


public void processStimulus(java.util.Enumeration criteria) 
{

            double ychange=.8;
            if (Direction==0) 
            {  
                rotationAngle -= ychange * rotYMul; //xchange * rotXMul;
            }
            else 
            {
                rotationAngle += ychange * rotYMul;//xchange * rotXMul;

            }
 
            integrateTransforms();
            wakeupOn(runCriterion);
     }
}

protected void integrateTransforms()
    {
          targetTG.getTransform( targetTransform );
          targetTransform.get( currentPos );
          
          double nowX = currentPos.x;
          double nowY = currentPos.z;

          nowX = nowX-rotationCentre.x;
          nowY = nowY-rotationCentre.z;
          
         
          double lenVec = distanceFromCentre;
          nowY = (Math.sin(rotationAngle)*lenVec);
          nowX = -Math.cos(rotationAngle)*lenVec; 

          nowX = nowX+ rotationCentre.x;
          nowY = nowY+ rotationCentre.z;
          
          currentPos.x = nowX;
          currentPos.z = nowY;    
                 
          targetTransform.set(currentPos);
          targetTransform.lookAt(new Point3d(currentPos.x, currentPos.y, currentPos.z), rotationCentre,  new Vector3d(0, 1, 0) );
          targetTransform.invert();
         
          targetTG.setTransform(targetTransform);   
    }

Well, I tired your code breakfast, but it doesn’t turn with the model all the way. It only turns one distance and that is it. Here is the code that I’m using, some of it should look very familiar to you 8)


private void initCamera()
            {
                  camTrans = new Transform3D();
                  camVec = new Vector3d();     
                  camTG.getTransform(camTrans);
                  camTrans.get(camVec); 
            }
    
            private void translateCamera(Transform3D ourTrans)
            {
            Vector3d ourVec = new Vector3d();
            Vector3d up = new Vector3d(0, 1, 0);
            ourTrans.get(ourVec);
                  ourVec.add(new Vector3d(-1.0d, 1.0d, 0.0d));
            
            Point3d cam = new Point3d();
            Point3d man = new Point3d();

            camTrans.setTranslation(ourVec);
                  ourTrans.transform(man);
            camTrans.transform(cam);
                  man.y = man.y +1.0;

                  camTrans.lookAt(cam, man, up);
            camTrans.invert(); 

            camTG.setTransform(camTrans);
            }

            private void rotateCamera(Transform3D ourTrans)
            { 
                  Vector3d currentPos = new Vector3d(); 
                  camTrans.get( currentPos );
      
                  double nowX = currentPos.x; 
                  double nowY = currentPos.z; 

                  Point3d rotationCentre = new Point3d(); 
                  ourTrans.transform( rotationCentre );
 
                  nowX = nowX-rotationCentre.x; 
                  nowY = nowY-rotationCentre.z; 
      
     
                  double lenVec = 1.0d; 
                  nowY = (Math.sin(rotateYAmount * speed*2)*lenVec); 
                  nowX = (-Math.cos(rotateYAmount * speed*2)*lenVec);  
 
                  nowX = nowX+ rotationCentre.x; 
                  nowY = nowY+ rotationCentre.z; 
      
                  currentPos.x = nowX; 
                  currentPos.z = nowY;     
        
                  camTrans.set(currentPos); 
                  camTrans.lookAt(new Point3d(currentPos.x, currentPos.y, currentPos.z), 
                                          new Point3d(rotationCentre.x, rotationCentre.y+1.0d, rotationCentre.z),  new Vector3d(0, 1, 0)); 
                  camTrans.invert(); 
     
                  camTG.setTransform(camTrans);    
            }

The rest of my behaviour varied the wakeup criterion so that it used a WakeupOnElapsedFrames as long as it was receiving the input from the buttons ( not really a good idea if you are not moderating the angle of rotation according to the elapsed time between frames, but it didn’t matter in my case ) so that once per frame we are updating the angle by 0.8 * rotYMul (not at work so I don’t remember what the value of that was, but it would be the value that would be moderated by the framerate).

The other wakeupcriterion is a behaviour post raised by the button eventlisteners, telling it to start the movement behaviour.

Got it fixed. I didn’t add the previous angle to the new angle, so now it works. thanks