Camera system working only for one player

saw792

Is known to say things. That is all.
Reaction score
280
I am currently making a first person camera system that allows control over the camera with the mouse using trackables. It only seems to be working for Player 1 though... I suspect the mouse movements that Player 2 does actually moves the camera for Player 1 as well. It's quite a complicated set of code (two libraries), but I would appreciate if you could have a look through.

I can't say it wasn't expected either, but it does lag a fair bit. Optimization suggestions are definitely worthwhile. I am only using gamecache temporarily until I can find a better system to store the trackable data. I don't normally attach to anything other than timers, so this has been interesting. Partial credits of the trackable API to Kattana, I heavily modified his basic idea.

TrackableAPI:
JASS:
library TrackableAPI

  globals
    private gamecache trackstore = InitGameCache("trackcache.w3v")
    trigger TrackTrigger = CreateTrigger() //Trigger that has its action set in FirstPerson library
  endglobals

  function H2I takes handle h returns integer
    return h
    return 0
  endfunction

  private function SetTrackableX takes trackable t, real x returns real //Takes a trackable and saves its X, then returns the trackable
    call StoreReal(trackstore, I2S(H2I(t)), "x", x)
    return x
  endfunction

  private function SetTrackableY takes trackable t, real y returns real //Saves the Y
    call StoreReal(trackstore, I2S(H2I(t)), "y", y)
    return y
  endfunction

  private function SetTrackableZ takes trackable t, real z returns real //Saves the Z
    call StoreReal(trackstore, I2S(H2I(t)), "z", z)
    return z
  endfunction

  function GetTrackableX takes trackable t returns real //Returns the saved X coordinate of the trackable
    return GetStoredReal(trackstore, I2S(H2I(t)), "x")
  endfunction

  function GetTrackableY takes trackable t returns real //Returns the Y
    return GetStoredReal(trackstore, I2S(H2I(t)), "y")
  endfunction

  function GetTrackableZ takes trackable t returns real //Returns the Z
    return GetStoredReal(trackstore, I2S(H2I(t)), "z")
  endfunction
  
  function CreateTrackableZ takes string path, real x, real y, real z, real face returns trackable
      local destructable d = CreateDestructableZ( 'OTip', x, y, z, 0.00, 1, 0 )
      local trackable tr = CreateTrackable( path, x, y, face ) //Creates a trackable on top of the destructible, letting us set the Z height
      call RemoveDestructable( d )
      call SetTrackableX(tr, x) //Saves the X
      call SetTrackableY(tr, y) //Saves the Y
      call SetTrackableZ(tr, z) //Saves the Z
      call TriggerRegisterTrackableTrackEvent(TrackTrigger, tr) //Adds a track event to global trigger
      set d = null
      return tr
  endfunction

