May 22, 2012, 11:56:44 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Please report any bugs or issues that you might be encountering with the Beta in the Support System so that we can better keep track of any oustanding issues that may come up.

GameCore Support System
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: [Example] Rotating Object (object without collisions)  (Read 1248 times)
Javier
Newbie
*
Posts: 47



View Profile
« on: July 28, 2008, 04:46:36 PM »

Hello all.

I have made a Script of a Rotating Object (My first script whit GameCore  Cheesy).
The Script should work whit any object without collisions (and without another script).

The object rotates and don't stop. You can set how many degress rotate each second. Configurable via the OPR file. Rotation along the X, Y and Z axis are possible (one, two or three axis at the same time).
Quote
OPRP

LoadObject RotatingObject.fbx

# Script
ControllerType Scripted
ControllerSetting ScriptFile RotatingObject.gsl

ControllerSetting RotateXAxis 0
ControllerSetting RotateYAxis 90
ControllerSetting RotateZAxis 0

The Script Code (RotatingObject.gsl):
Quote
//------------------------------------------------------------
//
//   Script for a Rotating Object (Object Without Collision).
//  Version: 1.0 (28-July-2008)
//
//  Tested whit: GameCore 2.0 Beta 1
//
//------------------------------------------------------------


/*

Controller Settings:

RotateXAxis - Angle to rotate, in a second, along the X Axis.
RotateYAxis - Angle to rotate, in a second, along the Y Axis.
RotateZAxis - Angle to rotate, in a second, along the Z Axis.

*/


// constants (modifiable from controller settings, see Initialize function)
float ROTATE_X_AXIS = 0;
float ROTATE_Y_AXIS = 90;
float ROTATE_Z_AXIS = 0;



//------------------------------------------------------------
//
//   void Initialize( Object@ object)
//
//      This function is called when the controller is created.
//      Any initialization should be done here.
//
//   Parameters:
//
//      object - The object that this controller is to act upon.
//

void Initialize( Object@ object)
{

   // Get the Controller Settings front the Object Properties.
   ROTATE_X_AXIS = object.GetControllerSettingFloat( "RotateXAxis", ROTATE_X_AXIS);
   ROTATE_Y_AXIS = object.GetControllerSettingFloat( "RotateYAxis", ROTATE_Y_AXIS);
   ROTATE_Z_AXIS = object.GetControllerSettingFloat( "RotateZAxis", ROTATE_Z_AXIS);
   
}

//------------------------------------------------------------
//
//   void DynamicsAdvance( Object@ object, float seconds)
//
//      This function is called once per dynamics step.  Any
//      object movement should be done here.
//
//   Parameters:
//
//      object - The object that this controller is to act upon.
//
//      seconds - How much time has passed since last call.
//

void DynamicsAdvance( Object@ object, float seconds)
{

   // Get the Current Matrix of the Object.
   Matrix mtx = object.GetCurMatrix();
   
   // ROTATE along Axis.
   
   // X Axis Rotation.
   if( ROTATE_X_AXIS != 0 )
   {
      mtx.RotateAxis( Vector(1, 0, 0), ROTATE_X_AXIS * seconds);
   } // End X Axis Rotation.
   
   // Y Axis Rotation.
   if( ROTATE_Y_AXIS != 0 )
   {
      mtx.RotateAxis( Vector(0, 1, 0), ROTATE_Y_AXIS * seconds);
   } // End Y Axis Rotation.
   
   // Z Axis Rotation.
   if( ROTATE_Z_AXIS != 0 )
   {
      mtx.RotateAxis( Vector(0, 0, 1), ROTATE_Z_AXIS * seconds);
   } // End Z Axis Rotation.
   
   // End ROTATE.
      
   // Set the Matrix to the Object.
   object.GetCurMatrix().XAxis = mtx.XAxis;
   object.GetCurMatrix().YAxis = mtx.YAxis;
   object.GetCurMatrix().ZAxis = mtx.ZAxis;   
   
}

Any comments, suggestions or better ways to do that will be welcome.  Grin
« Last Edit: September 05, 2011, 08:57:33 AM by Javier Barbero » Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #1 on: July 28, 2008, 05:39:18 PM »

