Results 1 to 21 of 21

Thread: Scripting for moving models with a controller board

  1. #1

    Question Scripting for moving models with a controller board

    Hello, I am fairly new to scripting LUA but a long time user of TTS. I currently am working on a mod and need some guidance on for how to accomplish my goal. Right now I am trying to move a custom model from its current posistion to a new postition based on clicking a button on a controle board (Tile). Right now I have the tile with all the buttons listed on it and they each have a function for when they are clicked. Which right now just prints a simple message. My next step is to have them move a custom model from its current posistion to a off set postiton.

    But let's just do one and once I understand I feel I can do the rest on my own.
    Code:
    --ID for this Pannel
    Controlepanel_GUID = '6a260a'
    myShip_GUID = '1292f5'
    
    --Make Btn for Stright 1
    buttonParameters_S1 = {}
    buttonParameters_S1.click_function = 'click_S1'
    buttonParameters_S1.function_owner = nil
    buttonParameters_S1.label = 'S1'
    buttonParameters_S1.position = {0.37,0.25,0.43}
    buttonParameters_S1.rotation = {0,0,0}
    buttonParameters_S1.width =120
    buttonParameters_S1.height =120
    buttonParameters_S1.font_size =100
    
    function onload ()
    
    Controlepanel = getObjectFromGUID(Controlepanel_GUID)
    myShip = getObjectFromGUID(myship_GUID)
    
    Controlepanel.setName('Pannel Name')
    
    Controlepanel.createButton(buttonParameters_s1)
    
    end
    
    --<><><><> On Button Press <><><><>
    
    MoveModule = {}
    
    myShip ={}
    function click_S1() then
    	print('This moves the ship 1 straight')
    	Controlepanel.getName()
    	myShip.translate{10,0,0}
    	myShip.setLock(true)
    end
    That is what I have so far, not sure what to do next. Ideally, since there are more than one ship and more than one panel I would need to connect each to its corresponding ship. Can I make it so the name of the tile which button that is pressed moves only the model that has the same name as the tile?

    I know that myShip.translate should move "myShip" 10 along the x-axis but I don't seem to have something set up right


    Any and all help is most welcomed.

  2. #2
    My typical method is to get the position then add to its X/Y/Z to move it as desired.

    local pos = myShip.getPosition()
    pos = {pos.x+10, pos.y, pos.z}
    myShip.setPosition(pos)

    As for using translate, wouldn't it be myShip.translate({10,0,0})? I think you just forgot the ()

  3. #3
    Quote Originally Posted by MrStump View Post
    My typical method is to get the position then add to its X/Y/Z to move it as desired.

    local pos = myShip.getPosition()
    pos = {pos.x+10, pos.y, pos.z}
    myShip.setPosition(pos)

    As for using translate, wouldn't it be myShip.translate({10,0,0})? I think you just forgot the ()
    Yes using setPosition is better because of what I need to do next. Right now it only moves my model in a given absolute direction. What I need to do now I get it to move in a relative direction based on its current rotation.
    Code:
    myShip ={}
    function click_S1()
    	local LocPos = myShip.getPosition()
    	local LocPos.x = math.sin( math.rad( myShip.rotation))
    	local LocPos.y = math.cos( math.rad( myShip.rotation))
    	LocPos = ({LocPos.x,LocPos.y+0.5,LocPos.z+3})
    	print(LocPos)
    	myShip.setPosition(LocPos)
    
    end
    It been a minute since I took Trigonometry but changing LocPos.x and LocPos.Y to use the sin and cos of that angle should move it along the X and Y axis to offset it a general distance, I think.

  4. #4
    Code:
    --Finds a position, rotated around the Y axis, using distance you want + angle
    --oPos is object pos, oRot=object rotation, distance = how far, angle = angle in degrees
    function findGlobalPosWithLocalDirection(oPos, oRot, distance, angle)
    	local posX = oPos.x + math.sin( math.rad(angle+oRot.y) ) * distance
    	local posY = oPos.y
    	local posZ = oPos.z + math.cos( math.rad(angle+oRot.y) ) * distance
    	return {x=posX, y=posY, z=posZ}
    end
    This is my generic "find a rotated position relative to the rotation of an object" script.

    In your case, oPos and oRot are the object's position and rotation. Distance is how many units to move it. Angle may vary. If you use 0 for an angle, it will use the object's forward position, so if you were making a thing that moves forward, relative to which way it is facing, then angle = 0. Its left would be -90, its right would be 90, and backwards would be 180.

    You may need some variant if I'm not understanding your intention here. So here is a nice easy summary page for commonly used trig: https://wiki.garrysmod.com/page/Trigonometry

  5. #5
    Quote Originally Posted by MrStump View Post
    Code:
    --Finds a position, rotated around the Y axis, using distance you want + angle
    --oPos is object pos, oRot=object rotation, distance = how far, angle = angle in degrees
    function findGlobalPosWithLocalDirection(oPos, oRot, distance, angle)
    	local posX = oPos.x + math.sin( math.rad(angle+oRot.y) ) * distance
    	local posY = oPos.y
    	local posZ = oPos.z + math.cos( math.rad(angle+oRot.y) ) * distance
    	return {x=posX, y=posY, z=posZ}
    end
    This is my generic "find a rotated position relative to the rotation of an object" script.

    In your case, oPos and oRot are the object's position and rotation. Distance is how many units to move it. The angle may vary. If you use 0 for an angle, it will use the object's forward position, so if you were making a thing that moves forward, relative to which way it is facing, then angle = 0. Its left would be -90, its right would be 90, and backwards would be 180.

    You may need some variant if I'm not understanding your intention here. So here is a nice easy summary page for commonly used trig: https://wiki.garrysmod.com/page/Trigonometry
    Right now I am looking for a way to get the models current angel that I then can plug into the function. However, every time I ask for myShip.getRotation it gives me a table of rotations and I really only need the rotation of the Y axis. Right now I am reading up on how to isolate that one varable.

    I am comfortable with sin/cos/tan functions as far as the math goes but I am having issue with how they are syntactically expressed in LUA

    so in your example what exactly is "angel+oRot.y" because as is it keeps putting out a nil value?

    EDIT

    Ok after pen and papering the math what I need is
    Code:
    pos = getPosistion()
    angel = --models current angel in deg
    Distance = 3
    posY= math.sin(math.rad(angel))/3
    posX= math.cos(math.rad(angel))/3
    setPosition(posX, 0.5, posY)
    Last edited by CrazyVulcan; 04-16-2017 at 05:33 PM.

  6. #6
    Hey stump... how come when moving things.. sometimes it just "jumps" to the position and other times it actually physically "moves"
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

  7. #7
    setPosition and setPositionSmooth. If you're doing takeObject, it tends to vary which it uses sometimes dependent on situation.

  8. #8
    Thanks MrStump for all of your help, I have the straight movement working correctly and now just need to do the same but along the arc of a circle. But I have learned a lot today and think I should be able to get it to work.

  9. #9
    circle.jpg
    Ok so now I am trying to accomplish the same thing but now we are moving along a circular path for a certain distance. The issue is I can not seem to find any resources to solve this math problem. Least not with the numbers that I know already.
    arcC = ?
    angel.y = the y rotation of the starting pos found from| local angel = myShip.getRotation()
    cX = ?
    cY = ?
    pos.x = got from| local pos = myShip.getPosistion()
    pos.y = got from| local pos = myShip.getPosistion()
    A = math.rad(45)
    r = 3
    newX = ?
    newY = ?

    Basically I need to solve for newX, newY and first I need to find cX and cY for the center of the circle where I do no always know what the angel.y is already. IE every time I have tried to move the object to the side it keeps changing based on angel.y So far I have
    Code:
    function click_BL1()
    	local pos = myShip.getPosition()
    	local angle = myShip.getRotation()
    	local forward = 4.25
    	local side = 2
    	forX= math.sin(math.rad(angle.y))*forward
    	forZ= math.cos(math.rad(angle.y))*forward
    	sideX= math.sin(math.rad(angle.y))*side
    	sideZ= math.cos(math.rad(angle.y))*side
    	myShip.setPosition({pos.x-forX,pos.y+0.5,pos.z-forZ})	
    	local pos = myShip.getPosition()
    	myShip.setPosition({pos.x-sideX,pos.y+0.5,pos.z-sideX})
    	myShip.setRotation({angle.x, angle.y-45, angle.z})
    end
    I have been playing with the exact numbers so dont worry about that I just need help with the method.

  10. #10
    I am a bit confused by your intention here.

    So before you were moving a piece X distance forward using its forward rotation, correct?

    So now is it still moving X distance but also rotating as it moves?

    Like, on a grid of 1x1 spaces. Are the option types "move forward 1 cube" or "move forward 1 cube and right 1 cube"? Or is it moving forward exactly 2 units as it turns, leaving it not centered in that imaginary 1x1 grid anymore?

  11. #11
    Sorry for the poorly worded question. I have a model myShip, it is located at coordinates posX, posY. I need to move it to newX, newY. This path in along the arc of a circle whose radius is still unknown at the moment. but I do know that from the center point it will travel along that arc 45 deg to its new position. I then plan to rotate the model 45 deg to be in line with its new position. However, all the resources I have been able to find to calculate this involve knowing the coordinates of the center of that circle and I can not seem to find it but I know I am getting close. Originally I have been trying to move it sideways X distance and then forward Y distance but I am having trouble getting it to land where I want as the models starting rotation can vary.

    I have got it to move sideways the correct distance, and move forward the correct distance and even rotate the correct way. all regardless of myShips starting angel.y however when I try to get it all to work together it kind of falls apart as if angle.y=math.rad(45) lands in a different spot relative to its starting pos then if it was angle.y=math.rad(15). So that tells me I am on the right idea but again am having an issue with the syntax of how LUA needs the information.

    I know this method should work but it is almost if TTS is trying to do it all at once. Or at least add the rotation before finishing the move. I will try to add a wait command and see if that fix's the issue.

    Code:
    --the distance's I don't know at the moment but hopefully, all I need to do is adjust these till the model lands in the right spot.
    local forward = ?
    local side = ?
    local pos = myShip.getPosition()
    local angle = myShip.getRotation()
    local forX= math.sin(math.rad(angle.y))*forward
    local forZ= math.cos(math.rad(angle.y))*forward
    local sideX= math.sin(math.rad(angle.y))*side
    local sideZ= math.cos(math.rad(angle.y))*side
    
    --first I will move it to the side, and this will always move it to the right side, as I am wanting
    myShip.setPosition({pos.x-sideX,pos.y+0.5,pos.z-sideX})
    --next, I need to move it forward relative to the angle it is facing, like with the moveY
    myShip.setPosition({pos.x-forwardX,pos.y+0.5,pos.z-forwardZ})
    and now that the model has to be moved to its correct place I need to rotate it
    myShip.setRotation({angle.x,angle.y-math.rad(45),angle.z})
    like I said I will try adding a short wait command and see if it gets it to trigger properly. Each part works correctly individually but once combined they go all haywire.

  12. #12
    Never mind I have it working now. Just needed a better system for getting the needed measurements. In college I took a 300 level Trig course for fun but Geomentery always seemed to escape me lol.

    function click_BL1()
    local pos = myShip.getPosition()
    local angle = myShip.getRotation()
    local forward = 4.1
    local side = 1.65
    local forX= math.sin(math.rad(angle.y))*forward
    local forZ= math.cos(math.rad(angle.y))*forward
    local sideX= math.sin(math.rad(angle.y+90))*side
    local sideZ= math.cos(math.rad(angle.y+90))*side
    myShip.setPosition({pos.x-forX,pos.y+0.5,pos.z-forZ})
    local pos = myShip.getPosition()
    myShip.setPosition({pos.x+sideX,pos.y+0.5,pos.z+si deZ})
    myShip.setRotation({angle.x, angle.y-45, angle.z})
    end

  13. #13
    TTS moves in strait lines.. if you want it to physically move in a arc you need to plot a number of points along the arc and then move between them.

    Is that what this code is doing?
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

  14. #14
    Tragic, that was my thought if he was using setPositionSmooth for stuff. But he's using setPosition, so that's why I was confused haha Especially since he already knows the end point apparently.

    That's what I am confused about Vulcan. Did you want it to move smoothly over an arc? So you're trying to figure out ways to find points on that art to move it to?

  15. #15
    Quote Originally Posted by MrStump View Post
    Tragic, that was my thought if he was using setPositionSmooth for stuff. But he's using setPosition, so that's why I was confused haha Especially since he already knows the end point apparently.

    That's what I am confused about Vulcan. Did you want it to move smoothly over an arc? So you're trying to figure out ways to find points on that art to move it to?
    Well in the game the moving template was a curve but just plopping it down in its relative position accomplishes the same thing.

  16. #16
    So to define a few terms I will use, "local" refers to the position/rotation of your figure you want to move, "global" refers to the world's position/rotation system that is what setPosition and setRotation utilize.

    My understanding is you want to take a figurine, and determine if it moved forward on a curve, what global coordinates are needed to make that happen. You have some local coordinates of some kind to make this desired effect happen, and you need to convert the local information into a set of global coordinates.

    Assuming that is correct, what are your local coordinates you have? Do you know the local coordinates in x/z format? Like is it moving 2 forward and 1 right? Do you know a set distance it is supposed to travel only with a 45 degree arc? What information do you have that you are trying to convert?

  17. #17
    Quote Originally Posted by MrStump View Post
    So to define a few terms I will use, "local" refers to the position/rotation of your figure you want to move, "global" refers to the world's position/rotation system that is what setPosition and setRotation utilize.

    My understanding is you want to take a figurine, and determine if it moved forward on a curve, what global coordinates are needed to make that happen. You have some local coordinates of some kind to make this desired effect happen, and you need to convert the local information into a set of global coordinates.

    Assuming that is correct, what are your local coordinates you have? Do you know the local coordinates in x/z format? Like is it moving 2 forward and 1 right? Do you know a set distance it is supposed to travel only with a 45 degree arc? What information do you have that you are trying to convert?
    Don't worry about it, I managed to get everything moving exactly as it should. It was much simpler then I was thinking it would be. I just have ONE LAST question MrStump and it involves GUID's

    I have A control board and it is hard coded to be linked with one ship model. But with Attack Wing I have as many as 80+ ships associated with the game and they each have a unique GUID. These ships are also located in separate tables and players bring them from being saved into their chest to the table that has the control panels.

    Now I need to find a way to link which controls panels go with which ship. To do this I have created a button on each Pannel called Sync. What I need is a function that,

    First when the Sync Button is clicked or onload finds the GUID of itself for each instance of the Controle Pannel and updates the variable "local ControlPanel_GUID = [the new GUID] "

    Second, it needs to look at the NickName of itself and then it needs to locate somewhere on the table a custom model that has the exact same name as the panel.

    Third I need to get the GUID of that custom model and in the LUA for the Control Panel update the variable "local myShip_GUID = [that models GUID]"

    This needs to be somewhat flexible so players can't break it if they say copy and past one of the Control Panels or one of the Ships. And as I said there are over 80 ships each with their own GUID.

    I am almost there guys, thank you all so much for your help. There are a lot of Attack Wing players that are excited to see this implemented and are very excited by it.

  18. #18
    For first and second, to me, the easier way would be to just put the object in a set position on top of the unit (make a space for it), spawn a scripting zone, and locate the object that way. Or use onCollisionEnter to keep track of the last object to touch the panel (and make it check the .tag of the object to prevent it from, you know, moving the table lol)

    For 1 and 2, either way you do it, you just need to set a NON local variable. myShip_GUID = "". Then any time that button is hit again, it overwrites that value. And that value is recording using the onSave and onLoad functions, so the pad will always remember which ship it was tied to. And when you use getObjectFromGUID(myShip_GUID), make sure you check that it doesn't equal nil. If it equals nil, it means the ship it WAS tied to is gone and it should forget it.

    On this table, I made some examples of locating objects http://steamcommunity.com/sharedfile.../?id=879494019

    The options of using GUID to locate it in the first place is out. Using a name is out, since people may change names. Using custom model information is out, due to different models being used. So you're left with locating objects using other information. Are the models always set to be "Generic" or "Figurine" as a object.tag? If not, then a temporary spawned scripting zone in a specific location your indicate on the board and using THAT to find it is probably your best bet. Finally, if all that isn't good, I included some basic distance example of finding objects by distance. You could check the distance of every object that isn't that board and then "attach" the guid of the closest item to it. Or use onCollisionEnter to detect the last new object which came in contact and use that.

    Basically, you've got a lotta options

  19. #19
    Thanks, MrStump those are really amazing resources. And I got it to work and everything but now I have run into a problem that I am mostly sure is an issue of syntax. I have updated the myShip varable to equal the GUID of the object placed on top of the card. However I am having issue with linking that back into the code that gets it to move the ships. What excatly do you mean by onSave() and onLoad()? I have that function but I only have it place the buttons I need. Do I need to re trigger the onSave() function? If you could please clarify that.

    What I think the problem is is that yes I have a var myShip, but when I add myShip = {} and its functions below it I get a nil value error. I have seen examples of doing myShip =({ functions go here }) but that seems to break the script as when I save all my buttons dissapear.

  20. #20
    Check out that example table for onSave and onLoad. There is also the Learning More Lua guide that goes with the table to explain the examples a bit.

    Basic idea is you make a non-local variable of myShip_GUID. Then anywhere in the script after you establish it, you can do print(myShip_GUID) and it will print that GUID, because it remembers what that variable equals. But if you hit undo, or reload, or save/load the table, it will forget what myShip_GUID was. The script doesn't have a memory. That is where onSave and onLoad come in. With onSave, it makes the script remember what you tell it to. In this case, myShip_GUID. Then in onLoad, you make it remember. So from then on, it will keep that information in its memory.

    Note that you cannot onSave an ENTITY REFERENCE though. So you'll need to save its guid.

    As for your syntax, ({}) is way off base. You need to locate the GUID of a given object. So you need to get that via one of the prescribed methods. Once you can use print() to print the GUID of the ship you are trying to attach to the board, then you can just do myShip_GUID = whatever_you_used_print()_on

  21. #21

    Hey, MrStump, I want to say thank you for all of your help, just got the update loaded into the workshop. I don't think I could have done this without your help. Well, maybe but there would have been a lot more crying and self loathing for being a idiot. Again Thanks

Similar Threads

  1. [SOLVED] Controller Issue
    By crazycdchris in forum Technical Support
    Replies: 2
    Last Post: 02-10-2017, 09:38 AM
  2. Replies: 11
    Last Post: 02-03-2017, 05:39 PM
  3. [SOLVED] (v7) Camera/Board moving on its own.
    By Mitsuma in forum Technical Support
    Replies: 3
    Last Post: 04-05-2016, 01:54 PM
  4. [SOLVED] Xbox One controller bug
    By tboisson in forum Technical Support
    Replies: 5
    Last Post: 01-10-2016, 09:33 AM
  5. Replies: 0
    Last Post: 08-31-2015, 04:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •