# Calculating force necessary to reach arbitrary point

## Recommended Posts

Hello. This one's been bothering me for a week or so. I'm trying to write code that calculates the force necessary to reach an arbitrary point in space (for NPC movement). I tried over at the Newton forums with little response, but the problem isn't with the engine itself. I'm doing (pseudocode):
necessaryForce = (distance - (velocity-velocity*0.6)) *npcMass /delta

They're all floats. It is based on f=ma (subtracting current velocity from the velocity necessary to reach the waypoint to find the necessary change in velocity, then dividing by the physics engine's timestep, as acceleration=delta velocity/delta time). So "distance" is just the NPC's distance from the waypoint , velocity is its current velocity (in m/s), npcMass is the NPC's rigid body's mass, and delta is the physics engine's timestep (returned in seconds, so it's usually <0.015). The "-velocity*0.6" is my lame attempt to take friction into account, which actually seemed to work quite well. Now, it's pretty damn close, to the extent that it almost suffices (not for an action game, unfortuantely). The problem is that the force applied when the NPC approaches the waypoint (<1m away) is too low, so it kind of "eases" towards it. Also, the necessaryForce itself seems to always fluctuate between a very large and a very small value as the NPC approaches, which I can't explain. I'd appreciate if someone could explain what's wrong with my method, and point to a better one. Thanks.

#### Share this post

##### Share on other sites
Quote:
 Original post by BaiameThe "-velocity*0.6" is my lame attempt to take friction into account

That looks like a red flag right there. There is no friction in Space.

I also don't see why distance is relevant here.
You take your current velocity, then compare to a proposed velocity which passes through the target. The difference between them is the total acceleration needed.
This gets divided by delta to return the acceleration for this timestep.
-im not seeing how distance ever needs to show up here.

I would expect soemthing more like this:

forcenow=(velocityneeded-velocity)/delta

Also, I'm assuming your Spaceship only needs to pass thru the target(like a waypoint), not stop and land there, right?

P.S. I'm guessing that the wierd fluctiation you saw has to do with the friction stuff. Lose the friction and I'd bet it goes away; you don't want it in Space anyhow.

#### Share this post

##### Share on other sites
So, if I understand correctly, you want to figure out how hard to whack something (e.g. a golf ball) such that it will come to rest at a certain spot (e.g. at the hole)?

I presume that friction is constant (e.g. your already on the green and it is completely level)?

Does my golf ball analogy hold up?

#### Share this post

##### Share on other sites
Doh, cross-post.

Oh so this is in space, and you don't want to simply 'reach' the point, you want to go 'through' it? What speed to do you to be as you go through it?

The friction stuff threw me as well.

#### Share this post

##### Share on other sites
Sorry, my first post was extremely misleading. I'm aware that there's no friction in space. I was referring to Euclidean space. I'm trying to move NPCs across the ground (testing on flat parts of a terrain). In fact, the point is not totally arbitrary; it is always a fixed distance above the ground. If you see something horribly wrong with the friction part of it, please point it out.

About velocities. Consider that the physics engine returns velocities in m/s. The distance in meters is the necessary velocity in m/s (as the time delta in this case is 1 second). Dividing by the physics timestep (also in seconds) should give you the acceleration, no?

Thanks for your help so far, everyone.

EDIT- And for further clarification the "points" I'm referring to are waypoints for my NPCs to stop at.

#### Share this post

##### Share on other sites
As is, your equation:
necessaryForce = (distance - (velocity-velocity*0.6)) *npcMass /delta   (1)
reduces to :
necessaryForce = (npcMass *(distance - 0.4*velocity)) /delta    (2)
which is just a line with nF increasing with distance but constrained by velocity (that is as velocity increases max(nF) -> -∞)). What are your variables? Is velocity a constant, is equation (1) the same as what is in your code? Your code doesnt seem very physical.

#### Share this post

##### Share on other sites
Well, if this is actually about characters Walking on the Ground, then I'd say the entire 'find force necessary' approach really doesn't make sense here.

Find a force like that makes sense in space, since thats how a rocket would work, by using force to make a course/path adjustment.

Walking on the ground though; well people dont have rockets, they have feet.
Friction and your leg mechnics really dominates the physics here. Its not so much about force or acceleration since you don't really do much of that.
For the most part when walking you can change direction pretty arbitrarily with minimal change in overall speed...

I'd just have my character get pointed in the right direction, and keep walking within the limits of how fast he can walk/run. stop walking and stand still when he gets there.

Even if you want your character to Run instead of Walk, there's not so much an acceleration as there is a transition from one mode to the other... nothing to be that significant I'd say.

#### Share this post

##### Share on other sites
Quote:
 Original post by BaiameAbout velocities. Consider that the physics engine returns velocities in m/s. The distance in meters is the necessary velocity in m/s (as the time delta in this case is 1 second). Dividing by the physics timestep (also in seconds) should give you the acceleration, no?Thanks for your help so far, everyone.EDIT- And for further clarification the "points" I'm referring to are waypoints for my NPCs to stop at.

You have v = delta_x / delta_t with delta_t = 1. so v is correct. But a <> v / dt.

#### Share this post

##### Share on other sites
Daerax, I don't entirely understand your posts. Are the variable descriptions in my first post insufficient? Maybe I should point out that 0.6 is just the friction coefficient of the surface below the NPC, and I just put the number straight in as I don't know how to return a friction coefficient using the physics library. What do you mean by "a <> v / dt."?

