Crystal Space
Welcome, Guest. Please login or register.
April 16, 2014, 11:23:37 pm

Login with username, password and session length
Search:     Advanced search
8990 Posts in 2037 Topics by 7582 Members
Latest Member: Diretz5
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  General Crystal Space Discussion
| | |-+  rotate the cmera with mouse
« previous next »
Pages: [1] 2 Print
Author Topic: rotate the cmera with mouse  (Read 5887 times)
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« on: December 15, 2005, 11:37:33 am »

I try make rotate the camera with mouse. My program running error when I use the: int x = mouse->GetLastX(); or int y = mouse->GetLastY(); methods...

.......
int w = g3d->GetDriver2D ()->GetWidth()/2; //screen width
int h = g3d->GetDriver2D ()->GetHeight()/2; //screen high

int x = mouse->GetLastX(); //get the cursor moving vertical
int y = mouse->GetLastY(); //get the cursor moving horizontal

  if (hasfocus) //if the cursor focused my program
  {
  view->GetCamera ()->GetTransform ().RotateThis (CS_VEC_TILT_UP, (y-h) * 0.01); //horizontal rotating
  view->GetCamera ()->GetTransform ().RotateOther (CS_VEC_ROT_RIGHT, (x-w) * 0.01); //vertical rotating
  g3d->GetDriver2D ()->SetMousePosition (w, h); //cursor position
  moved |= (y-h);
  moved |= (x-w);
  }
.........
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1704


View Profile
« Reply #1 on: December 15, 2005, 11:44:20 am »

I try make rotate the camera with mouse. My program running error when I use the: int x = mouse->GetLastX(); or int y = mouse->GetLastY(); methods...

.......
int w = g3d->GetDriver2D ()->GetWidth()/2; //screen width
int h = g3d->GetDriver2D ()->GetHeight()/2; //screen high

int x = mouse->GetLastX(); //get the cursor moving vertical
int y = mouse->GetLastY(); //get the cursor moving horizontal

  if (hasfocus) //if the cursor focused my program
  {
  view->GetCamera ()->GetTransform ().RotateThis (CS_VEC_TILT_UP, (y-h) * 0.01); //horizontal rotating
  view->GetCamera ()->GetTransform ().RotateOther (CS_VEC_ROT_RIGHT, (x-w) * 0.01); //vertical rotating
  g3d->GetDriver2D ()->SetMousePosition (w, h); //cursor position
  moved |= (y-h);
  moved |= (x-w);
  }
.........

Did you correctly initialize the 'mouse' variable? Can I see that code?

Greetings,
Logged
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« Reply #2 on: December 15, 2005, 11:59:14 am »

#include "main.h"

CS_IMPLEMENT_APPLICATION

////
Viewer *viewer_; //osztalyra mutato pointer
////

//------------------------------------------------------------------------------
Viewer::Viewer ()
{
  SetApplicationName ("TT3D_Viewer_1_0.Viewer");
}

Viewer::~Viewer ()
{
}



//------------------------------------------------------------------------------
void Viewer::ProcessFrame ()
{
    csTicks elapsed_time = vc->GetElapsedTicks (); //ido eloallitasa

    float speed = (elapsed_time / 1000.0) * (0.06 * 20); //belso ora inicializalasa
    bool moved = false;

////
  int w = g3d->GetDriver2D ()->GetWidth()/2;  //eger kurzor viszintes pozicio lekerdezese
  int h = g3d->GetDriver2D ()->GetHeight()/2; //eger kurzor fuggoleges pozicio lekerdezese

//int x = 0;
//int y = 0;
  int x = mouse->GetLastX();
  int y = mouse->GetLastY();

  if (hasfocus) //ha a program ablakaban van a kurzor
  {
int x = mouse->GetLastX();

  view->GetCamera ()->GetTransform ().RotateThis (CS_VEC_TILT_UP, (y-h) * 0.01); //fuggoleges forgatas
  view->GetCamera ()->GetTransform ().RotateOther (CS_VEC_ROT_RIGHT, (x-w) * 0.01); //vizszinte forgatas
  g3d->GetDriver2D ()->SetMousePosition (w, h); //kurzor folyamatos helyzete
  moved |= (y-h);
  moved |= (x-w);
  }
////

    //mozgatas a kurzor gombokkal vizszintesen, realtime
    if (kbd->GetKeyState (CSKEY_UP))
    {
        view->GetCamera ()->Move (CS_VEC_FORWARD * speed * 4.0);
        moved = true;
    }

    if (kbd->GetKeyState (CSKEY_DOWN))
    {
        view->GetCamera ()->Move (CS_VEC_BACKWARD * speed * 4.0);
        moved = true;
    }

    if (kbd->GetKeyState (CSKEY_LEFT))
    {
        view->GetCamera ()->Move (CS_VEC_LEFT * speed * 4.0);
        moved = true;
    }

    if (kbd->GetKeyState (CSKEY_RIGHT))
    {
        view->GetCamera ()->Move (CS_VEC_RIGHT * speed * 4.0);
        moved = true;
    }

    g3d->SetPerspectiveAspect (g3d->GetDriver2D ()->GetHeight ());
    g3d->SetPerspectiveCenter (g3d->GetDriver2D ()->GetWidth ()/2,
        g3d->GetDriver2D ()->GetHeight ()/2);

    view->Draw ();
}