there is a rotating object script somewhere but nice with contributions. Try to post the code here too. It's alot better to see it live here to without downloading
Logged
Javier
Newbie
*
Posts: 47



View Profile
« Reply #2 on: July 28, 2008, 05:49:29 PM »

there is a rotating object script somewhere but nice with contributions. Try to post the code here too. It's alot better to see it live here to without downloading
Good idea. I have added the code to the first post.
I have used (quote) because whit (code) looks very strange.

Thanks.
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #3 on: July 29, 2008, 01:45:32 AM »

Looks really pro and you seem to be able to pick up angelscript easy. I looked at old example scripts and someone posted a rotating door script (a door opening) and here is a line i saw:

Code:
object.GetCurMatrix().Rotate( 0, 90, 0);

But your script controls it alot more with seconds and all. Good post!
Logged
Bensta
Jr. Member
**
Posts: 63



View Profile
« Reply #4 on: July 29, 2008, 02:55:20 AM »

Aye could come in handy that good work thanks for shareing
Logged
Javier
Newbie
*
Posts: 47



View Profile
« Reply #5 on: July 29, 2008, 09:20:11 AM »

Thanks both.  Smiley

Anglescript seems to be very easy to use.
The list and the documentation of the functions help me too much.
Logged
BigDaz
Sr. Member
****
Posts: 345


HardCore


View Profile
« Reply #6 on: July 30, 2008, 01:43:18 PM »

Thanks.

I would like to see an example of inputs from the keyboard rotating an object. So the player presses a key and the object rotates left, right, up, down and so on.
Logged
gekido
Guest
« Reply #7 on: July 30, 2008, 06:45:29 PM »

The scripting docs aren't 100% updated with everything that's available in the new version quite yet, but we're going through them and updating the functions as quickly as possible.

We have a new junior programmer starting next week who will be helping to document things as well.
Logged
Javier
Newbie
*
Posts: 47



View Profile
« Reply #8 on: July 31, 2008, 01:34:17 AM »

The scripting docs aren't 100% updated with everything that's available in the new version quite yet, but we're going through them and updating the functions as quickly as possible.

We have a new junior programmer starting next week who will be helping to document things as well.
This is great gekido!  Smiley
Logged
Javier
Newbie
*
Posts: 47



View Profile
« Reply #9 on: July 31, 2008, 03:03:31 AM »

Thanks.

I would like to see an example of inputs from the keyboard rotating an object. So the player presses a key and the object rotates left, right, up, down and so on.
Hi BigDaz. This is very easy to do.

First, you must set a new action and KeyMap a key for this action. Edit the "input.cfg" file (GameEditor -> Items (right panel) -> Config -> input.cfg. They will be open in the Script editor. Add the lines that are in bold. You can change KeyR for any Key that you want. Also you can set any name for the Action.
Quote
###########################################################################
# input settings for specific controllers

InputSet Player Camera
...

InputSet Player Character
Action Forward
Action Backward
Action Left
Action Right
Action Fire
Action Jump
Action MouseJump
Action Run
Action WeaponA
Action WeaponB
Action RotateObject
KeyMapping KeyUp Forward
KeyMapping KeyDown Backward
...
KeyMapping Joy2 Jump
KeyMapping Joy3 Run
KeyMapping KeyR RotateObject
...

Now, open the Rotating Object OPR file and add the input area.
Quote
ControllerInputCenter 0 0 0
ControllerInputBoxSize 3 3 3
This will set a Box Input Area (size 3x3x3) centered at the center of the Object. To help you to adjust the size and the center of the Input Box you can turn on "Show Input Areas" in the GameEditor. Options -> Show Iput Areas (check it Smiley).


Now, go to edit the script file.
First add a global script variable. This variable will be true if the player is rotating the object. False if not.
Quote
// variables
bool inputRotating = false;

In the DynamicsAdvance function only rotate the object if the inputRotating variable is true.
Quote
void DynamicsAdvance( Object@ object, float seconds)
{

   if(inputRotating)
   {

              // Roate the Object here.
             //  .....   
   }
   
}

Now, add the HandleEvent function (see the complete script code).

The complete script:
Quote
/*

Controller Settings:

RotateXAxis - Angle to rotate, in a second, along the X Axis.
RotateYAxis - Angle to rotate, in a second, along the Y Axis.
RotateZAxis - Angle to rotate, in a second, along the Z Axis.

*/


// constants (modifiable from controller settings, see Initialize function)
float ROTATE_X_AXIS = 0;
float ROTATE_Y_AXIS = 90;
float ROTATE_Z_AXIS = 0;

// variables
bool inputRotating = false;




//------------------------------------------------------------
//
//   void Initialize( Object@ object)
//
//      This function is called when the controller is created.
//      Any initialization should be done here.
//
//   Parameters:
//
//      object - The object that this controller is to act upon.
//

void Initialize( Object@ object)
{

   // Get the Controller Settings front the Object Properties.
   ROTATE_X_AXIS = object.GetControllerSettingFloat( "RotateXAxis", ROTATE_X_AXIS);
   ROTATE_Y_AXIS = object.GetControllerSettingFloat( "RotateYAxis", ROTATE_Y_AXIS);
   ROTATE_Z_AXIS = object.GetControllerSettingFloat( "RotateZAxis", ROTATE_Z_AXIS);

}

//------------------------------------------------------------
//
//   bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
//
//      All of the keys that can be pressed for this object are
//      passed to this function every frame.
//
//   Parameters:
//
//      object - The object that this controller is to act upon.
//
//      event - Name of the event type
//
//      params - Parameters for the event (specific to each event type)
//

bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
   // Only Events for Player.
   if (params.player == null)
      return false;

   // Input event
   if (event == "Input")
   {
      if (params.stringValue == "RotateObject")
      {
         if(params.floatValue >= 0.5f)
         {
            inputRotating = true;
            return true;
         }
      }
   
   }
   
   return false;
}