endlibrary
FirstPerson:
JASS:
library FirstPerson initializer Init requires TrackableAPI

  private keyword Track //Allows us to use uninitialised struct
  
  globals
    integer FPCount = 0
    Track array data
    unit array dummy
  endglobals
  
  struct Track
  
    private real x
    private real y
    private real z = 0
    private integer increment = 0
    private trackable array tracks[4000]
    private integer maxcount = 0
    
    static method create takes nothing returns Track //Loops through a rectangle, adding trackables at different heights in a grid
      local Track t = Track.allocate()
      loop
        set t.y = -3000
        set t.x = -3000
        set t.z = t.z + 50
        exitwhen t.z >=300
        loop
          exitwhen t.x >= 3000
          set t.tracks[t.increment] = CreateTrackableZ("units\\human\\Peasant\\Peasant.mdx", t.x, t.y, t.z, 0)
          set t.increment = t.increment + 1
          set t.x = t.x + 300
        endloop
        loop
          exitwhen t.y >= 3000
          set t.tracks[t.increment] = CreateTrackableZ("units\\human\\Peasant\\Peasant.mdx", t.x, t.y, t.z, 0)
          set t.increment = t.increment + 1
          set t.y = t.y + 300
        endloop
        loop
          exitwhen t.x <= -3000
          set t.tracks[t.increment] = CreateTrackableZ("units\\human\\Peasant\\Peasant.mdx", t.x, t.y, t.z, 0)
          set t.increment = t.increment + 1
          set t.x = t.x - 300
        endloop
        loop
          exitwhen t.y <= -3000
          set t.tracks[t.increment] = CreateTrackableZ("units\\human\\Peasant\\Peasant.mdx", t.x, t.y, t.z, 0)
          set t.increment = t.increment + 1
          set t.y = t.y - 300
        endloop
      endloop
      set t.maxcount = t.increment
      return t
    endmethod
    
    method IsTrackInStruct takes trackable t returns boolean //Checks if a trackable is in the instance of the struct (in the array)
      set .increment = 0
      loop
        exitwhen GetTriggeringTrackable() == .tracks[.increment] or .increment == .maxcount
        set .increment = .increment + 1
      endloop
      if .increment == .maxcount then
        return false
      endif
      return true
    endmethod
    
  endstruct
  
  function EnableFirstPersonForPlayer takes player p returns nothing //Function called to enable the system
    set dummy[GetPlayerId(p)] = CreateUnit(p, 'h000', 0, 0, 0)
    set data[FPCount] = Track.create() //Creates a Track instance based on player ID (creates the trackables unique to the player)
    set FPCount = FPCount + 1
    call EnableTrigger(TrackTrigger) //Enables detecting trigger
    call FogEnableOff()
    call FogMaskEnableOff()
    if GetLocalPlayer() == p then //Sets camera fields for the player
      call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, 100.00, 0)
      call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, -2.00, 0)
      call SetCameraField(CAMERA_FIELD_ZOFFSET, 180.00, 0)
      call SetCameraField(CAMERA_FIELD_FIELD_OF_VIEW, 10, 0)
      call SetCameraTargetController(dummy[GetPlayerId(p)], 0, 0, false)
    endif
  endfunction
  
  private function GetTrackOwner takes trackable t returns player //Loops through each Track instance to find which player 'owns' the trackable
    local integer i = 0
    loop
      exitwhen data<i>.IsTrackInStruct(t) == true
      set i = i + 1
    endloop
    return Player(i)
  endfunction
  
  private function Actions takes nothing returns nothing
    local player p = GetTrackOwner(GetTriggeringTrackable()) //The player that triggered the trackable
    local real x = GetTrackableX(GetTriggeringTrackable())
    local real y = GetTrackableY(GetTriggeringTrackable())
    local real z = GetTrackableZ(GetTriggeringTrackable())
    local real x2 = GetUnitX(dummy[GetPlayerId(p)])
    local real y2 = GetUnitY(dummy[GetPlayerId(p)])
    local real z2 = 180
    local real dx = x - x2
    local real dy = y - y2
    local real d = SquareRoot(dx * dx + dy * dy)
    local real dz = z - z2
    local real a = Atan2BJ(dz, d) //Raises or lowers camera angle of attack depending on mouse location
    local real a2 = Atan2BJ(dy, dx) //Moves camera left or right depending on mouse location
    if GetLocalPlayer() == p then
      call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, a, 1)
      call SetCameraField(CAMERA_FIELD_ROTATION, a2, 0.5)
    endif
    set p = null
  endfunction
  
  

  private function Init takes nothing returns nothing
    call DisableTrigger(TrackTrigger)
    call TriggerAddAction(TrackTrigger, function Actions)
  endfunction

endlibrary</i>
To enable the system for a player you call EnableFirstPersonForPlayer().

I plan to submit this as a system once (if) it works well in multiplayer.

Thanks
 

saw792

Is known to say things. That is all.
Reaction score
280
Bump

Commented code properly for better readability.
 

saw792

Is known to say things. That is all.
Reaction score
280
Yeah true that will probably fix it. I didn't read any of that tutorial.

EDIT: Hmm it seems obvious now, but the reason it wasn't working for player 2 was because the loop reached its limit. Storing the owner obviously fixed this. This is a problem I ran into in my .create() method as well, and I have fixed that too. Go me.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top