How can I change an object's direction while it coasts (retains inertia in Physics) ?

Not favoritedFavorited Favorited 0 favourites
  • 6 posts
From the Asset Store
Rotate & Animation for 16 Direction & Mouse Direction
  • I want to change the direction (vector) of an object that retains inertia after a force is applied. Example: a car's gas pedal is released, but you can still steer.

    The logic: if speed is greater than 0 then the player can steer. When I'm trying to create that system my object continues to spin or not turning.

    How can I change angular velocity (direction) while some inertia left (speed)?

    Here is my project: https://www.dropbox.com/scl/fi/4levq7x64aeb3lmswerxf/Inertia-001.c3p?rlkey=nv3zvksw1ckr6q4p5zis7rn58&st=iv7ldpyf&dl=0

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Inertia means it tends to keep going in the direction its moving. With a car it turns due to the friction on the tires opposing sliding sideways.

    Put another way, if you just apply forces and torque to the center of mass of an object you end up with floaty motion much like BB-8 or a vehicle that is rolling on one big ball.

    You can model a car much better by simulating wheels. Aka you'd apply force from the back two wheels and then apply a friction force to any sideways motion on each of the four wheels.

    Mouse up/down to adjust gas/reverse, Left/right to steer, and click for handbrake.

    dropbox.com/scl/fi/g9p9bdi5etv39r7jwz4tw/car_physics.capx

    Probably can be improved in many ways. One that comes to mind is instead of directly applying acceleration to the back tires we could just apply wheel spin and rely on friction to accelerate. Beyond that weight transfer per tire, but that may be delving more into 3d stuff. It would probably benefit by making a distinction between static and kinetic friction.

    I ended up not using the physics behavior but the idea can be applied there too. Here are some useful formulas as well:

    velocity along angle:

    vel: vx*cos(a)+vy*sin(a)

    velocity on point:

    vx: body.velx-body.angularVelocity*(pos.y-body.y)

    vy: body.vely+body.angularVelocity*(pos.x-body.x)

  • Wow, R0J0hound — Thank you! Your example is very complex, and that’s fascinating! It will take some time to fully understand. There are a lot of interesting solutions I wouldn’t have thought of. I’ll definitely use handbrake and tire burnout opacity that depends on velocity in my car controller.

    Actually, I already achieved what I consider perfect car movement, but I ran into trouble with hit/collision registration. Using Pin behavior I can transfer velocity, but I can’t transfer kinetic energy between the “engine” sprite and the “body” sprite after a collision. I heard a lot of times that mixing physics with other behaviours is a bad idea. Thats why I didn't continue work on this... maybe there is a good way to add physics collision and I don't know about it?

    Here is the my latest version without physics: https://www.dropbox.com/scl/fi/05k132mdw3v2gu1kykpde/Car-006.c3p?rlkey=9c4nla9jh9xd3gmvk6iaahtxd&st=sxbxq10a&dl=0

    After many attempts, I decided to use Physics behavior to get the Box2D physics simulator. Using Physics Joints can transfer velocity and kinetic energy between parts after a collision with another object, but I can’t get that “perfect” movement with Physics. After the gas pedal is released the car slides like soap and I can’t affect that vector...

    Here is the my version with physics and failed attempts (to show what's the problem): https://www.dropbox.com/scl/fi/mtiyyfx6i0b3cpmq0qt0d/Car-Physics.c3p?rlkey=2tcdn4952bnhquxrolddqr66d&st=0jhvvoi0&dl=0

    Indeed I can force it mixing with other behaviours or for example use sprite.angle and change it, but it's a bad practice.

    I will continue investigating your project...

    Is there any way to calculate Physics inertia Force after Force apply or convert Speed to Force?

    If so, I could check the Physics overall velocity > 0 and use an inertia force to apply in the needed direction.

  • I’ll have to look at your examples later.

    Most of the complexity of my example is replicating what the physics behavior does for you but I just like doing stuff from scratch.

    You can combine the physics behavior with other behaviors but I tend to prefer doing other things than working around behavior conflicts.

    Anyways, your physics terminology is a bit off. Presumably you could just set the angular and linear velocity of the car depending on if it’s moving forward or backwards and depending on the wheel turn. But that’s not really physics

    If you want to control everything with forces you’d do it with friction per tire.

    Inertia isn’t a force. It’s basically a property that controls how resistant an object is to changing its velocity.

    Maybe you’re just after finding a force to change the velocity in a specific way?

    You could get that by solving this for the force. Note you’d split that into two formulas for x and y.

    Force = mass * acceleration

    Force*time=mass*(velocityEnd-velocityStart)

    I guess time would be a timestep dt. But maybe it’s better to do that instantly so you could solve for impulse instead:

    Impulse = mass*(velocityEnd-velocityStart)

  • R0J0hound Sorry for the late reply. Discussion, your explanation and tips helped me achieve almost everything I needed. Thank you very much! Unfortunately, I couldn’t find a use for the force value I got from Force = mass × acceleration because it was too large to apply as an impulse or as a force at the specific angle. Maybe it’s my English, physics, or IQ, but I didn’t understand what you meant by (velocityEnd − velocityStart). Is that the velocity from one frame earlier?

    I ended up setting damping to 0 when no keys are pressed and manually applying friction (resistance), as you suggested earlier.

    The handbrake was surprisingly easy to create: all I had to do was change the linear and angular damping. After that it's starts to slide

  • Hi, no worries. Glad some of what I wrote was helpful. I haven’t been able to look into this further. There’s no urgency to reply to forum posts.

    Forces with the physics behavior are off by 50 times. So unfortunately a calculated force will be off unless it’s adjusted.

    What I mean by those velocities is say you want to calculate the force to stop an object in motion in one second. The start velocity can be anything and you’d want the end velocity to be 0 in that equation.

    So the formula would simplify down to:

    Forcex = -mass*velocityX

    Forcey = -mass*velocityY

    Now to make the units correct you have calculate the mass yourself since the expression is off.

    It comes out to something like this as I recall with the physics behavior.

    Mass = area*density = (width/50)*(height/50)/1

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)