//------------------------------------------------------------
//
//   void DynamicsAdvance( Object@ object, float seconds)
//
//      This function is called once per dynamics step.  Any
//      object movement should be done here.
//
//   Parameters:
//
//      object - The object that this controller is to act upon.
//
//      seconds - How much time has passed since last call.
//

void DynamicsAdvance( Object@ object, float seconds)
{

   if(inputRotating)
   {

      
      // Get the Current Matrix of the Object.
      Matrix mtx = object.GetCurMatrix();
      
      // ROTATE along Axis.
      
      // X Axis Rotation.
      if( ROTATE_X_AXIS != 0 )
      {
         mtx.RotateAxis( Vector(1, 0, 0), ROTATE_X_AXIS * seconds);
      } // End X Axis Rotation.
      
      // Y Axis Rotation.
      if( ROTATE_Y_AXIS != 0 )
      {
         mtx.RotateAxis( Vector(0, 1, 0), ROTATE_Y_AXIS * seconds);
      } // End Y Axis Rotation.
      
      // Z Axis Rotation.
      if( ROTATE_Z_AXIS != 0 )
      {
         mtx.RotateAxis( Vector(0, 0, 1), ROTATE_Z_AXIS * seconds);
      } // End Z Axis Rotation.
      
      // End ROTATE.
         
      // Set the Matrix to the Object.
      object.GetCurMatrix().XAxis = mtx.XAxis;
      object.GetCurMatrix().YAxis = mtx.YAxis;
      object.GetCurMatrix().ZAxis = mtx.ZAxis;


      // The User must hold the Key to continue rotating the object.
      inputRotating = false;

   
   }
   
}



Hope help you.
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #10 on: July 31, 2008, 05:24:49 AM »

MAn you are really picking this up. Now i can create a nice model viewer Smiley
Logged
BigDaz
Sr. Member
****
Posts: 345


HardCore


View Profile
« Reply #11 on: July 31, 2008, 01:35:52 PM »

Wow that was quick. Thanks. Smiley
Logged
hikmayan
Full Member
***
Posts: 231



View Profile
« Reply #12 on: August 11, 2008, 02:35:43 PM »

Javier Barbero:
Thanks for the posts...very handy indeed. Keep them coming,it help us(none-programmers) see the light at the end of the tunnel. Cool.
Logged

You have to be in it.....to win it!
Pages: [1]
  Print  
 
Jump to:  

 
Powered by MySQL Powered by PHP bluBlur Skin © 2006, hbSkins
Powered by SMF 1.1.14 | SMF © 2006-2011, Simple Machines LLC
Valid XHTML 1.0! Valid CSS!