This topic is 2934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello

i am currently working for a 2D game on iphone using opengl.
currently there are objects(cars) that travel in a straight line with varying velocity..

i am looking for some suggestions on how to gradually deaccelerate...

1. how to detect some object is at the front

2. if my object which is ahead is traveling at a slow speed than the object which is at the back of it, should gradually slow down and move along with it...

3. if my object which is ahead comes to a halt..then the object behind should come to a halt gradually..

[Edited by - anupgupta on September 29, 2010 11:37:19 AM]

##### Share on other sites
Potential fields + damping.

I.e., make your cars exert repulsive forces as appropriate.

##### Share on other sites
hello everyone

could anyone help me with and give a detail explanation as to how could
i detect, if two objects are traveling in a line with varying speeds.

how to get that there is an object in the front..

##### Share on other sites
Ok, I'll elaborate on my previous two-sentence-fragment answer. Here's one approach from which you should be able to take ideas.

1. Dream up some function -- I'll call it 'w' -- that takes the positions and orientations of two cars and returns a (scalar) influence of one vehicle on the other. Picture it as a spotlight shining out the back of each car. I'll give an example of such a function in a bit.

2. Suppose there are N cars; the ith car has position p_i. Now compute, for each car i, the sum

$f_i = \sum_{i \neq j} -\mathrm{grad~}_{p_i}w(p_j, h_j, p_i, h_i)$

3. Exert this force on the ith car.

----

Here's an example of an influence function 'w'. Let me emphasize that this is just a function I'm making up right now; be creative yourself. Try plotting it as a function of p_2, with p_1=0 and h_1=(1,0); you'll see how it falls off. Change h_1 and see how it rotates to match the heading.

$w(p_1, h_1, p_2, h_2) &= f\left( \frac{\left}{||p_2 - p_1||}\right ) g\left(p_1, p_2 \right) \right )$
where 'f' is the continuously-differentiable "quadratic clamp" function,

