Constrain actor, rotation, and joystick issue

Hi,

I am trying to constrain a turret on a ship (main actor) with an offset that will allow the turret to always be at the front end of the ship no matter which rotation it is at. I have tried this formula I found here, but my control is a joystick.

constrain self.pos.x -> ship.Pos.X +(cos(game.Angle)*150)
constrain self.pos.y -> ship.Pos.Y+(sin(game.Angle)*150)

My joystick rule is when stick is grabbed = true
Then: Constrain self.rotation (of ship) to game.Angle.

The problem is that the turret is only constrained when I am holding down the Joystick which has a snap to center ability.

So if I let go of the joystick, the turret moves from its correct position.

Is there a way around this without changing my controls?

Thanks.

Comments

  • ericzingelerericzingeler Member Posts: 334
    edited August 2013
    If you're simply trying to constrain the position of the turret, than do this like so:

    Note: Assumes front of ship is at angle 0 from center

    ----

    If turret.pos.y = ship.pos.y:

    offset.x = displacement from center of ship actor along x-axis

    constrain outside of rules:

    turret.pos.x = ship.pos.x + offset.x * cos(ship.rotation)
    turret.pos.y = ship.pos.y + offset.y * sin(ship.rotation)

    ----

    If turret.pos.y not= ship.pos.y:

    offset.x = displacement from center of ship actor along x-axis
    offset.y = displacement from center of ship actor along y-axis

    change attributes on scene start outside of rules, before the constrain below:

    angle.offset (angle attribute) = vectorToAngle(offset.x,offset.y)
    distance.offset (real attribute) = sqrt(offset.x^2,offset.y^2)

    constrain outside of rules:

    turret.pos.x = ship.pos.x + distance.offset * cos(ship.rotation + angle.offset)
    turret.pos.y = ship.pos.y + distance.offset * sin(ship.rotation + angle.offset)
Sign In or Register to comment.