//------------------------------------------------------------------------------
void Viewer::FinishFrame ()
{
    g3d->FinishDraw ();
    g3d->Print (0);
}


////
/*
bool Viewer::HandleEvent (iEvent& ev)
{

  if (ev.Type == csevBroadcast && csCommandEventHelper::GetCode(&ev) == cscmdFocusChanged)
  {

    hasfocus = (bool)csCommandEventHelper::GetInfo(&ev);
    if (hasfocus)
    {
//      int w = g3d->GetDriver2D ()->GetWidth()/2;
//      int h = g3d->GetDriver2D ()->GetHeight()/2;
//      g3d->GetDriver2D ()->SetMousePosition (w, h);
      g3d->GetDriver2D()->SetMouseCursor (csmcNone);
    }
    else
    {
      g3d->GetDriver2D()->SetMouseCursor (csmcArrow);
    }

  }

  else if (ev.Type == csevBroadcast && csCommandEventHelper::GetCode(&ev) == cscmdProcess)
  {
    viewer_->ProcessFrame ();
    return true;
  }
  else if (ev.Type == csevBroadcast && csCommandEventHelper::GetCode(&ev) == cscmdFinalProcess)
  {
    viewer_->FinishFrame ();
    return true;
  }


  else if ((ev.Type == csevKeyboard) &&
    (csKeyEventHelper::GetEventType (&ev) == csKeyEventTypeDown))
  {
    switch (csKeyEventHelper::GetCookedCode (&ev))
    {
    case CSKEY_ESC: //kilepes
      {
        csRef<iEventQueue> q (CS_QUERY_REGISTRY (object_reg, iEventQueue));
        if (q) q->GetEventOutlet()->Broadcast (cscmdQuit);
        return true;
      }
  //  case '1':
//      CreateBox ();
      break;
    }
  }

  return false;
}


bool Viewer::SimpleEventHandler (iEvent& ev)
{
  return viewer_->HandleEvent (ev);
}
*/
////



//------------------------------------------------------------------------------
bool Viewer::OnInitialize(int argc, char* argv[])
{
    if (!csInitializer::RequestPlugins(GetObjectRegistry(),
        CS_REQUEST_VFS,
        CS_REQUEST_OPENGL3D,
        CS_REQUEST_ENGINE,
        CS_REQUEST_FONTSERVER,
        CS_REQUEST_IMAGELOADER,
        CS_REQUEST_LEVELLOADER,
        CS_REQUEST_REPORTER,
        CS_REQUEST_REPORTERLISTENER,
        CS_REQUEST_END))
        return ReportError("Failed to initialize plugins!");

  ////
  /*
  view = csPtr<iView> (new csView (engine, g3d));
  view->GetCamera ()->SetSector (room);
  view->GetCamera ()->GetTransform ().SetOrigin (csVector3 (0, 5, 0));
  view->GetCamera ()->GetTransform ().LookAt (csVector3(5,-5,20), csVector3(0,1,0));
  csRef<iGraphics2D> g2d = g3d->GetDriver2D ();
  view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());

  hasfocus = true;
  int w = g3d->GetDriver2D ()->GetWidth()/2;
  int h = g3d->GetDriver2D ()->GetHeight()/2;
  g3d->GetDriver2D ()->SetMousePosition (w, h);
  */
  ////

    if (!RegisterQueue(GetObjectRegistry()))
    return ReportError("Failed to set up event handler!");

    //iNativeWindow* nw = g2d->GetNativeWindow ();
    //if (nw) nw->SetTitle ("TT3D-Viewer 1.0"); //ablak fejlec

  return true;
}



//------------------------------------------------------------------------------
void Viewer::OnExit()
{
}



