[Tutorial] How to make a rotating camera on class selection
#1


A tutorial to creating camera rotating
Why? What? Why'd you make this?

Out of boredom, I decided to make a class rotating camera tutorial. Would be nice for A/D servers to have such a feature.

What you'll need in this tutorial:
  • A little bit about trignometry (using COS, SIN)
  • A brain (which you all have :P)
Okay, note that this is not even close to what Dice7 wrote a long time ago on his class rotating camera selection. So yeah, don't say it is.

The camera rotates around the player and goes by the circumference of this circle. Some options are tweak-able to adjust the camera though you must get rid of your current camera inside of your gamemode before using.

Basic structure - Diagram


Lets script!
Variables
Using variables/constants looks more neater than defining for me (yes, I'm creating some 32 bit variables facepalm.jpg)
pawn Code:
/* ** Server Data ** */
const // Constant values, you may change it to "new" if they're going to be modified once rotating.
    Float: spawnX = 0.0,     // The SPAWN X you're willing to work from
    Float: spawnY = 0.0,     // The SPAWN Y you're willing to work from
    Float: spawnZ = 5.0,     // The SPAWN Y you're willing to work from
    Float: camRadius = 20.0, // The radius of the camera rotating
    Float: camSpeed  = 1.25, // The speed of the camera moving around. MUST NOT BE NULL; IT WONT MOVE IF SO!
    Float: camHeight = 6.0   // The height of the camera once moving!
;

/* ** Player Data ** */
new
    prc_Timer                   [MAX_PLAYERS],  // The timer for the movement.
    bool: prc_Moving            [MAX_PLAYERS],  // Check if the player is moving his camera
    Float: prc_Degree           [MAX_PLAYERS]   // The degree counter to show which the player is on.
;

forward MoveCamera(playerid); // We'll add this here to prevent future warnings..
OnPlayerRequestClass
pawn Code:
public OnPlayerRequestClass(playerid, classid)
{
    SetPlayerPos(playerid, spawnX, spawnY, spawnZ); // Set the player at those 3 const spawn values from before.
   
    if(prc_Moving[playerid] == false) // Check whether the camera is already set.
    {
        prc_Degree[playerid] = 0; // Reseting the variable
        prc_Timer[playerid] = SetTimerEx("MoveCamera", 75, true, "d", playerid); // Setting the timer
        prc_Moving[playerid] = true; // okay, now we're going to activate the moving variable
    }
    return 1;
}
Camera movement
pawn Code:
public MoveCamera(playerid) // The core of the movement
{
    static // Static for the repeation :O
        Float: nX,  // The newX
        Float: nY   // the newY
    ;
    if(prc_Moving[playerid] == false) // check whether the timer is activated and the variable isn't.
    {
        KillTimer(prc_Timer[playerid]); // Killing the timer, ofc we don't want a 75 ms timer always running.
        prc_Degree[playerid] = 0; // Reseting the variable
    }
    if(prc_Degree[playerid] >= 360) prc_Degree[playerid] = 0; // If the rotation is past 360, reset to 0 (looks more neater)
    prc_Degree[playerid] += camSpeed; // For some smooth rotation, I'm using 1.25
   
    nX = spawnX + camRadius * floatcos(prc_Degree[playerid], degrees);
    nY = spawnY + camRadius * floatsin(prc_Degree[playerid], degrees);
   
    // So, we're going to get the spawn axis and add the radius in to them.
    // Then we use the trignometric functions to apply such angles to the movement
    // making it look nice.
    SetPlayerCameraPos(playerid, nX, nY, spawnZ + camHeight); // Setting the Camera position around the player
    SetPlayerCameraLookAt(playerid, spawnX, spawnY, spawnZ); // Looking at the player position from there :)
    SetPlayerFacingAngle(playerid, prc_Degree[playerid] - 90.0); // to make it face the camera :)
    return 1;
}
OnPlayerDisconnect
pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    if(prc_Moving[playerid] == true)
    {
        KillTimer(prc_Timer[playerid]); // Killing the timer, ofc we don't want a 75 ms timer always running.
        prc_Degree[playerid] = 0; // Reseting the variable
        prc_Moving[playerid] = false; // Reseting the variable
    }
    return 1;
}
OnPlayerSpawn
pawn Code:
public OnPlayerSpawn(playerid)
{
    if(prc_Moving[playerid] == true)
    {
        KillTimer(prc_Timer[playerid]); // Killing the timer, ofc we don't want a 75 ms timer always running.
        prc_Degree[playerid] = 0; // Reseting the variable
        prc_Moving[playerid] = false; // Reseting the variable
        SetCameraBehindPlayer(playerid); // Preventing bugs from appearing.
    }
    return 1;
}
Screenshots
I rather have screenies than a video, since ******* wrecks the video quality for me - Uploaded with imageShack, sorry for people that cannot view it!




