Help with your scripting

Camera - Stop at Bounds

Camera - Stop at Bounds

Postby Shando » 04 Oct 2017, 23:28

Hi All,

Is there a way to limit the area that the main Camera can move in?

For example, I would like to restrict movement of the Camera to say +100/-100 units in the X & Z axes and +100/+10 in the Y axis.

If this isn't possible, does anyone have any code for a programmed Camera that I could use and change where necessary?

Thanks in advance

Shando
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby StarFire » 05 Oct 2017, 12:55

Sorry there is no limit for the main camera but you could use the camera entity and manually set its movement range, just move it as you would any other entity using the entity library.
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby SolarPortal » 06 Oct 2017, 10:37

The way i would tackle this is too implement my own camera movement,

so to start, i would add a camera entity to the scene.
* Set its property to "Fixed".
* Add a microscript.
* Use the entity.yaw() and entity.pitch() to rotate the camera by storing a current pitch and current yaw variable that you increase or decrease based on camera movement.

Note: On the start of the update, set the camera's rotation to 0,0,0 using the entity.setRotation(0,0,0) command, then set the yaw followed by the pitch to make sure there is no gimbal lock.

This way you can control what angles the camera can rotate to on the yaw and on the pitch :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby Shando » 06 Oct 2017, 11:04

Thanks for the ideas, I'll see what I can do :)

Still trying to get most of my logic down for this game ( already got well over 2000 lines of actual code - ignoring blank lines :evil: )

Who would of thunk adding those few extra lines for each message would cause such a huge increase :mrgreen:

Up to almost 70 messages now and that's only really on the Server Side, haven't looked too closely at Client Side responses yet :(
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby SolarPortal » 06 Oct 2017, 11:35

It just keeps getting bigger and bigger :)

are you using the include files to make it easier to maintain :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby Shando » 06 Oct 2017, 12:23

Hi SP,

I'm currently writing across 6 different files (currently 4 Server Side and 2 Client Side) :)

Haven't 100% decided what will go where as I'm writing "blind" (i.e. just coding in Notepad++ and hoping it will all come together!). I have done this frequently in the past as I then don't get bogged down fixing small issues as I go, and Notepad++ makes it easy to add (and remove) the judicious use of lprints 8-) I then just copy/paste the whole file into the Script Editor and run it.

This way, I can also carry on coding whilst waiting for new features or replies in the Forum (the time difference can be a real ****** at times).

Shando
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby StarFire » 06 Oct 2017, 12:26

Heres a bit of camera bounds code for you :D

Code: Select all

--[[
   Aurasoft Uk 2017 Skyline Example Script.
   
   Add and entity camera to your scene.   Attach this script either as an external script or a micro script.
   the camera bounds is set by the "bounds" variable
]]

speed       = 6;         -- speed of movement
rotSpeed    = 100;       -- speed of rotation
bounds    = 5;         -- camera bounds in both x and z

obj          = 0;
posx       = 0;
posy       = 0;
posz       = 0;

function onInit(objID)
   sky.lprint("LUA: Script Active!");
   obj = objID;
   sky.ui_setFontColor("#000000FF");
end

function postInit()
   camera.setActiveCamera(obj);
end

function onUpdate( td )
      moveSpeed = 0;
      rotate = rotSpeed * td;
      
      if(input.keyDown("a") == 1)then   entity.yaw(obj, rotate, enum.rotateDegree());   end
      if(input.keyDown("d") == 1)then   entity.yaw(obj, -rotate, enum.rotateDegree());end
      if(input.keyDown("w") == 1 )then   moveSpeed = -speed*td;end
      if(input.keyDown("s") == 1 )then   moveSpeed = speed*td;end
      
      dir = newType.vec3(entity.getDirectionOfEntity(obj));
      entity.move(obj, dir.x * moveSpeed, dir.y * moveSpeed , dir.z * moveSpeed); -- direction * speed
      doBoundsCheck();
end

function doBoundsCheck(  )
   x,y,z = entity.getPosition(obj);
   sprint(math.modf(x).." | "..math.modf(z),1)
   if(x < -bounds )  then x = -bounds;end   
   if(x > bounds )    then x =  bounds;   end
   if(z < -bounds )    then z = -bounds;end   
   if(z > bounds )    then z =  bounds; end
   entity.setPosition(obj, x,y,z);
end
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby Shando » 06 Oct 2017, 12:56

Hi StarFire,

All I can say is....

