Moving an object to a waypoint with physics?

0 favourites
  • 13 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hi everyone, I have a simple test where i want to do a kind of spy hunter style driving game but i want to move the vehicle around with physics not just "move to" stuff. I'm having some success but the biggest problem is that i can't figure out how to slow down the approach?

    my basic approach at this point is to compare the Y of the destination and the object.. this pic below is the basic approach but i did one better where i made the distance value be a variable to the impulse, but it didn't seem to make much difference. the idea being the further away it was from it's destination the more impulse and as it got closer less impulse.. but it would still effectively overshoot about the same.

    basically what happens is the object just keeps going back and forth overshooting the destination. like a sine wave behavior.. up down up down..

    I've been banging my head at this for awhile before i came to the forum. this is just Y but i believe if i can get this behavior under control i'll be able to apply the same to X. Just understanding how to throttle directional speed with impulse effectively.

    Thanks!

    Caleb

  • You need to change the impulse intensity depending on the distance to the target. Try something like this:

    Apply impulse X=0, Y= clamp((DestinationY-Car.y)/100, -1, 1)

    Of course, you'll need to experiment with the numbers.

  • that's awesome. i've not used clamp before. will research that some for sure. Thanks!

  • ok so using the clamp approach worked but ultimately has the same result.. a sign wave effect.. the target overshoots the destination and comes back then overshoots again.. makeing the number bigger or smaller just makes the wave action less tight.. 100 50 or even 5 make no difference.. but going to 500 for example makes the width wider.. but no less "overshot" like smaller values.. just slower..

    maybe some kind of increase of "counter impulse" should go on as it gets closer to the center point? but it seems like if maybe the impulse just cuts down..

    friction seems to have no effect on this either.. i thought higher friction might make it take more impulse to move but it seems to have no "slowing" effect.

  • is there some kind of math formula that could make a value increase at some exponential level? my math fails me at this level..

    it feels like it needs to be more of a curve.. not an even slope of impulse but rather more gradual bell curve.. i want MORE impulse as it gets further away but much less as it gets closer.. even some possibly negative impulse to slow down the travel to the destination point.

  • You need to add some margin, because it will never come to the exact point you are aiming for. For example you want car to go to Y=100, car.Y can be at 100.015469, but it will never be exactly 100. So you need to stop the car when it's between Y 99 and 101 (you can experiment with margins to see what works for you).

  • how do margins work? i do understand physics being physics in game will never be 100% tight.. i don't even mind a little overshoot but right now it's pretty much 100% overshoot.. perfectly going back and forth.. never getting closer to the target.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It depends on how often you apply the impulse, if it's every tick - I would imagine dop2000's code should get the Car close to your Destination point and then overshoot back and forth within few pixels. So you should stop the code when DestinationY-5 < Car.Y < DestinationY+5 . Easiest way to do this is with conditions:

    Car.Y > DestinationY-5 and Car.Y < DestinationY+5 then do nothing

    Else Apply impulse X=0, Y= clamp((DestinationY-Car.y)/100, -1, 1)

  • Ok i see what you're saying now about margins i think. i'll give it a try and see how it goes! Looks promising!

  • it's still doing the same basic thing.. i have an audio clip to let me know when the impulse is firing off.. but i can hear that the tolerance is still working as the sound turns off while in the acceptable range so it's working. this is what it looks like:

    dropbox.com/s/tvivt775zi17x2d/physics%20question%202.JPG

    NOTE: for some reason i can't upload a picture today so i'm using dropbox.

    It still does the same back and forth never getting closer to the target. there has to be some way to reduce the impulse value drastically faster than a steady downward slow (which is what you get when you simply apply an impulse value based on distance difference)

    the "tollernace" var is just a value set to i think 25 right now.. mostly just so i could hear the pause in audio triggering.. but it doesn't make any noticable difference if i change it to a tighter value like 5.. but also it actually has no effect if i make it 100 either..

    I've noticed also that friction doesn't seem make any difference either.. which is odd. i'd have thought friction would fight impulse.. or at least hinder it's effectiveness.

  • maybe someone knows an elegant way to some how measure an objects velocity (has to factor in X and Y, which tends to get me tripped up) as it gets closer and to introduce a smaller counter impulse? I'm going to try some stuff but basically it seems that the impulses are great moving to the right destination but nothing is slowing it down.. i just don't understand why i can't seem to get any friction.. no matter what i set my car's friction too it still seems to just glide.. i have world gravity set to 0 for now (since it's top down).

  • ok linear dampening seems to be helping. but i'm going to see how it goes with moving to an X and Y position, but i get it now.. friction is probably how items rub against each other on their sides.. not it slid on a surface.. that makes sense because this is a 2d game engine..

  • Ok really it wasn't even the clamp.. margin stuff was good though.. but ultimately linear dampening was the key to getting things to calm down when they get closer..

    dropbox.com/s/dgqz048gxv5jn8u/physics%20question%203.JPG

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