Rotate/Constrain to Actor Problem/Question

EricTippettEricTippett Member Posts: 45
edited July 2012 in Working with GS (Mac)
Hey Everyone,

So right now I have basically a tank bottom and a tank top. These work fine, the tank bottom is what moves and the tank top rotates and is constrained to the tank bottom.

However, now I need to constrain 5 turrets side by side to the tank top and on the right of it, so like 10 px from the center.
Right now I have them constrained and I made them be 10 px to the right of the top (default position before player controls it). However, when I rotate the top, the turrets dont move, they just stay in their position.

So my question is how would I constrain these turrets to a certain point of the Tank Top and not just the center (or any other way of achieving this effect).

So just to draw a better picture in case this doesnt make sense. There is a Tank bottom that moves, a Tank Top that rotates and 5 Turrets that are side by side on top of the Tank Top. At first the Turrets are facing to the right, but when I rotate the Tank Top they should move and rotate to where the Tank Top is facing.

Any ideas? I watched all of the rotate tutorials but nothing covers this problem.

Best Answers

  • NconanNconan Posts: 6
    Accepted Answer
    If I understand your question correctly, you want the turrets to keep their relative position to the main turret as it rotates? If so, this should work for you with some tweaking:

    You'll need two constrains on the extra turrets, one for their x position and one for their y.

    For the x, you'll do: [main turret position x] + [distance] * cos( [main turret rotation] )

    The y is similar: [main turret position y] + [distance] * sin( [main turret rotation] )

    Anything in brackets represents a variable, but it should be straightforward. Basically what this'll do is constrain the extra turrets position to a point along a circle around the main turret, based on the main turret's rotation.

    However, I used this to create the floating hand sort of thing in a Thing-Thing clone/prototype I had been working on, and it's set up so the object will basically be constrained as close to the mouse cursor as possible. With that in mind, it should just need some kind of offset to the rotation to work for you. If you have any trouble, just let me know.
  • NconanNconan Posts: 6
    edited July 2012 Accepted Answer

    Constrain Attribute: Self.Position.Y To:Player1TopYPosition*sin(game.Angle1)
    That would appear to be the problem. It should be: Player1TopYPosition + 10*sin(game.Angle1)

    Or is that just a typo?
    Do you have an unlocked actor in the scene to deal with the camera origin?
  • NconanNconan Posts: 6
    Accepted Answer
    Have you tried having the constraints active outside of the rule? It looks like you have the same thing in both the rule and the otherwise.

    Of course, from what you're saying, it sounds like Angle1 is constrained to the thumbstick, but it's rotation sets to zero if it is released (Maybe unintentionally). Does that sound about right?

    If that is the case, you could try constraining only when the thumstick is being used, with an empty otherwise. I don't know exactly how you've set up your thumbstick, so I could be wrong, but it doesn't seem like you're too far off now. Just look over how Angle1 is set and see if you notice anything.
  • NconanNconan Posts: 6
    Accepted Answer
    This is where the offset I mentioned earlier comes in. From the sound of it, I believe you'll need an offset of 270 (or -90) to get the turret to the right and 90 to get it to the left. Choose one of these to be your 'base' offset (no variable needed), and add that value to Angle1 in both constrains.

    From there, create an integer variable within the turret actor to be used as a multiplier for the side the turret's on. For this, I'm going to use 270 as the offset. That means that the integer will be 1 for the right side turrets and -1 for the left side ones. Just multiply the 15 by the multiplier variable in both constrains and that should set the left ones to the opposite side.

    This may throw off the rotation of your side turrets. If it does, just use another offset on the self.rotation setting of the turrets. It should be a multiple of 90 again, but may be different for left and right. Create a variable for the offset if you want.

    For setting the distance, just create another variable to act as another multiplier based on which "slot" the turret is. Set a value of 1 for slot 1 and 2 for slot 2. Then multiply the 15 by this value as well (order of operations shouldn't matter since it's all multiplication). You may have to set the variable to a value of 3 for slot 2 to get the spacing you want.

    All this is assuming you already have the turrets in-game, but inactive rather than spawning them in. If you're spawning them in currently, that makes things a bit more difficult, so I'd recommend just keeping them offscreen and having their behaviors only active when that turret is active, ie "If game.turret count >= self.turret number" or something. You can still do it by spawning, but you'll have to do some trickery to set the variables for the turret when it's spawned.
  • NconanNconan Posts: 6
    Accepted Answer

    game.Player1Top Y Position + 35*sin(self.Rotation +40)
    That doesn't look entirely correct, but if it's working for you, then I see no problem.

    But to clarify, what I meant was more like:
    Player1Top Y Position + 15*[Side Multiplier (the integer)]*[slot number]*sin(self.Rotation +offset)

    The side multiplier makes it so it adds to the position if it's right (1) and subtracts if it's left (-1). This puts them on opposite sides. The slot multiplier basically increases the radius that the turret would orbit the center around. I'm not sure why 40 is your offset there, it could have to do with your existing code, but again, if it's working for you, it doesn't really matter.

    As for the snapping, I actually wasn't entirely sure until just now. It seems to be because the thumbstick initially has a value of zero when it's touched. After quickly playing with the Cross-Platform Controller Template, I found that if you add in the slightest delay to the Stick Active variable getting set to true (either with a timer or a timer work around), it seems to work perfectly. Try that out an see how it works.
  • NconanNconan Posts: 6
    Accepted Answer
    So yeah, now I am just working on having the bullets come out of the gun instead of the weapon slots and then having the gun stick into the slot when they choose it. So I am guessing I will have to get the x and y of all 5 weapon slots and then constrain the weapon to that.

    Or maybe I could just put a bunch of Rules in each slot that if certain conditions are met, the graphic of the slot would change to the gun and the bullet would change to a certain bullet.. that may be a better way.
    Missed this post initially, my bad.

    There are a lot of different ways you could do it. Spawning may be the simplest way to do it, but performance may take a hit if the fire rate is high. Recycling bullets may take some effort to implement, but could help down the road. Though doubling up on constrains for every turret may hurt your performance as well. You could try 'simulating' bullets, and basically just setting it so that when you're firing, the game comes up with a percentage chance of hitting the enemy and just uses that chance to decide if the enemy was hit each "shot."

    There are so many different ways you can do it, and it largely depends on how you're setting up your game. I'm probably not gonna be able to help you as much here, it'll just take a bit of trial and error and playing around with ideas to figure out what'll work best.