haphazardlynamed, since I need a physics engine for other purposes anyway, I suspect that calculating the necessary force is the easiest method in the long-term. And in my experience, acceleration and deceleration when running have a significant effect. Infinite acceleration and deceleration in a game would be very noticeable and unconvincing.

#### Share this post

##### Share on other sites
Hey.

Quote:
 The distance in meters is the necessary velocity in m/s (as the time delta in this case is 1 second). Dividing by the physics timestep (also in seconds) should give you the acceleration

Is the same thing as a = v /dt. But a = dv / dt. Aceleration is a, dv and dt are change in velocity and time.

----
What I mean about your equation is I am not sure why you are dividing mass by time? Like I said it reduces to: m(d - .4v)/dt.

Also how do you calculate your velocity when solving for force?

[Edited by - Daerax on March 19, 2007 7:09:29 PM]

#### Share this post

##### Share on other sites
What I'm trying to say is, that in the case of walking animations:
The change in acceleration when turning or start/stopping only occurs very briefly; it doesn't really take part in most of the path calculation when your character is simply moving in a straight line at his standard walking speed.

So rather than calculate a force needed Every Frame; simply have your character plan a linear path with fixed walking speed; and only use this ease-in ease-out velocity change stuff when he's getting with say 5feet of his destination. and even then it could consist of a canned character animation/path sequence since he walks consistently...
In this manner... since the majority of the path was just at a fixed walking speed, he's probably going to arrive on time...

#### Share this post

##### Share on other sites
I think that there are a few things you might not understand. If you already know all this then bear with me. Force is a continuous thing. You apply force over a period of time. In addition, in a case without friction, applying a force for a period of time results in a certain velocity. Whether the velocity is 10 m/s, 1000 m/s, or .00000001 m/s, you will always reach the destination at some point. So the question, "how much force does it take to reach a point?" doesn't make a lot of sense.

Now, in your case you have some sort of friction. The solution is simple -- apply the necessary force to travel at a certain velocity until you reach the destination.

Here is how you determine the necessary force. The trick here is that when something travels at a constant velocity, its acceleration is 0.
    a = overall acceleration    F = force on the player    v = desired velocity    C = friction constant        a = 0 = F/m - Cv/m
Solving for F, you get
    F = Cv
A force equal to Cv will accelerate the player until it reaches the velocity v. You just need to tweak C and v to make it look right.

On the other hand, I agree with the others that applying a velocity (or impulse) in this case is easier than applying a force.

#### Share this post

##### Share on other sites
The line thing is also a good idea. Suppose your character is at (x0, y0) and he wants to get to (x1,y1) in increments of step then you can move the character to point B in terms of a line.

total_steps = distance(A,B)/step

x = x0 + steps_taken/total_steps * (x1 - x0)
y = y0 + steps_taken/total_steps(y1-y0)
steps_taken += step

Most of the time people walk at a constant speed so it is not worth it to calculate any minor speed up or slow down at the beginning and end i feel.

#### Share this post

##### Share on other sites
Quote:
 Is the same thing as a = v /dt. But a = dv / dt. Aceleration is a, dv and dt are change in velocity and time.

I was aware that acceleration=velocity delta/time delta, but I was under the impression that subtracting the current velocity from the necessary velocity gives you the necessary velocity delta. Is that incorrect?
Quote:
 What I mean about your equation is I am not sure why you are dividing mass by time?

I'm not really, that's just the (stupid) order I put the equation in. To make more sense, it should be written with the time delta and the mass switched around, but as far as I know, it isn't of any consequence for actual calculation. The first part is acceleration, which is then fed into F=ma.
Quote:
 So rather than calculate a force needed Every Frame; simply have your character plan a linear path with fixed walking speed; and only use this ease-in ease-out velocity change stuff when he's getting with say 5feet of his destination. and even then it could consist of a canned character animation/path sequence since he walks consistently..

Oh, I must've misunderstood you. I'll think about it, but I still can't imagine it'd be much easier than what I'm trying to do, given that the physics simulation must be consistent.

JohnBolton,
I was aware that in a frictionless environment, a constant force leads to constant acceleration, at least in classical mechanics. I was also aware that force is applied constantly, though I don't know how it's handled internally in the physics engine (I suppose it simulates application of the force for the duration of each update). I see what you mean about the flaw of my method, though. The solution you present looks nice, I'll definitely try it out a bit later on.

Thanks for your help everyone.

EDIT:
JohnBolton,
After studying and trying your solution, I think that my intent still wasn't entirely clear. Movement in a direction isn't a problem. What I'm trying to do is make code that works for NPC waypoints, so it's more than a direction to move in, it's a position to come to a stop at.

So what I'm trying to do is apply a force such that the NPC comes to a stop, at the waypoint, as quickly as it possibly can. I thought that calculating the force necessary to reach the waypoint at the next physics engine update would do it, but either the concept or my method is flawed.

EDIT2:
JohnBolton,
I studied your entire post again, it seems you understand what I'm trying to do, unfortunately I can't get your method to work (and neither do I see how it could work). I try setting v to the distance (and thus the velocity in m/s), and also to v/dt (thus the velocity in m/dt). The NPC either seriously under or overshoots the waypoint, respectively. The thing I don't get about your method is that you set a to 0. Is that because the target acceleration is 0? I don't see how that applies to the "waypoint" problem. Can you please clarify?

And I should point out that when trying all these methods, I have both positive and negative force limits. It's the "ideal" force that I'm trying to calculate.

[Edited by - Baiame on March 21, 2007 1:20:06 AM]

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

• ### Forum Statistics

• Total Topics
628379
• Total Posts
2982354

• 10
• 9
• 15
• 24
• 11