#Object swaying on cursor?

1 messages · Page 1 of 1 (latest)

thick bolt
#

I have an object that the player can drag around that I want to add a little flavor to. I want the chess piece to kinda dangle while the player is holding it so that it tilts around a pivot point on the object. The object moves to the cursor by grabbing the mouse's position and setting the transform position, shown in the 2nd picture. I thought I could do this by adding some sort of physics to the object, so that it naturally sways with the momentum of dragging the object around, but I'm not completely sure that would work. Is there a better way to do this?

opaque jewel
#

just brainstorming a solution. idk if this'll work perfectly but its what i could come up with in the moment
pseudocode:

// adjust values as needed
float gravity; // gravity will affect how fast it swings
float drag; // how quickly the piece settles and stops swaying, 0 to 1
float mouseVelMult; // adjust mult to make the piece seem heavier or lighter

float sway = 0; // angular vel
Vector2 prevMousePos;

Update() {
    // x just for side-to-side sway
    float mouseXVel = (curMousePos - prevMousePos).x / deltaTime;
    float curAngle = transform.rotation; // assuming chess piece's pivot is the grab point
    
    // fake pendulum physics

    // -mouse vel because piece should swing in the opposite direction the mouse is moving
    sway += -mouseXVel * mouseVelMult;
    // inverse sign (1 or -1) of the cur angle so gravity is always pushing towards angle of 0
    sway += (curAngle < 0 ? 1 : -1) * gravity * sin(curAngle);
    // drag is scaled based on sway to keep it tending towards 0
    sway += -sway * drag;

    // prevents sway from wiggling between super small values
    if (abs(sway) < 0.01f)
        sway = 0;

    transform.rotation += sway * deltaTime;
    float newAngle = transform.rotation;

    // clamp rotation to prevent it from spinning around wildly
    transform.rotation = Clamp(transform.rotation, -maxAngle, maxAngle);
    
    // if rotation was clamped, sway should be stopped to prevent floating
    if (transform.rotation != newAngle)
        sway = 0;

    prevMousePos = curMousePos;
}
#

rn youre offsetting the piece which will make swinging a real pain to do. Id suggest parenting whatever piece youre holding to an invisible parent that follows the mouse, and then rotating that parent, and making the offset in localPosition

#

assuming you want something like this

thick bolt
#

I don't have the time to look into whether or not this will work, but I'll check it out tomorrow to see if it does

thick bolt
#

@opaque jewel I did this and it seems like its kinda working, it just only moves by like 1 degree on the z axis

#

which obv isn't right

#

i had to do some adapting to your code though, and I don't know how quaternions work

#

so that mightve messed something up

#

its still a step closer though!

#

wait hold on i might be dumb 😭

#

ok yeah i just set the maxAngle variable to 1.0 and then forgot about it, but its still wonky

#

i can do some testing

opaque jewel
thick bolt
#

I'm using 3d, im just treating it like it's 2d because I want to do some stuff with that later on

earnest inlet
#

If you want to make it physics based, a solution with a rigidbodies and joints shouldn't be too hard. I haven't looked into it but also a proper code based pendulum simulation I would bet is quite straight formard especially in 2D