Download

Example gamemode with the default structure

Problems?
If there is any problems which mostly said not to be, please report them and I'll fix them.
Reply


Messages In This Thread
How to make a rotating camera on class selection - by Lorenc_ - 22.10.2011, 00:45
Re: How to make a rotating camera on class selection - by ©•Riddy•© - 22.10.2011, 00:49
Re: How to make a rotating camera on class selection - by Lorenc_ - 22.10.2011, 00:52
Re: How to make a rotating camera on class selection - by Xx_OutLawZ_xX - 22.10.2011, 02:55
Re: How to make a rotating camera on class selection - by vassilis - 22.10.2011, 07:04
Re: How to make a rotating camera on class selection - by Max_Coldheart - 22.10.2011, 07:07
Re: How to make a rotating camera on class selection - by SmiT - 22.10.2011, 07:20
Re: How to make a rotating camera on class selection - by System64 - 22.10.2011, 08:00
Re: How to make a rotating camera on class selection - by Twizted - 22.10.2011, 08:01
Re: How to make a rotating camera on class selection - by Lorenc_ - 22.10.2011, 10:33
Re: How to make a rotating camera on class selection - by vassilis - 22.10.2011, 11:55
Re: How to make a rotating camera on class selection - by juraska - 22.10.2011, 16:26
Re: How to make a rotating camera on class selection - by Lorenc_ - 22.10.2011, 19:46
Re: How to make a rotating camera on class selection - by vassilis - 22.10.2011, 20:17
Re: How to make a rotating camera on class selection - by Luis- - 23.10.2011, 01:04
Re: How to make a rotating camera on class selection - by Lorenc_ - 26.10.2011, 08:24
Re: How to make a rotating camera on class selection - by Niko_boy - 26.10.2011, 08:33
Re: How to make a rotating camera on class selection - by Michael@Belgium - 26.10.2011, 12:00
Re: How to make a rotating camera on class selection - by Aira - 04.11.2011, 07:20
Re: How to make a rotating camera on class selection - by Lorenc_ - 19.11.2011, 06:19
Re: How to make a rotating camera on class selection - by GangsTa_ - 19.11.2011, 06:32
Re: How to make a rotating camera on class selection - by Astralis - 19.11.2011, 06:36
Re: How to make a rotating camera on class selection - by thimo - 09.12.2011, 18:52
Re: How to make a rotating camera on class selection - by Kyle - 28.12.2011, 19:41
Re: How to make a rotating camera on class selection - by Ronaldo_raul™ - 19.01.2012, 11:18
Re: How to make a rotating camera on class selection - by vassilis - 19.01.2012, 11:45
Re: How to make a rotating camera on class selection - by Ronaldo_raul™ - 19.01.2012, 12:15
Re: How to make a rotating camera on class selection - by Jonny5 - 17.05.2012, 00:58
Re: How to make a rotating camera on class selection - by Fuler - 25.10.2013, 10:26
Re: How to make a rotating camera on class selection - by dusk - 10.11.2013, 09:31
Re: How to make a rotating camera on class selection - by Ryz - 30.09.2014, 04:13
Re: How to make a rotating camera on class selection - by seanny - 30.09.2014, 23:12

Forum Jump:


Users browsing this thread: 3 Guest(s)