Determining animation speed based on distance [solved]

Started by DrewCCU, Thu 17/06/2010 03:07:56

Previous topic - Next topic

DrewCCU

I have a couple quick questions here.  Let's knock out the easy one first:

Q1
AGS reads the "location" of a character as the coordinates where the bottom center of the graphic is.  (so say you click on a circle on the ground with your mouse then the bottom center of the characters graphic will move to that circle (in most cases the player character has feet so it looks as if his feet are at that location)).  Okay - well a while back someone told me you could change this by altering the "z" property of the character. (so if you have maybe a snake for a character then you wouldn't want his tail to move to the point you click on, you'd want his head to move there.)

Anyway, can the same be done with objects?  Now if i'm correct AGS does things a little different with objects.  Instead of the bottom center, AGS reads the bottom left corner for object location/positioning.  I'm making a set of laser beams that "fly/shoot" to whereveryou click and as of now if you click a location, ags is putting the bottom left corner of the object sprite there, not the top center like i would like.  (i'd like to avoid using these lasers as a character if possible).

okay, so onto my next question:

Q2
In this first person part of the game where you're firing lasers (the lasers come from off the bottom of the screen to the target) i have a nifty little laser animation that works pretty well but the problem is if you click on a location closer to you "aka bottom of the screen" then the lasers get there faster obviously, and the animation doesnt finish before reaching the location.  So i need some sort of formula that will allow me to match the distance traveled with the animation speed (so that the animation finishes as it reaches the destination).
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

Ryan Timothy B

Answer to question 1, use:
Code: ags

Game.SpriteWidth[object[0].Graphic]


You could then divide the width in half and subtract it from the X.  Or whatever you end up doing with the object to have it centered.

DrewCCU

Quote from: Ryan Timothy on Thu 17/06/2010 05:36:25
Answer to question 1, use:
Code: ags

Game.SpriteWidth[object[0].Graphic]


You could then divide the width in half and subtract it from the X.  Or whatever you end up doing with the object to have it centered.

could you explain this a little better please?
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

Khris

Say the player clicked at x;y and the object dimensions are w & h.
To position the object so that its top center is at x;y, the bottom left corner needs to move to x - w/2; y + h-1.

Ryan explained how to get the dimensions of an object. Object.Graphic returns the slot number, so you'll have to use oLaser.Graphic inside the room script or object[index].Graphic in the global script.
Game.SpriteWidth/Height[] are two arrays that store the sprite dimensions of the slots.

tzachs

As for the 2nd question:
Are you sure you want to change the animation speed and not the movement speed?
Anyway, the method will remain the same, so let's assume that it is the animation speed.
What I suggest is using linear interpolation.
By trial and error search for the appropriate animation speed for the farthest and closest locations.
Let's say the farthest location is in distance d0 with animation speed s0,
and the closest location is in distance d1 with animation speed s1.

So you have two equations of:
d0 = a*s0 + b
d1 = a*s1 + b

Extract a & b from these equations, and each time before shooting the laser, by using the distance d of your current target, you can find the animation speed with the extracted a&b.

DrewCCU

#5
tzachs,

when you said linear interpolation i knewyou were onto something.  I recall linear interpolation from my days of schooling (lol).  But your forumlas you posted confused me a bit.  So i researched linear interpolation and got the actual formula for it ... with that plus what you said i was able to come up with this (which makes more sense to me):

so assume d0 (the furthest distance away) = 500, and d1 (the closest distance) = 100. (both 500&100 represent Y screen cooridnates in ags).  The animtion speed (from furthest to closest) are - let's just say: s0= 5 and s1= 1.  So what we have now is:

(500, 5) & (100, 1) -- where the x value in these coordinates represent distance and the y values represent animation speed.

The only thing we are missing that we need for the linear interpolation formula is another distance.  But this is easy to get - this distance equals the y cooridnate of wherever the player clicks on the screen.  So lets assume the player clicks on a location where the y value is 250 (so dead center between our minimum and maxiumum).  Now using linear interpolation we can figure out what the animation speed should change to for a y vaule (distance) of 250.

The formula:
s3 = (((d3 - d1)(s0 - s1)) / (d0 - d1)) + s1

with:
d0 = 500 (max. distance)
d1= 100 (min. distance)
d3 = 250 (player selected distance)
s0 = 5 (max. speed)
s1 = 1 (min. speed)
s3 = the speed needed to get to the player selected distance (d3).

So we plug everything in and solve for S3,

Solving the equation:
s3 = (((250 - 100)(5 - 1)) / (500 - 100)) + 1

i'm not going to do all the math here but it comes out to s3 = 2.5  .... and to be honest we didn't need the formula to figure this out because obviously if your choosing a point halfway between the minimum and maximum distances then the speed is going to be half of the max. speed.  So i chose 250 as an example to make sure the formula worked and i wasn't doing something wrong.  And it indeed does work.

Now, i'm not home at the moment so i can't test this out in ags but the only problem i forsee running into is that i'm pretty sure animation speeds can't accept non-whole numbers. So it wouldn't recognize 2.5. Which isn't a huge issue cus the animation doesnt have to be exact ... just close enough.  So i would need to tell ags to round the result of s3 to a whole number.

Now, tzachs, it's possible that your forumlas do the exact same thing i just did only a much simpler way ... but i could not see it.

"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

tzachs

Yes, it's just a different representation...
My formulas are just the standard ones to represent a straight line, the formula you found is an extraction of mine.
Take your values and put in my formulas, you'll get:

500 = 5a + b
100 = 1a + b

Extract a&b, you'll get:
a = 100
b = 0

Now with 250 you will get:

250 = 100 * x + 0

x = 2.5

Same results...

DrewCCU

yeah i see now ... but can AGS solve equations like that?
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

Khris

If by solve you mean evaluate a term without unknowns, sure.

SMF spam blocked by CleanTalk