How to make an object follow a path with CustomPathFollow

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: How to make an object follow a path with CustomPathFollo

Postby JoeJ » Sun Jul 20, 2014 6:13 am

maybe it's not wrong at all. At the end of 2nd video it looks the bodies keep a fixed distance to the line and rotate around it, which is right.

Verify by removing any offset:

fst::mat4 m = pl::BodyMatrix(b);
joint = new CustomPathFollow(dgMatrix(&m[0].x), b);

then the bodies should stick exactly at the line without the spirals. What happens if you do so?
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: How to make an object follow a path with CustomPathFollo

Postby JoeJ » Sun Jul 20, 2014 6:25 am

The other thing you should try is using larger bodies. The line is 10 units high, so use a box 2 units wide in x axis, and one unit in yz axis. Then you can see if the x axis stays parallel to the line.

And you should add a pick interface so you can drag the body with you mouse. You can check if you can pull the body away from the line.
Pick interface is not easy to do, but it's the easiest way to check if physics work like it should, so it pays off even if you don't need it for the game itself.
Sandbox has code you can look for.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: How to make an object follow a path with CustomPathFollo

Postby arkdemon » Sun Jul 20, 2014 6:27 am

@1st post after mine Hi :). When I do that, I get this: https://dl.dropboxusercontent.com/u/949 ... 2.4.44.wmv
(Look at my previous post :P)

Edit: I have to go, I will be back in I think 1 hour or 2
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: How to make an object follow a path with CustomPathFollo

Postby JoeJ » Sun Jul 20, 2014 7:58 am

ok, i think you spawn multiple bodies at the same place. After a while floating point inprecision seperates the enough so newton can try to seperate them, the joint works against this, things go bad and simulation blows up. I tried to reproduce this but explosions do not happen to me, it's just that newton fails to seperate them and they keep sticking together. The line joint works as expected and no body flys away. I assume after a longer while the bodies would return to the line also for you.

Try larger bodies and mass of 1 to be sure floating point precision is ok, and avoid overlapping multiple bodies on initialisation. In practice you should also have the bodies aligned with the line initially.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: How to make an object follow a path with CustomPathFollo

Postby arkdemon » Sun Jul 20, 2014 1:15 pm

Hello :) I have rescaled everything and ^put the mass to 1. When I take the default values, it works even though it is quite laggy (eg the glueing or stiffness). Here is a little example :P ; it shows the "normal" case, the "glueing" case and the "stiffness". At the end, I made some objects shoot each other (they should not move except fo 0.0 10.0 0.0 and when shot with the collision with the bullet) : https://dl.dropboxusercontent.com/u/949 ... 9.9.24.wmv

However, for the use I am trying to make of it, there are some problems and I do not understand some points.
I think the problem I have, comes from lineSlope or tangent. As newton already normalizes it, do we have to do it once more? or is lineSLope = normalize(targetpos - bodypos)?
Thank you for your answers
Last edited by arkdemon on Sun Jul 20, 2014 1:27 pm, edited 1 time in total.
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: How to make an object follow a path with CustomPathFollo

Postby Julio Jerez » Sun Jul 20, 2014 1:18 pm

It should not be that difficult to get is going.

In the video can you draw a line to where the path is supposed to be? I do no understand what the video is doing.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: How to make an object follow a path with CustomPathFollo

Postby arkdemon » Sun Jul 20, 2014 1:41 pm

Hi.

THe first body that I create is to show when it is normal, the second body is to show when it starts rotating fast and the third to show the "glueing". Finally, the fourth shows when the bodies move in an irregular way, eg they move fast one moment then they move very slowly at another moment.

For rendering the line, I will implement it right now :P and post the result.
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: How to make an object follow a path with CustomPathFollo

Postby JoeJ » Sun Jul 20, 2014 2:08 pm

It still looks buggy as soon as the bodies collide, that's really strange. One good idea would be to recreate the scene in nowton sandbox, if you can compile it.
If it works there you would know more...

arkdemon wrote:lineSLope = normalize(targetpos - bodypos)


