Physics issue with spinning actor after being shunted

RedCatRedCat Member Posts: 26
edited June 2012 in Working with GS (Mac)
Hi All,

So I'm experiencing an issue where my player actor (or other actors) will randomly spin around after being shunted by another actor, or any collision.

I can stop this happening by using "fixed rotation", but I really like the ability to shunt actors around the scene.

I'm using:

vectorToAngle( self.Motion.Linear Velocity.X , self.Motion.Linear Velocity.Y )

to control the rotation of the actor, which works fine, except when it's (I guess) been knocked off what it thinks is the actor's true rotation.

Is there a check, or reset I can do to allow for the offset that may be being caused by a shunt?

Answers

  • MobileRocketGamesMobileRocketGames Member Posts: 128
    edited June 2012
    I think you need to clarify what you want to do here and not use words like "Shunt".
    For our non-British friends:

    ~Shunt
    Noun:
    An act of pushing or shoving something.
    If I understand this correctly, the actors in your scene start to rotate after they have been hit by another actor. Using the fixed rotation attribute stops them from rotating... but this is not ideal because.... you like the ability to "shunt" them. Shunting is unrelated to rotation so this is where you've lost me.

    As for your formula:

    I'm using:
    vectorToAngle( self.Motion.Linear Velocity.X , self.Motion.Linear Velocity.Y )
    This is not the correct usage of this function. vectorToAngle finds the angle of a line drawn between two (or four) points (physical locations). So when you specify "self.Motion.Linear Velocity.X" and "self.Motion.Linear Velocity.Y" it is converting those numbers into actual locations.
    For example if the X velocity is 120 and Y is 37, it is thinking you are giving it the coordinate X:120, Y:37; it is drawing a line from X:120 to Y:37. Obviously this is going to give you unpredictable results.

    As an alternative, what you could do is constrain the rotation velocity to the sum of its linear velocity (with a modifier of course, so it doesn't rotate too fast or too slow). This would have the effect of it rotating fast when its moving fast, and as it slows down, the rotation also slows. The rotation would come to a nice smooth halt when the actor stops moving.

    The only issue with this is that you will always be rotating in the same direction, and so I would add an attribute (call it randRotate or something) to the actor(s) and after something collides with it, change that attribute to equal random(0,1). Add that to the rule for your rotation formula. It should look something like:

    ( Rule:
    Actor collides with specific actor or tag:

    Change Attribute:
    randRotate = "random(0,1)"

    )


    ( Rule:
    Attribute randRotate = 1

    Constrain Attribute:
    Angular Velocity = "( ( abs( (self.LinearVelocity.X) )+abs( (self.LinearVelocity.Y) ) )/2 )"

    )

    and

    ( Rule:
    Attribute randRotate = 0

    Constrain Attribute:
    Angular Velocity = "( ( abs( (self.LinearVelocity.X) )+abs( (self.LinearVelocity.Y) ) )/2 )*(-1)"

    )

    For example, your actor gets hit and randRotate rolls 1, the average of the two velocities equals 100, so it will rotate CCW with a velocity of 100 and when it gets hit again randRotate will roll again, if it lands on 0 and the average of the two velocities equals 160 it will rotate CW with a velocity of -160.
    Of course you will need to play with the density/friction/bounciness to get your preferred collision and movement style. You could also add all of that code to a rule that only happens when that actors collides with a certain actor, that way it is not constantly changing directions as it bounces around, although this is the most realistic way of doing things, as things will change directions mid-air in real life. You might also want to add some modifier to the average of the two velocities like *2 or *10 to get it rotating faster.

    PHEW! didnt think i would have so much to say about such a small topic!
    If you have any other questions or this didn't help you feel free to ask!

Sign In or Register to comment.