Answers

  • EricTippettEricTippett Member Posts: 45
    And I should add that the current way I am doing this, is the turrets do rotate.. but they do not move so they just stay stuck to the right. So thats the main problem, them not moving.
  • EricTippettEricTippett Member Posts: 45
    Hey thanks for helping me out.
    So yeah thats the idea, I am making 5 Turrets on top of the Tank Top which is on Top of the Tank Bottom. Later they will be Turret slots and I would equip turrets into them so the player can change them.
    But I did what you wrote and I am just doing 1 turret right now. It isnt staying on top of the Tank Top, instead it is way at the bottom of the screen and when I move the joystick up it comes up to the ship. I am using a joystick to shoot and that is what game.Angle1 is by the way.

    So right now I have
    Constrain Attribute: Self.Position.X To:Player1TopXPosition +10*cos(game.Angle1)
    Constrain Attribute: Self.Position.Y To:Player1TopYPosition*sin(game.Angle1)

    What did I do wrong?
  • EricTippettEricTippett Member Posts: 45
    Ah ok yeah I fixed that and now its working, I see what its doing now.

    Yeah I made my camera follow the Player1Base, which is the bottom.

    So now when I shoot it looks perfect. However when I dont shoot, it snaps back into the default position while my turret is facing the correct way (way the joystick last faced)

    So I did this

    Rule Fire with Thumbstick 1

    Attribute Game.stickisActive is True

    self.Rotation to game.Angle1
    Constrain Attribute: Self.Position.X To:Player1TopXPosition +15*cos(game.Angle1)
    Constrain Attribute: Self.Position.Y To:Player1TopYPosition+15*sin(game.Angle1)
    Timer (fire info)


    Otherwise
    Constrain Attribute: Self.Position.X To:Player1TopXPosition +15*cos(game.Angle1)
    Constrain Attribute: Self.Position.Y To:Player1TopYPosition+15*sin(game.Angle1)



    I am guessing somehow I have to tell the otherwise to look at the last angle made by the joystick, but I have no clue how to type that in. Also, I do need the otherwise, or else when I move the turret slots get left behind.
  • EricTippettEricTippett Member Posts: 45
    Yeah I moved the 2 constraints from otherwise to out and it does the same thing.

    If I dont have them at all, the turret gets left behind when moving, but will snap back when I shoot.

    So I changed it to this

    Otherwise
    Constrain Attribute: Self.Position.X To:Player1TopXPosition +15*cos(self.rotation)
    Constrain Attribute: Self.Position.Y To:Player1TopYPosition+15*sin(self.rotation)


    So now it works but every time I drag the joystick after I let go, it quickly snaps back but still works. I guess its not that bad, but its just a graphic thing.


    So that was for my middle turret. Now I am going to add 4 more. So 1 to the left of the middle turret, 1 to the left of that turret, 1 to the right of the middle turret and 1 to right of that turret.

    I was looking for a way to +5 so I can just move them over, but the constrain attribute would just make them be in front instead of the sides.

    Then i tried constraining the Middle Turrets position so i could get its position and just +5 to the x or y but that wasnt working for me either because it doesnt really follow it, just stays in the default position.

    Do you know the best way to do this?

    Thanks so much for helping me by the way!
  • EricTippettEricTippett Member Posts: 45
    Ah okay its working now, I was trying to follow what you were saying but I got kinda confused.
    Is this what you were talking about? This is what I just did and it works well.

    In the constrain attribute I would have
    To:game.Player1Top Y Position + 35*sin(self.Rotation +40)

    Do you know why when I first touch the joystick, the top turret snaps back into default and then follows the joystick? It just makes the ship look funny when it keeps snapping back into default for alike half a second after I move the joystick.
  • EricTippettEricTippett Member Posts: 45
    edited July 2012
    So yeah, now I am just working on having the bullets come out of the gun instead of the weapon slots and then having the gun stick into the slot when they choose it. So I am guessing I will have to get the x and y of all 5 weapon slots and then constrain the weapon to that.

    Or maybe I could just put a bunch of Rules in each slot that if certain conditions are met, the graphic of the slot would change to the gun and the bullet would change to a certain bullet.. that may be a better way.
  • EricTippettEricTippett Member Posts: 45
    Ah it worked after I added a every .01 sec timer. Thanks!
  • EricTippettEricTippett Member Posts: 45
    Yeah I guess I should try to build this to my device soon and see if there is any lag or long load times or something I am unaware of.
Sign In or Register to comment.