//------------------------------------------------------------------------------
bool Viewer::Application()
{
    if (!OpenApplication(GetObjectRegistry()))
    return ReportError("Error opening system!");

    g3d = CS_QUERY_REGISTRY(GetObjectRegistry(), iGraphics3D);
    if (!g3d) return ReportError("Failed to locate 3D renderer!");

    engine = CS_QUERY_REGISTRY(GetObjectRegistry(), iEngine);
    if (!engine) return ReportError("Failed to locate 3D engine!");

    vc = CS_QUERY_REGISTRY(GetObjectRegistry(), iVirtualClock);
    if (!vc) return ReportError("Failed to locate Virtual Clock!");

    kbd = CS_QUERY_REGISTRY(GetObjectRegistry(), iKeyboardDriver);
    if (!kbd) return ReportError("Failed to locate Keyboard Driver!");

    loader = CS_QUERY_REGISTRY(GetObjectRegistry(), iLoader);
    if (!loader) return ReportError("Failed to locate Loader!");

    view.AttachNew(new csView (engine, g3d));
    iGraphics2D* g2d = g3d->GetDriver2D ();

    view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());
    engine->SetLightingCacheMode (0);

    CreateRoom();

    engine->Prepare ();

    rotY = rotX = 0;

    view->GetCamera ()->SetSector (room);
    view->GetCamera ()->GetTransform ().SetOrigin (csVector3 (0, 5, -3));

    Run();

    return true;
}



//------------------------------------------------------------------------------
void Viewer::CreateRoom ()
{
    if (!loader->LoadTexture ("stone", "/lib/std/stone4.gif"))
    ReportError("Error loading 'stone4' texture!");

    iMaterialWrapper* tm = engine->GetMaterialList ()->FindByName ("stone");

    room = engine->CreateSector ("room");

    csRef<iMeshWrapper> walls (engine->CreateSectorWallsMesh (room, "walls"));
    csRef<iThingState> ws =
    SCF_QUERY_INTERFACE (walls->GetMeshObject (), iThingState);
    csRef<iThingFactoryState> walls_state = ws->GetFactory ();
    walls_state->AddInsideBox (csVector3 (-5, 0, -5), csVector3 (5, 20, 5));
    walls_state->SetPolygonMaterial (CS_POLYRANGE_LAST, tm);
    walls_state->SetPolygonTextureMapping (CS_POLYRANGE_LAST, 3);

    csRef<iLight> light;
    iLightList* ll = room->GetLights ();

    light = engine->CreateLight(0, csVector3(-3, 5, 0), 10, csColor(1, 0, 0));
    ll->Add (light);

    light = engine->CreateLight(0, csVector3(3, 5,  0), 10, csColor(0, 0, 1));
    ll->Add (light);

    light = engine->CreateLight(0, csVector3(0, 5, -3), 10, csColor(0, 1, 0));
    ll->Add (light);
}



//------------------------------------------------------------------------------
int main (int argc, char* argv[])
{
  return csApplicationRunner<Viewer>::Run (argc, argv);
}
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1704


View Profile
« Reply #3 on: December 15, 2005, 12:00:42 pm »

There is nothing in that code you pasted that sets the 'mouse' variable. Of course that can't work then. You need to set the mouse from the object registry. There are some examples in the CS code.

Greetings,
Logged
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« Reply #4 on: December 15, 2005, 12:18:04 pm »

mouse = CS_QUERY_REGISTRY (object_reg, iMouseDriver);

Okay. Wink
« Last Edit: January 07, 2006, 04:07:34 pm by [SOM]Roberto » Logged
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« Reply #5 on: January 07, 2006, 04:27:11 pm »

Next probelm with mouse moving: For example I moving left and I rotating the camera direction a point always with mouse. My scene trembling. If I make look_at position and move left-right up-down with mouse, scene trembling. But I make cursor moving, scene not trembling. I using cursor position input for mouse moving.

How can I make mouse imput directly?

