parabolic movement for a throwing object

Started by fitbo, Tue 22/01/2008 18:02:01

Previous topic - Next topic

fitbo

Hi,

I really suck at geometrics so I have to ask you guys. Basically I want an object to fly from a certain point(x1,y1) to another(x2,y2) in a parabolic curve. The starting point would be the position of the main character throwing an object into a window.

I'd really appreciate your help.

???

Snarky

I did something like that a few years ago. Code snippets here (though it'll probably be a little bit different in the "new" OO scripting language).

OneDollar

You want to break it down into two different movements; a horizontal one and a vertical one. Assuming you don't have anything like air resistance your horizontal velocity is going to be constant, so you can just have an amount being constantly added (or subtracted) to the object's x coordinate.

The vertical velocity will change due to gravity, so you will need to constantly add an integer to the object's y coordinate, but this integer will be made bigger and bigger, and will eventually become positive at which point the object will start to fall. The code below does something like this, but you might have to alter it slightly to your needs.

Code: ags

//at the top of the script
int xVel=1;  //the amount the object will move in the x direction each loop
int yVel=-3;  //the amount the object will move in the y direction each loop. Initially negative so that it will start moving upwards
int gravity=1;  //the amount the yVel will change every loop
int xFinal=25;  //the x coordinate the object will finish at
int refreshRate=5  //the number of loops to wait until moving the object

//in the function where the object is thrown
while(object.x<=xFinal){  //while the object has not yet reached its target
  object.x+=xVel;  //move in the x direction
  object.y+=yVel;  //move in the y direction
  yVel+=gravity;  //change speed down
  wait(refreshRate);
}


With certain values your object could start falling too fast, so you could add a terminal velocity in there. You'd need another integer (such as termVel=10), and replace the yVel+=gravity with

Code: ags

if(yVel<termVel){
  yVel+=gravity;
}

Khris

A few pointers I'd like to add:

-To get a smooth curve and exact calculations, the internal variables used to calculate the position should be floats. Only at the end of each loop, the coordinates are turned into integers and used to display the object.

-A parabola is the graph of a function with three unknowns: y = a*x²+b*x+c
So x1;y1 and x2;y2 aren't enough to define it; there's an infinite amount of parabolas passing through both points.
You'll need some additional info, like the initial velocity or the height of the throw.

DazJ

If this is to happen for just a one-off sequence, try using a largish sprite and animate the object being thrown from there.

Khris

Here's a piece of code from one of my games:

Code: ags
        Overlay*o=Overlay.CreateGraphical(320, 0, 381, false);
        int x=301;
        int y;
        while(x>=159) {
          y=35+((x-240)*(x-240)/64);
          o=Overlay.CreateGraphical(x, y, 381, false);
          Wait(1);
          x-=2;
        }

As you can see, the apex is at (240; 35) and the factor is 1/64 resulting in a nice arc.

Rui 'Trovatore' Pires

Hmmmm.

Is there any chance any of you great scripters who are already familiar with this issue, I say, is there any chance you could make a module of it? Throwing things tends to happen a lot, in adventure games, it seems, and having a function that could turn a strange object movement into a fluid parabolic movement would be aces.
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

SMF spam blocked by CleanTalk