Calculate tween-time dependent on object's start position

Started by daniel, Sun 05/08/2018 10:06:32

Previous topic - Next topic

daniel

Hey guys,

I have these tweening objects moving and resetting at their final position.
However, when the room first loads I would like them to be at somewhat random positions and I need some way to calculate the required tween-time according to the objects actual start position.
Otherwise, if the object starts close to the end point it would be moving way too slow...

Code: ags
#define Cloud1Speed 50.0
#define CloudsEndPosition 185

void InitClouds()
{
  oBeachCloud1.SetPosition ((Random(20)+120), (Random(20)+20));
  oBeachCloud1.TweenX(Cloud1Speed, CloudsEndPosition, eEaseLinearTween, eNoBlockTween);
}

function room_Load()
{
  InitClouds();
}


The standard distance for the tween is -(Game.SpriteWidth[oBeachCloud1.Graphic]) to "CloudsEndPosition". The correct tween-time for the object starting at -(Game.SpriteWidth[oBeachCloud1.Graphic]) is "Cloud1Speed".
So basically what I need is to get a percentage of how far along X the object starts within the tween-space, so I can use that to calculate the adjusted tween-time, right?

Can anybody tell me how to do this in AGS?

cheers

tzachs

Actually, you don't need to do that, as the tween module has a setting in which you can set speed instead of duration, so the speed can be fixed regardless of the distance.

Code: ags

    oBeachCloud1.TweenX(Cloud1Speed, CloudsEndPosition, eEaseLinearTween, eNoBlockTween, 0.0, eTweenSpeed);

daniel

Thanks tzachs!

Nice, I didn't realize that's possible...gonna give this a shot later.

Snarky

For your original question...

You measure the full distance by taking the differences of the start and end coordinates, then using Pythagoras (square them, add them, take the square root).
You measure the partial distance the same way.
You divide the partial distance by the full distance (as a floating point) to get what proportion of the full distance the partial distance is.
You multiply the tween time by this proportion to get the time it should take to move at the same speed.

SMF spam blocked by CleanTalk