My code in process frame method:

    int w = g3d->GetDriver2D()->GetWidth()/2;
    int h = g3d->GetDriver2D()->GetHeight()/2;
    int x = mouse->GetLastX();
    int y = mouse->GetLastY();

    if (hasfocus)
    {
        if (tetbox_camera == true)     //moving on sphere
        {
            //following the target point
            csVector3 up (0, 1, 0);
            const csVector3& camerapos = view->GetCamera()->GetTransform().GetOrigin();
            csVector3 lookAtpos (0, 1, 0);
            view->GetCamera()->GetTransform().LookAt(lookAtpos - camerapos, up);

            //moving around the point
            if (x)
            {
                view->GetCamera()->Move (CS_VEC_RIGHT * (-(x-w)) * 0.08);
            }

            if (y)
            {
                view->GetCamera()->Move (CS_VEC_UP * ((y-h)) * 0.08);
            }
        }
        else                 //simple camera rotating
        {
            //horizontal camera rotating
            view->GetCamera()->GetTransform ().RotateOther (CS_VEC_ROT_RIGHT, (x-w) * 0.008);

            //vertical camera rotating
            view->GetCamera()->GetTransform().RotateThis (CS_VEC_TILT_UP, -(y-h) * 0.008);
        }

       g3d->GetDriver2D()->SetMousePosition (w, h); //set position always render
        g3d->GetDriver2D()->SetMouseCursor (csmcNone);
Logged
bernardofd
Jr. Member
**
Posts: 74


59948732 bernardofd@click21.com.br
View Profile WWW
« Reply #6 on: January 12, 2006, 03:46:33 am »

After a bit of work and lot of errors, I achieved the mouselook code in CS. Unfortunately, I do not have the code right now.

But I think this code is in our project's SF page, in CVS.

Try here: Estrada Real Digital.

When I get the right code, I'll post it here.
Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« Reply #7 on: January 13, 2006, 03:06:00 pm »

Thaks, but this SF project is empty! Smiley
« Last Edit: January 13, 2006, 03:07:40 pm by [SOM]Roberto » Logged
jorrit
Administrator
Hero Member
*****
Posts: 1704


View Profile
« Reply #8 on: January 13, 2006, 03:13:37 pm »

Thaks, but this SF project is empty! Smiley

No it is not. Works fine for me and you can get the source there (with CVS).

Greetings,
Logged
bernardofd
Jr. Member
**
Posts: 74


59948732 bernardofd@click21.com.br
View Profile WWW
« Reply #9 on: January 13, 2006, 03:23:36 pm »

The only problem with the project is that the map direcory and file is missing. It's just an minimal code, with an FPS schema, collision, gravity, WASD movement and mouse freelook.

We will upload our map file as soon as it get ready (It is now going through a lot of changes).
Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« Reply #10 on: January 13, 2006, 03:32:13 pm »

The only problem with the project is that the map direcory and file is missing. It's just an minimal code, with an FPS schema, collision, gravity, WASD movement and mouse freelook.

We will upload our map file as soon as it get ready (It is now going through a lot of changes).

Oh I see. Thanks. SmileySmileySmiley
Logged
[SOM]Roberto
Jr. Member
**
Posts: 79


View Profile Email
« Reply #11 on: January 21, 2006, 07:21:34 pm »

Well this erd project using mouse event, but it not compatible low FPS speed. But this mouse event trembling too.
I imlemented this code, not trembling my scene:

 if (hasfocus)
        {
                csVector3 up (0, 1, 0);
                const csVector3& camerapos = view->GetCamera()->GetTransform().GetOrigin();
                csVector3 lookAtpos (0, 1, 0);
               ////->
                view->GetCamera()->GetTransform().LookAt(lookAtpos - camerapos, up);
               ////->

                if (x)
                {
                    view->GetCamera()->Move (CS_VEC_RIGHT * -(x-w) * 0.008);
                }

                if (y)
                {
                    view->GetCamera()->Move (CS_VEC_UP * (y-h) * 0.008);
                }
              ////->
               view->GetCamera()->GetTransform().LookAt(lookAtpos - camerapos, up);               
             ////->
            }
« Last Edit: January 21, 2006, 08:03:37 pm by [SOM]Roberto » Logged
kornerr
Full Member
***
Posts: 101


347968356
View Profile WWW Email
« Reply #12 on: March 17, 2006, 12:04:26 pm »

I want smooth camera turning with a mouse. So far I have the following which produces very sharp turning:
Code:
    iCamera *camera = view->GetCamera ();
    float turn_coef = 0.07;

    if (md->GetLastX () > window_width / 2)
        y_rot += turn_coef;
    if (md->GetLastX () < window_width / 2)
        y_rot -= turn_coef;
    if (md->GetLastY () > window_height / 2)
        x_rot -= turn_coef / 2;
    if (md->GetLastY () < window_height / 2)
        x_rot += turn_coef / 2;

    g2d->SetMousePosition (window_width / 2, window_height / 2);

    csMatrix3 rot = csXRotMatrix3 (x_rot) * csYRotMatrix3 (y_rot);
    csOrthoTransform ot (rot, camera->GetTransform ().GetOrigin ());
    camera->SetTransform (ot);

How to make it turn smoothly?

Thanks.
Logged

kornerr
Full Member
***
Posts: 101


347968356
View Profile WWW Email
and
« Reply #13 on: March 17, 2006, 12:54:33 pm »

And how to make camera movement with a mouse similar to real games: move left-right and forward-backward with no dependence of how camera is turned (move camera always parallel to the floor)? Should I move it using MoveWorld (...)? Or I must use only Move and care about moving by myself? If so, how to do it then?

Thanks.
Logged

bernardofd
Jr. Member
**
Posts: 74


59948732 bernardofd@click21.com.br
View Profile WWW
« Reply #14 on: March 17, 2006, 02:10:15 pm »

Well, smoothness in camera movement is related to acceleration and not with constant speed. So, you can make a code that increases and decreases speed over time...

If you achieve that, please post it in here, ok? I'm was looking for something like that but I didn't have time to make this.

Regards!
Logged

__________________________
Bernardo Figuerêdo Domingues
Computer Science - UFMG - Brazil
Pages: [1] 2 Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 9.794 seconds with 16 queries.