Reading bodypos makes me think you change the line parameters at runtime?
That would explain the bugs, because the line is meant to be static.

Also the joint will not make the body move to a target, you would need to add an external force for that, or make it a powered joint.

Can you describe what is your goal?
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: How to make an object follow a path with CustomPathFollo

Postby arkdemon » Sun Jul 20, 2014 2:37 pm

For rendering the line, how can I initialize the matrix if I have the position and the direction or the point 1 and the 2nd point?

@JoeJ, no I do not modify the line at runtime, bodypos it is just the initial body position, when it is created.
What I meant by a target is a point or a vector, not another body.

What I want to do is to make an object that I picked go to another point (that I click).
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: How to make an object follow a path with CustomPathFollo

Postby JoeJ » Sun Jul 20, 2014 3:15 pm

Then your slope formula is right, but using the line joint is nood really a good idea.
The joint keeps the object (a soldier) on the line, even if another object (a heavy tank) should push it aside.
And you still need to apply a force to make it move to the target, so the joint will not make it easier to code.

Why don't you simply set the velocity of your object without any joint?
When you are happy with that, you should apply a force instead, which is easy:

inline sVec3 ConvertLinVelToForce ( sVec3 &targetVel, sVec3 &currentVel, float timestep, float mass)
{
sVec3 force ((targetVel - currentVel) * (mass / timestep));
return force;
}

TargetVel is the velocity you want, currentVel is the velocity the body has (from NewtonBodtGetVeloc.).
You should multiply the resulting force with some smoothing factor like 0.3, and clamp the force against a smallest possible value to keep the collide with tank example stable.
You then apply in ForceTorqueCallback with NewtonBodyAddVelocity().

But first simply set the body velocity (NewtonSetVelocity(), also in ForceTorqueCallback), and when that works, convert to force.

Getting the target velocity is as simple as (targetPos - bodyPos).Unit() * speed.

Controlling orientation follows the same math, but is harder due to inertia...
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: How to make an object follow a path with CustomPathFollo

Postby arkdemon » Sun Jul 20, 2014 5:07 pm

Ok thank you :)
I will then use the force method
At the beginning I tried this force method (not as complex as yours though) but it made huge loops to finally come to the point where I wanted it to go but I managed to remove the loops now with NewtonBodyGetVelocity (with your code) :P

Thank you for your help as well as Julio's help ;)
I think the problem is resolved now ^^


and thanks for passing some time trying to understand and resolve my problem which was strange...
and sorry if the problem (or what I was trying to do) was not clear
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: How to make an object follow a path with CustomPathFollo

Postby Julio Jerez » Sun Jul 20, 2014 7:05 pm

Ha cool can I see a version of a working version?
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: How to make an object follow a path with CustomPathFollo

Postby arkdemon » Mon Jul 21, 2014 6:18 am

Well it is quite an early (very early) version :mrgreen: so don't expect much. I already did something before with some spaceships and planets but the code was getting too bloated (was not efficient and there were some comments everywhere of code that I used before that could still be useful) so I restarted everything 2 weeks ago with git (bitbucket & sourceTree) so that I have a backup and a history.

Now, I am focusing more on the ground thing and gameplay, so I am doing all the graphics and effects later :P (as well as the terrain that I nearly finished a while ago. I used geomipmapping and tried to implement morphing without success)

I'll send you a link by PM when I willl have finished polishing my little code and perhaps added some buildings and units x)

For my game, there will be around 4 factions that are quite different (the names might change):
- the Human Union - or the humans which can specialize their units with either clones, mutants, or robots
- the Draedor - a group of predator species that do not have any buildings or techs, but lairs where they evolve naturally (and grow huge and stronger over time)
- the Eclipse Order (I am not sure yet but I think I will divide them into 2 "tribes", the Nox and the Feos) where the highly educated and chosen ones become Seekers / Guardians which have special abilities
- the Joguls (kind of ants) - they have a queen (if the queen dies, they lose :P ) which can spawn workers, soldiers(different types), or hatchers (you see what I mean)
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Previous

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests