#Inflating causes player to spin

1 messages · Page 1 of 1 (latest)

toxic relic
#

Yo! I have a project from school where we have to make a game using one input. So I was thinking about replicating inflating from little slime blows up. I analysed the video and some more videos about soft body and I got my player to inflate! But I got some problems:
1: When I inflate, sometimes the player just spins.
2: Inflating while moving the character also spins the player.
3: I'm not sure about whether using rb.AddForce(f, ForceMode2D.Impulse) or rb.AddForce(f, ForceMode2D.Force).
Should I calculate Vector2 dir = (rb.position - center).normalized or do something with the radius?

At the start of the game I spawn circles with rigid body 2d, circle colider 2d and springs. Then I connect the springs.
When the player presses the space bar, bJumpButtonPressed is set to true and then in fixed update I calculate the direction vector:
I get the position of the rigid body of the circle - the centre point of the player (I have uploaded a picture of this vector * 10 in the red line).
and as long as you hold the space bar, you stay in the inflating state.
When you release, the circles should stop inflating and return to where they were before. (which they do)

The problem? Sometimes the player just spins itself, causing the inflating to move the player left or right (which I don't want). And that's without mentioning the fact that I'll need to move the player right which makes the spinning even more problematic

Scripts:
Spawning the circles and connecting the spring joints: https://paste.mod.gg/ohhzsonauhds/0
Inflating the player and moving it: https://paste.mod.gg/gcqnreqyvtie/0

heady copper
#

A video would be super helpful. But spinning while moving here is likely related to friction

#

a ball-like object such as this will tend to roll

toxic relic
heady copper
toxic relic
#

I know that there are ruls here to sharing code right?

#

like with !code

#

!code

peak laurelBOT
heady copper
#

Yeah read this^

#

and share your code like that

toxic relic
toxic relic
toxic relic
#

I have coding experience with ue5 (both C++ and blueprints)

#

So if my practices are not perfect then please tell me so I can write better code

toxic relic
#

@heady copper Do you know what's the problem?

heady copper
# toxic relic <@179367739574583296> Here is a video

I think at some point just due to floating point inaccuracy you're pickingup a slight tilt to one direction or another, and then the expansion force is amplifying that. And then your code uses the average position of the circles to calculate a lot of stuff which gets really thrown off especially when part of the whole thing is compressed

#

It's hard to say - but I think you have a lot of small floating point error that gets amplified and thrown back through the feedback loop

#

Also this:
rb.AddForce(delta * (50f * Time.fixedDeltaTime), ForceMode2D.Force);
is wrong in a couple ways

#
  1. Time.fixedDeltaTime should never be multiplied into the force. It's automatically done so when you're using ForceMode2D.Force
  2. 50f * Time.fixedDeltaTime is usually just 1, basically cancelling it out anyway. Because fixedDeltaTime by default is 1/50 (0.02)
#

so I recommend just getting rid of the whole (50f * Time.fixedDeltaTime) term here

toxic relic
#

@heady copper I see but that's not the inflating part the inflating part is in the first section of the function ApplyInflateAndFrameForce()

heady copper
toxic relic
#

not pressing

heady copper
#

I really just think this is a matter of like:

  • It gets some slight angle from floating point error
  • then you're applying forces in or out of the center while it's moving slightly which turns into torque
#

and it's just a feedback loop that gets stronger

#

As for how to fix it I'm not terribly sure other than maybe intentionally applying your own stabilizing torque

toxic relic
#

@heady copper Do you think making the force as an int will fix this?

heady copper
kind tangle
#

Quick fix: manually set the horizontal velocity to 0 until the player starts moving. After that it's not a problem anymore because there's already sideways movement and any small aberrations disappear. When the rigidbody goes to sleep (=stops moving) start limiting the horizontal velocity to 0 again

#

or freeze the x axis

heady copper
#

the problem is it's a bunch of small Rigidbodies

#

and part of the expansion and contraction involves most of them moving sideways to some degree

kind tangle
#

hmm yeah

#

so maybe setting the parent x position

toxic relic
#

Hold up

#

@heady copper What if we look at it from the perspective of a radius?

#

This is when radius is 1.05

#

And this is when radius is set to 5

#

@heady copper So armed with this knowledge, What if we can just set the radius

toxic relic
#

@kind tangle What do think?

kind tangle
#

Frankly I don't see what you're getting at or how it would help