$f(x)= \left\{ \begin{array}{rcl} x^2 & \mathrm{if} & x > 0 \\ 0 & \mathrm{if} & x \leq 0 \end{array} \right.$
and 'g' is the "poly6 kernel,"

$g(p_1, p_2) = \left\{ \begin{array}{cl} \left(1 - \frac{||p_1 - p_2||^2}{R^2} \right )^3 & \mathrm{if~~} ||p_1 - p_2||^2 < R^2 \\ 0 & \mathrm{otherwise} \end{array} \right.$

where p_1 and p_2 are the positions (2-vectors) of the two vehicles, h_1 and h_2 are their unit heading vectors, and R is some user-tunable radius.

[Edited by - Emergent on October 1, 2010 9:44:49 AM]

##### Share on other sites
I think you may be blowing OP away with the vector calculus stuff. But not me, I have a degree in physics!
Q1: I think I understand it all except for the quantity
< p1-p2, -h1 >. A scalar value is called for where this appears. Is it the dot product of p1-p2 with -h1 ?

Q2: Why f = grad w not f = -grad w ?

Computing grad w may be tricky since w is a product of piecewise continuous functions. I'll be experimenting with your approach ( using a scalar potential function to generate forces from ).

Q3: Please. how do you display equations in posts such as you have? Do you use a special program for this?
EDIT: Found answer to Q3 in forum sticky re: LaTex based equations

[Edited by - fun2code on October 1, 2010 12:51:48 AM]

##### Share on other sites
Quote:
 Original post by fun2codeI think you may be blowing OP away with the vector calculus stuff.

Hopefully not; I think I had the idea that if I was specific enough and spelled everything out then it'd be ok.

Quote:
 Original post by fun2codeQ1: I think I understand it all except for the quantity< p1-p2, -h1 >. A scalar value is called for where this appears. Is it the dot product of p1-p2 with -h1 ?

Yep. Inner product.

Quote:
 Q2: Why f = grad w not f = -grad w ?

Oversight! Fixed, thanks.

Actually, there were some inconsistencies beyond this in what I'd written (I started out with w being a function just of the displacement between the two bodies, and by the end had turned it into a function of the whole positions+orientations of both bodies, but had forgotten to change the stuff at the beginning to reflect this), so those are fixed as well.

Quote:
 Computing grad w may be tricky since w is a product of piecewise continuous functions. I'll be experimenting with your approach ( using a scalar potential function to generate forces from ).

Yeah, a little, so you can definitely use some other function if you prefer. One advantage to the poly6 kernel in particular, though, is that it has finite support (i.e., outside of a radius R, it's exactly zero everywhere) which means you don't have to actually loop over all pairs of objects but can instead do sweep-and-prune stuff like you'd use for collision detection to efficiently locate only those pairs of objects that are close to one another. It's also fast to compute and continuously-differentiable, which are pluses.

Quote:
 Q3: Please. how do you display equations in posts such as you have? Do you use a special program for this?EDIT: Found answer to Q3 in forum sticky re: LaTex based equations

Ok, nice. I was using this online LaTeX editor, in case that's not the same one mentioned in the sticky.

##### Share on other sites
I hadn't noticed the need to specify which set of coordinates that grad would apply to. Good catch. Hopefully it would have become obvious once attempting to find grad w. The question would arise: "Differentiate with respect to which variables?".

The link you provided to the codecogs LaTeX editor is more direct than the one given in the sticky I mentioned. I have bookmarked the page your link leads to. Thank you.
I think I'll be spending some time at www.codecogs.com, I see a LOT of interesting stuff there!

##### Share on other sites
After I wrote this I realized that this solution only applies to the case that the objects are not seeking to detect non-parallel object interactions. An alternate reading could clarify the question to include the case if two objects approach the same point from different directions they should detect and slow. My example works only for colinear overtaking detection and handling. The field and damping based answers seem excessive for this case.

If for each object you keep track of its motion vector, and the coordinate pair at which it crosses an axis you can use those to determine if any two objects are in a line with each other. Where the vectors are the same (or negative) the lines are parallel and if the coordinate pair at which they will cross an axis is the same they are the colinear.

Once you know any pair or more of objects are colinear you can use the basic displacement, velocity, acceleration formulas to determine how quickly to decelerate to match velocity at a given distance. The gradualness of the deceleration will be dependent upon how you select the time any function can do the job but a linear equation is probably best.

A simple example given 4 objects,
Obj A, motion vector <1,5>, axis crossing at (0,3), and current location of (3,18), and a velocity of 6m/s
Obj B, motion vector <1,5>, axis crossing at (3,0), with location of (6,15), velocity 6m/s
Obj C, motion vector <1,5>, axis crossing at (0,3) and location of (-3,12), velocity 9m/s
Obj D, motion vector <1,4>, axis crossing (1,0) location of (3,8)/ velocity 6m/s

Objects A, B, and C are parallel, objects A and C cross the axis at the same point and thus are colinear. Except where current location of A and C are the same one of them must be 'in front' for some frame of reference. Object D in this case is not parallel to any other given object.

Now determine the deceleration necessary solving the displacement function for your desired time of deceleration.

Using objs A and C assuming desired 4 meters separation at matched speed:
4 = sqrt((x1 + x2)^2 + (y1 +y2)^2) + (v2 - v1)t + at^2
Rearranged to solve for the acceleration:
a = sqrt(sqrt((x1 + x2)^2 + (y1 +y2)^2) + (v2 - v1)t - 4) / t
It's an ugly equation but it's straightforward to process and will only be called in cases where the previous step has already detecting a colinear object set. Where you have several objects in a line you might want to optimize by predicting the final location relative to the front object instead of the next object if middle objects will achieve velocity matching before subsequent objects catch up.

Similar equations will work for halting by adjusting the final displacement so that the objects are not locked in the same deceleration function as used in normal overtaking or else they would both come smoothly to a halt together as if they were a single object.

This will work best in a case of vehicles traveling in a grid pattern with traffic control devices that prevent them from intersecting and only allow overtaking and halting which is how I originally read the expressed problem.

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632955
• Total Posts
3009450
• ### Who's Online (See full list)

There are no registered users currently online

×