making a jump from A to B

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

making a jump from A to B

Post by Asimov »

Hi all,

Maths not being my strong point I wondering how to move an object from A to B on a parabolic trajectory.
eg
Image

I know if I was in 2D to make x,y get to c,b I would just have to increase x, or y until it reaches coordinate c,b over time.

First of all I have been moving things around in irrlicht by trial and error. I know you have x,y and z coordinates, and I know the middle coordinate is height.
In 3ds max, you have x and y, and z is height.

In irrlicht I have worked out that this would be (0,20,0) hieght, but I am not sure if that middle coordinate is z in irrlicht.

Anyway to get a piece moving on a parabola I would need to increase hieght, over time, and when it gets to the centre, decrease hieght.
While doing this x and y would also be moving towards their target.

X and Y are easy to compute ish, but z being hieght would be very hard to work out. Well unless you are very good with forumulas.

Any ideas?
I presume this is complex as I can't use loops. I suspect a formula would require checking for if the piece has reached the centre and then switch it about so that it descends towards the correct hieght.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: making a jump from A to B

Post by mongoose7 »

(xa +(xb - xa)*t, h - 4.*h*(t - .5)*(t - .5), za + (zb - za)*t) takes the object from (xa, 0, za) to (xb, 0, zb) through (.5*(xa + xb), h, .5*(za + zb)) as t goes from 0 to 1. You have to set t in the rendering loop by using device->getTimer()->getTime().
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: making a jump from A to B

Post by Asimov »

Hi Mongoose,

Thanks.
How to go about working out a formula like that?
I would like to understand it more. Obviously I want to use it, but where do you start?

Stupidly I thought PI and cosine would have to be used LOL.

Unfortunately my maths was never that good, but I like to try.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: making a jump from A to B

Post by mongoose7 »

I think you need to have done some maths exercises. For example, a + (b - a)t goes from a (at t = 0) in the direction b-a. So (b-a)t will move in the direction of b, further as t increases. So a + (b-a)t starts from a and moves towards b as t increases from 0. When t=1, a + (b-a)t = a + (b-a) = b. It is just vectors or linear algebra. This way of thinking, though, needs experience.

The quadratic is a bit harder. It is symmetrical about a and b and is inverted, so we should start with y = -m(t - (0 + 1)/2)^2 + n. When t = 0 and t = 1 we want y = 0, so 0 = -m(.5)^2 + n. When t = .5, y = h, so h = n and m = 4h.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: making a jump from A to B

Post by Asimov »

Hi mongoose,

Well I did algebra at school, but I am 46 now so it was many years ago LOL.
We only has 6 computers in the whole school, and they were commodore pets, texas instruments and bbc micros, so my maths are a bit vague LOL.

I can work out a lot of formulas, but when it comes to tranjectory and stuff like that I haven't got a clue heh heh.
I would like to learn how to work out formulas like that. Have you got any good sources online I can look at.
I can understand the simple stuff, but when it comes to arcs, and circular stuff I can't visualise it.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: making a jump from A to B

Post by mongoose7 »

No, I remember it from Uni or make it up myself. Like with the inverted quadratic, once you know it is quadratic and you know the endpoints, you can work it out. I'd just Google "linear algebra" and "vector math" and see what comes up.
Kojack
Posts: 67
Joined: Sun Jan 20, 2008 2:39 am

Re: making a jump from A to B

Post by Kojack »

Another way to go is a quadratic bezier curve.
You have three control points: the start (A), the top (B) and the end (C).
The bezier works by interpolating between A and B, interpolating between B and C, then interpolating between the previous two results.
Image

The point B isn't the actual top of the jump, but it controls it. The cool thing about this style of curve is that by moving B away from the centre, you can bias the curve to either side. Move it near A and the jump will start fairly vertical, then drift across. Move it closer to C and it becomes a long jump that rapidly decelerates at the end to drop down onto a target (like using air brakes).
You can also place A and C at different heights, it will still work fine (if B is suitable).

Some code... (I put it in the Example 4 code, taking over the sphere)

Code: Select all

// Setting up some stuff
float t=0; // t==0 is the beginning, t==1 is the end
irr::core::vector3df a(-30,0,30); // start
irr::core::vector3df b(20,30,30); // middle
irr::core::vector3df c(30,0,30); // end

Code: Select all

// inside of your main loop
t+=frameDeltaTime; //increment t
if(t>=1.0f) //if we reach the end of the jump, just go back to the beginning
    t=0.0f;
core::vector3df nodePosition = irr::core::lerp(irr::core::lerp(a,b,t), irr::core::lerp(b,c,t), t); //the actual bezier calculation, a lerp of two lerps
node->setPosition(nodePosition);
There's a slight difference in the results between mine and mongoose7's. His looks like it has constant horizontal velocity, which is how real life works (at least in a vacuum). Mine has lower horizontal velocity at either end than in the middle. So mine is like an overhead view of a car driving along the curve, while his is like a side view of a person jumping. Just something to be aware of.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: making a jump from A to B

Post by Asimov »

Hi mongoose and Kojack,

I think I will be going for mongoose's method, although I can also see where the kojack method is useful however.
I am going to use this code to move pieces around the board, so I want the constant velocity that is mongoose's formula.
However kojack,I can see your formula for something like throwing a stone, which would start of at a one velocity and then slow down while travelling.

I really wish I could work this stuff out. When I do animation in 3ds max it is easy, I can just play with the curves until I get it to look right, but when you have to try and work out a formula to do the same thing I haven't a clue.

Anway thanks. I won't be using the formula for a couple of weeks, but when I do I will let you know how it worked.
I still got a lot of setting up to do, before I start on the serious programming heh heh.

PS. Kojack I love your animation. Did you make that, or find it on the net?
Kojack
Posts: 67
Joined: Sun Jan 20, 2008 2:39 am

Re: making a jump from A to B

Post by Kojack »

It's from wikipedia. I use it in my lectures on curve processing and rendering.
The higher order ones are even more fun. :)
http://en.wikipedia.org/wiki/B%C3%A9zier_curve
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: making a jump from A to B

Post by Asimov »

Hi KoJack,

Hey are you a maths teacher? Because when I was at school I had a really really bad maths teacher who knew her subject really well, but couldn't control the class, which is why I ended up doing badly at maths at school.

However when learning to program on an old zx81 I learned binary arithmetic from a book and taught myself. I then went on and did an electronics course and got a Distinction in my City and guilds exam level N1. There was a lot of maths involved in that. Thank goodness for calculators though :D
Post Reply