Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Waypoints, moving an object to a specific location.

Author
Message
theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 11th Feb 2008 13:44
Hopefully someone can help me with a simple way to code an object moving from one spot to another.

A read through all the moveobjects commands at first does not seem to suggest clearly how to, for example, move object A to position ( or object ) B.

Yes, the moveobject commands allow you to have the object in question move along one of the X,Y,Z axis by a set amount. ( or in a given direction ). Or to be instantaneously shifted to another position without 'moving' there.

But there is as far as I can see, no straightforward command to move from point X,Y,Z, to point X1,Y1,Z1 etc.
monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 11th Feb 2008 14:15
I'm not entirey sure if you're asking how to position an object i.e. dbPositionObject or if you are asking how to move an object along a path to simulate an A.I character walking.

If the later is true, thare you using a custom written A.I system or are you using DarkA.I

Projects Started: 20
Projects Completed: 0
theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 11th Feb 2008 14:30
Yes, the latter, I want to move an object along a path. ( From A to B, where B is sometimes changing )

Since I do not really want to purchase DarkA.I at the moment, I am trying to fathom out how to do this using a fairly simple custom routine.
monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 11th Feb 2008 14:39 Edited at: 11th Feb 2008 14:40
You're posts are on approval, but to answer you're question which will show up later.

You need to implement an A* pathfinding algorythum to find paths around objects etc.. Then you would need to use the dbMoveObject function and set the move speed to a realistic speed for running/walking. Once the object reaches the destination point stop it.

Reaching exactly the specified point will be hit and miss, for instance your character may pass the point by a little amount then try to correct itself and walk backwards a little then pass it again and try to correct itself etc which will result in you're character jerking around. So the best thing to do is check if your character is within an acceptable distance from the destination point and stop it.

But I would highly recommend you buying the DarkA.I plug-in, it contains some nice features which make developing A.I easier.

Projects Started: 20
Projects Completed: 0
theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 11th Feb 2008 15:57
Thanks .

I have searched the forum for articles referencing 'pathfinding' and have come up with plenty of interesting reading on the topic. Quite complex some of it, with few specific examples of coding sadly. ( all theory, no practice ! )

That said, 'intelligent' pathfinding and obstacle avoidance are for later when I understand the above better. Right now, for the purposes of what I am doing, I just want a clean, simple algorithm / method that will move an object straight from point A to point B along a path.
Sephnroth
21
Years of Service
User Offline
Joined: 10th Oct 2002
Location: United Kingdom
Posted: 11th Feb 2008 22:28
You will need to decide things like a) how fast the object will move each step, b) how much time to wait before taking the next step, etc.

I will try and give you code examples to walk you through this but as i'm writing in a browser window and not in my c++ IDE and im not actually building and testing with a real project as I go there may be bits overlooked or little bugs for you to explore and fix in your real life project

There are two ways you can do things. One is to use the DGDK commands that make your life easier but your code only really relevent to dgdk based games (ie, cant re-use in a directx game you may make in a years time) or you can do things by hand.

If you were doing this by hand you would store off the objects current location and angle. You would then get the angle between the objects current location and its destination and rotate the object by that angle.
you would then check to see if you were close enough to the destination to be considered "there" (dont check for exacts because of inaccuracies after the decimal point) and if not then plot a coordinate in a line using the angle you were now facing that brought you closer to the destination. I can give you code for this sort of thing if you want it - but im going to assume the easy way is fine and show you that for now instead.

ok, using dgdk commands:

First thing you need to do is point your object towards its destination (this is all presuming streight lines is fine). dgdk makes this easy. Assuming your objects ID is stored in an integer called nObjID and that you're storing your target destination in three floats called fDX, fDY and fDZ then:

dbPointObject(nObjID, fDX, fDY, fDZ);

now you need to check to see if you're close to your destination. Use my distance checking functions to do this, the code for them is:



and use them to create a conditional like this:



Now I would put the dbPointObject() call in the "code to move to destination here" section of the if myself, no need to point otherwise. Then you need to move towards your destination.

Like I said earlier, you need to decide how frequently to move and how much to move when you do. Create an integer to store the time that the object last moved, like:

int nLastUpdate = 0;

also two variables to store how frequently to move and how much to move. Make them variables so you can easilly tweak these settings without changing the code, just to see how it reacts.

int nUpdateFrequency = 10; //move every 10ms - the higher this is, the jerkier the movement! but stops it going crazy fast on high speed cpus.
float fUpdateStep = 1.5f; //move 1.5 units each update

now the code to make the object move using the dgdk easy commands:



and that, should simply do it. so a finished program should look SOMEHING like:



okay, so i got carried away and wrote the whole thing. Still, it might be helpful!! haha, hope that makes things clearer (I usually make things more confusing xD)

monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 11th Feb 2008 23:02 Edited at: 11th Feb 2008 23:05
But of course if the entity is a character and not a spaceship or some other flying object, you do not want to point it up or down along the axis i.e. you would use the characters' current height as the y co-ordinate for the point object function call. Because when you walk up some stairs for instance you stand upright and not perpendicular to the staircase.

Also, I would recommend using dbCurveValue to smooth the rotation around the y axis using a timer based system similar to the movement, otherwise your character will instaneously flip around which is not very life like.

I would also recommend not using square root, and instead square the distance before hand, so if you want your entity to stop within 10.0 units of the destination point then this squared equals 100.0

then use:



Edit: Obviously the above code is just an example and not a complete working solution like the very kind Sephnroth posted above.

Projects Started: 20
Projects Completed: 0
Sephnroth
21
Years of Service
User Offline
Joined: 10th Oct 2002
Location: United Kingdom
Posted: 11th Feb 2008 23:13
indeed, for a character you wouldnt point "upwards" whilst moving, but he just specified streight lines so decided to keep it very simple

I dont mind using that distance function alot because the fastsqrt function is very fast. Possiably faster than the to the power of operator, but i havnt tested this!

good shouts there all around though mono

theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 12th Feb 2008 01:33
Thanks for that, I will read through the code example thoroughly later.

My initial experiment at its simplest just compared the two objects X and Z values and used an 'if else' conditional to then increment or decrement the objects new position based on whether the moving objects X,Z values were greater or less than the targets.

Thus resulting in the object homing in on its target. Of course, there was no proviso in this bit of code for what to do when the target was reached. I needed another 'if else' to bypass the code one the X,Y,Z values for both become equal, or within a set range.

eg

Niels Henriksen
20
Years of Service
User Offline
Joined: 27th Sep 2004
Location: Behind you breathing heavely
Posted: 12th Feb 2008 16:39
What about this?

X = X + ((dbObjectPositionX(2) > dbObjectPositionX(1))*10)+5
Z = Z + ((dbObjectPositionZ(2) > dbObjectPositionZ(1))*10)+5

As I know (maybe not in C++) then if an expression is true the value is -1. And (-1*10)+5 = -5

If its false its: (0*10)+5 = +5

Niels Henriksen
Working on a (MMO)RPG right now in LightEngine (thanks kBessa)
theagg
16
Years of Service
User Offline
Joined: 11th Feb 2008
Location:
Posted: 12th Feb 2008 19:11
Niels, nicely tidied up.

Slight correction though, the 'true' value returns a 1, not -1. So the +5 ( or whatever incremental value for the move is required ) should in fact be a -5.

Login to post a reply

Server time is: 2024-09-29 11:31:31
Your offset time is: 2024-09-29 11:31:31