Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Mould Cheeze on Wed 16/05/2007 15:01:23

Title: How to make a character curve?
Post by: Mould Cheeze on Wed 16/05/2007 15:01:23
I want to make a charaxter to jump in a curve but I have no clue how to.
Let's say the player stands at x0 y0 and needs to jump to x100 y0. He should jump in a flawless curve and in the middel he meanwhile reaches the peak y-value of 50 but then comes down to y0 again.

I would be grateful for your help!

(I hope this was somewhat understandable!)
Title: Re: How to make a character curve?
Post by: glaft on Wed 16/05/2007 15:24:04
the lazy solution would be to draw the curve inside the sprite. as in, you draw all sprites of the animation in the same rectangular shape, where the bottom of the shape is always 0.

plus, it's probably easier to draw that way anyway
Title: Re: How to make a character curve?
Post by: monkey0506 on Wed 16/05/2007 15:31:36
void Leap(Character* Char, int destination_x, int destination_y, int peak_offset) {
  // NOTE: This function is BLOCKING, i.e., you cannot call it from rep_ex_always
  // Char is the character performing the leap
  // destination_x and destination_y are the X and Y coordinates of the destination (respectively)
  // peak_offset is the difference between Char's initial Y value and the peak of the leap
  if (Char == null) return;
  bool leap_left = true;
  int count = (Char.x - destination_x);
  if (count < 0) {
    count = (destination_x - Char.x);
    leap_left = false;
    }
  int y_offset = ((Char.y + peak_offset) / count));
  int y_orig = Char.y;
  bool reached_peak = (peak_offset == 0);
  while (count) {
    if (leap_left) Char.x--;
    else Char.x++;
    if (!reached_peak) {
      if (((peak_offset < 0) && (Char.y > (y_orig + peak_offset))) ||
      ((peak_offset > 0) && (Char.y < (y_orig + peak_offset)))) Char.y += y_offset;
      else reached_peak = true;
      }
    else if (((peak_offset < 0) && (Char.y > destination_y)) || ((peak_offset > 0) && (Char.y < destination_y))) Char.y += y_offset;
    Wait(1);
    count--;
    }
  }

// some function
Leap(player, player.x + 10, player.y, player.y + 10); // should make the player leap 10 pixels to the right with a parabolic peak at 10 pixels above  his y-coordinate


Completely untested. Probably won't even work. But it's the best I could do without actually putting some effort into it. :P

BTW, this really boils down to the "There is no real 'Make My Game' option in AGS" talk again. This isn't something you would ordinarily need to do in an adventure game, and really it's just a simple matter of scripting if you know how to do the math properly (again, mine is probably horrifically off). I've provided you with enough that if you can work out the math, you should be able to get something out of this...but it's always useful to show that you've put some effort into your requests.

But I should be going.
Title: Re: How to make a character curve?
Post by: glaft on Wed 16/05/2007 15:55:15
wouldnt that make a triangular trajectory?

if curves are involved, there probably should be some cosines in the middle of this mess.

that said, i'm not good enough in scripting in ags to really tell. mathematically, to get a curve, you'd use something like:
***Warning! Maths included!****
norm_x= (Char.x - destination_x)/2
norm_y_offset=squrt(1-(count-norm_x)/(norm_x))^2)
y_offset=norm_y_offset*peak_offset

(due to sin^2=squrt(1-cos^2)
Title: Re: How to make a character curve?
Post by: monkey0506 on Wed 16/05/2007 16:00:01
Hmm...yeah I thought about that then forgot. Because I'm tired and trying to get off the computer but keep getting distracted. ;)

But it's the basic concept of how the function should be written. Just the mathematical part is bunk as I've encoded a straight lined approach instead of a proper curve. Oh well. 8)
Title: Re: How to make a character curve?
Post by: Ashen on Wed 16/05/2007 16:06:20
This thread (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=9749.0) contains code for a parabolic jumping curve. I think it pre-datd the float type, so it could maybe stand updating, but it seems like it worked OK.
Title: Re: How to make a character curve?
Post by: SupSuper on Wed 16/05/2007 16:13:17
I crudely came up with this using physics.

x=x0+v0x*t
y=y0-v0y*t+0.5*g*(t*t)

(x0 = initial x, y0 = initial y, t = time, g = gravity)

In AGS, it'd end up something like:

float xx, yy, t=0.0;

rep_ex() {
 if (t < 12.0) {
  t += 0.25;
  xx = 100.0+5.0*t;
  yy = 200.0-8.0*t+0.5*1.0*(t*t);
  cEgo.x = FloatToInt(xx);
  cEgo.y = FloatToInt(yy);
 }
}
Title: Re: How to make a character curve?
Post by: Khris on Wed 16/05/2007 16:49:35
That's right, calculating these trajectories becomes a piece of cake as soon as one realises that's it's a linear x-movement combined with a gravity-influenced y-movement.

Btw, using a constant delta t will make the char penetrate platforms (if the resulting delta y > 1). It's therefore necessary to use 1 as delta y, then calculate delta x and delta t from there. (To be precise, calculate the other two values for both delta x = 1 and delta y = 1, then use the x/y pair where both x and y are <=1.
I think Bernie once published a module or template where this was already implemented.

Calculating the initial Vy so that the char will leap exactly 50 pixels into the air is a whole different story, though.

I'm extremely temped to give it a try as soon as I get home.