grazie
merci
asante
tualumba
dhanyawaad
miigwetch
go raibh maith agat

I.e. Thank You 7 times over ;)
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby StarFire » 06 Oct 2017, 14:37

My pleasure :D
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby Shando » 08 Oct 2017, 02:13

Hi All,

Do the following commands work?
Code: Select all
camera.getPitch ( )
camera.getRoll ( )
camera.getYaw ( )


I'm trying to limit the camera rotation in Y to 45 degrees left & right and the above commands all seem to return zero?

Thanks in advance

Shando

@Tattie - thanks for the code & reply to my previous post (which I deleted before I saw your reply :( as I thought I'd found a way around it and no-one else is usually online at this time of night in the UK!)

PS: also noticed there were no similar commands for an "entity".
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Camera - Stop at Bounds

Postby TattieBoJangle » 08 Oct 2017, 02:17

Hi m8 hope this helps it wasn't limited tho the bottom one is joypad

Code: Select all
-- | User Changeable Properties.
-- |=========================================================================================================================
minSpeed = 80;       -- Min speed the plane can travel. This is in no unit measurement. Larger = faster. Smaller = Slower
maxSpeed = 120;    -- Max speed the plane can travel. This is in no unit measurement. Larger = faster. Smaller = Slower

vBoost = 80;       -- Use to rotate the plane on the pitch(up/down) quicker. Larger = faster.
hBoost = 130;       -- Use to rotate the plane on the roll(left/right) quicker. Larger = faster.

reactionSpeed = 5;    -- This changes how fast you can cahnge direction when another key is pressed. Larger = Firmer. Smaller = Lazier

-- | Required Script Variables. (Globals)
-- |=========================================================================================================================
obj = 0;
speed = 0;
wKeyDown = 0; aKeyDown = 0; sKeyDown = 0; dKeyDown = 0;
cVertTurn = 0;
cHorTurn = 0;

-- | Airplane script
-- |=========================================================================================================================

function onInit(objID)
   obj = objID;
end

function onUpdate( timeDelta )
   -- Fetch inputs and get smooth values
   doInput( timeDelta );
   
   -- Propel the plane forwards
   doMovement( timeDelta );
   
   -- Apply Pitch & Roll to control the plane and make it turn corners.
   doRotation( timeDelta );
   
   -- Stop the plane from going through the terrain floor.
   checkGroundClearance();
end

function doMovement( timeDelta )
   -- get the normalised(0-1 range) forward direction of the plane.
   forward = newType.vec3 (vector3.normalisedCopy( entity.getForward(obj) ) );
   
   -- set the speed of the plane, slower going up and faster pointing down for gravity effect.
   speed = speed - forward.y * timeDelta * 50.0;
   if(speed < minSpeed)then speed = minSpeed; end
   if(speed > maxSpeed)then speed = maxSpeed; end
   
   -- Now move the entity forwards by the speed and using timedelta for the same flight at all framerates
   entity.move(obj, (forward.x * speed) * timeDelta, (forward.y * speed)  * timeDelta, (forward.z * speed) * timeDelta )
end

function doRotation( timeDelta )
   entity.turn(obj, cVertTurn * vBoost * timeDelta, 0, cHorTurn * hBoost * timeDelta);
end

function checkGroundClearance()
   -- Keep plane above terrain ground.
   currentPos = newType.vec3(entity.getPosition(obj));
   terrainheight = terrain.getHeightAtPoint( currentPos.x, currentPos.z );
   if( currentPos.y < terrainheight )then
      entity.setPosition( obj, currentPos.x, terrainheight, currentPos.z );
   end   
end

function doInput( timeDelta )
   -- Grab the input values and smooth them out for nicer control
   smoothRate = reactionSpeed * timeDelta;
   
   -- Plane pitching Up/Down input
   if(wKeyDown == 1)then   
      cVertTurn = cVertTurn - smoothRate;
   elseif(sKeyDown == 1)then   
      cVertTurn = cVertTurn + smoothRate;
   else
      if(cVertTurn > 0.1)then
         cVertTurn = cVertTurn - smoothRate;   
      elseif(cVertTurn < -0.1)then
         cVertTurn = cVertTurn + smoothRate;
      else
         cVertTurn = 0;
      end
   end
   
   if(cVertTurn > 1)then cVertTurn = 1; end
   if(cVertTurn < -1)then cVertTurn = -1; end
   
   -- Plane Banking rolling input
   if(aKeyDown == 1)then
      cHorTurn = cHorTurn - smoothRate;
   elseif(dKeyDown == 1)then
      cHorTurn = cHorTurn + smoothRate;
   else
      if(cHorTurn > 0.1)then
         cHorTurn = cHorTurn - smoothRate;
      elseif(cHorTurn < -0.1)then
         cHorTurn = cHorTurn + smoothRate;
      else
         cHorTurn = 0;
      end
   end
   
   if(cHorTurn > 1)then cHorTurn = 1; end
   if(cHorTurn < -1)then cHorTurn = -1; end
end


-- | Input key events
-- |=========================================================================================================================

function onKeyDown( key )
   if(key == "w")then   wKeyDown = 1; end
   if(key == "a")then   aKeyDown = 1; end
   if(key == "s")then   sKeyDown = 1; end
   if(key == "d")then   dKeyDown = 1; end
end

function onKeyUp( key )
   if(key == "w")then   wKeyDown = 0; end
   if(key == "a")then   aKeyDown = 0; end
   if(key == "s")then   sKeyDown = 0; end
   if(key == "d")then   dKeyDown = 0; end
end



Code: Select all
-- | User Changeable Properties.
-- |=========================================================================================================================
minSpeed = 80;       -- Min speed the plane can travel. This is in no unit measurement. Larger = faster. Smaller = Slower
maxSpeed = 120;    -- Max speed the plane can travel. This is in no unit measurement. Larger = faster. Smaller = Slower

vBoost = 80;       -- Use to rotate the plane on the pitch(up/down) quicker. Larger = faster.
hBoost = 130;       -- Use to rotate the plane on the roll(left/right) quicker. Larger = faster.

reactionSpeed = 5;    -- This changes how fast you can cahnge direction when another key is pressed. Larger = Firmer. Smaller = Lazier

-- | Required Script Variables. (Globals)
-- |=========================================================================================================================
obj = 0;
speed = 0;
wKeyDown = 0; aKeyDown = 0; sKeyDown = 0; dKeyDown = 0;
cVertTurn = 0;
cHorTurn = 0;
joyPadPitch = 0;
joyPadRoll = 0;
-- | Airplane script
-- |=========================================================================================================================

function onInit(objID)
   obj = objID;
end

function onUpdate( timeDelta )
   -- Fetch inputs and get smooth values
   doInput( timeDelta );
   
   -- Propel the plane forwards
   doMovement( timeDelta );
   
   -- Apply Pitch & Roll to control the plane and make it turn corners.
   doRotation( timeDelta );
   
   -- Stop the plane from going through the terrain floor.
   checkGroundClearance();
end

function doMovement( timeDelta )
   -- get the normalised(0-1 range) forward direction of the plane.
   forward = newType.vec3 (vector3.normalisedCopy( entity.getForward(obj) ) );
   
   -- set the speed of the plane, slower going up and faster pointing down for gravity effect.
   speed = speed - forward.y * timeDelta * 50.0;
   if(speed < minSpeed)then speed = minSpeed; end
   if(speed > maxSpeed)then speed = maxSpeed; end
   
   -- Now move the entity forwards by the speed and using timedelta for the same flight at all framerates
   entity.move(obj, (forward.x * speed) * timeDelta, (forward.y * speed)  * timeDelta, (forward.z * speed) * timeDelta )
end

function doRotation( timeDelta )
   entity.turn(obj, cVertTurn * vBoost * timeDelta, 0, cHorTurn * hBoost * timeDelta);
end

function checkGroundClearance()
   -- Keep plane above terrain ground.
   currentPos = newType.vec3(entity.getPosition(obj));
   terrainheight = terrain.getHeightAtPoint( currentPos.x, currentPos.z );
   if( currentPos.y < terrainheight )then
      entity.setPosition( obj, currentPos.x, terrainheight, currentPos.z );
   end   
end

function doInput( timeDelta )
   -- Grab the input values and smooth them out for nicer control
   smoothRate = reactionSpeed * timeDelta;
   
   -- Plane pitching Up/Down input
   if(wKeyDown == 1)then   
      cVertTurn = cVertTurn - smoothRate;
   elseif(sKeyDown == 1)then   
      cVertTurn = cVertTurn + smoothRate;
   elseif(joyPadPitch > 0.1)then
      cVertTurn = ( cVertTurn - smoothRate ) * joyPadPitch;
   elseif(joyPadPitch < -0.1)then
      cVertTurn = ( cVertTurn + smoothRate ) * -joyPadPitch;
   else
      if(cVertTurn > 0.1)then
         cVertTurn = cVertTurn - smoothRate;   
      elseif(cVertTurn < -0.1)then
         cVertTurn = cVertTurn + smoothRate;
      else
         cVertTurn = 0;
      end
   end
   
   if(cVertTurn > 1)then cVertTurn = 1; end
   if(cVertTurn < -1)then cVertTurn = -1; end
   
   -- Plane Banking rolling input
   if(aKeyDown == 1)then
      cHorTurn = cHorTurn - smoothRate;
   elseif(dKeyDown == 1)then
      cHorTurn = cHorTurn + smoothRate;
   elseif(joyPadRoll > 0.1)then
      cHorTurn = ( cHorTurn - smoothRate ) * joyPadRoll;
   elseif(joyPadRoll < -0.1)then
      cHorTurn = ( cHorTurn + smoothRate ) * -joyPadRoll;
   else
      if(cHorTurn > 0.1)then
         cHorTurn = cHorTurn - smoothRate;
      elseif(cHorTurn < -0.1)then
         cHorTurn = cHorTurn + smoothRate;
      else
         cHorTurn = 0;
      end
   end
   
   if(cHorTurn > 1)then cHorTurn = 1; end
   if(cHorTurn < -1)then cHorTurn = -1; end
end


-- | Input key events
-- |=========================================================================================================================

function onKeyDown( key )
   if(key == "w")then   wKeyDown = 1; end
   if(key == "a")then   aKeyDown = 1; end
   if(key == "s")then   sKeyDown = 1; end
   if(key == "d")then   dKeyDown = 1; end
end

function onKeyUp( key )
   if(key == "w")then   wKeyDown = 0; end
   if(key == "a")then   aKeyDown = 0; end
   if(key == "s")then   sKeyDown = 0; end
   if(key == "d")then   dKeyDown = 0; end
end


---------------------------------------------------------------------------------
-- INPUT: GAMEPAD
---------------------------------------------------------------------------------
function onJSAxisMoved(axis, value)
   if(axis==0) then
      if(value<-400 )then
         joyPadPitch = 1;
      elseif(value > 400)then
         joyPadPitch = -1;
      else
         joyPadPitch = 0;
      end
     
   end

   if(axis==1) then
      if(value<-400 )then
         joyPadRoll = 1;
      elseif(value > 400)then
         joyPadRoll = -1;
      else
         joyPadRoll = 0;
      end
   end
end
Case: CM Storm Trooper CPU: I7 5930k X99 Cooler: Noctua NH-D15 Graphics: Asus GTX 1080 Motherboard: Rampage Extreme V x99 Ram: RipJaws DDR4 3000mhz Storage: x2 SSD Crucial 500GB + x5 2TB Hdd PSU: Evga 1500w OS: Windows 10
User avatar
TattieBoJangle
Community Manager
Community Manager
 
Posts: 858
Joined: 26 Jan 2015, 00:15
Location: United Kingdom
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer
Skill: Great creative

Re: Camera - Stop at Bounds

Postby SolarPortal » 08 Oct 2017, 15:38

Hmm, the camera commands must be working of the wrong node or from the camera, will have to check them out lol :P

the lua entity library has:

Code: Select all
entity.getYaw();
entity.getPitch();
entity.getRoll();

entity.setYaw();
entity.setPitch();
entity.setRoll();

entity.yw();
entity.pitch();
entity.roll();

entity.getRotation()
entity.setRotation()


when dealing with limiting axis. i do this sort of thing: (pseudo code)

Code: Select all
currentYaw  =0
currentPitch =0
function onUpdate(td)
    currentYaw = currentYaw +0.01; -- Use mouse delta to control yaw
    currentPitch = currentPitch +0.01;-- Use mouse delta to control pitch

    -- limit the angles
    if(currentYaw > 45)then currentYaw = 45
    elseif(currentYaw < -45)then currentYaw = -45 end

    if(currentPitch > 45)then currentPitch = 45
    elseif(currentPitch < -45)then currentPitch = -45 end

    -- Reset orientation back to identity rotation.
    entity.setRotation(obj, 0,0,0 )

    -- Now yaw first to get the turn
    entity.yaw( currentYaw )

    -- Then pitch to get the angle.
    entity.pitch( currentPitch )
end


Thats pretty much it :) If you convert that to code, then it should work fine :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 4 guests

cron