Mathematical Problem

Posted By: jenGs

Mathematical Problem - 11/19/13 23:24

Hi,

I am creating some sort of interactive slideshow.
So here is what I want to do:
I want to increase a value, for example a distance, to a specific value, in a given fixed(!) time.
So far my function works fine. But I need too add a third component with wich I have a lot of problems. I want to smooth the movement in and out. I have no problem to do that with a not fixed timeframe, but I need a fixed one.
My problem is, that I don't now how much time is gone on a specific position, because I use time_step to be framerate independent.

what I have (works fine):
Code:
var camDistance; // filled in another function

void MoveToOrbit(var distance, var time)
{
  var t = 0;
  while(t < time)
  {
    t += 16 * time_step;
    var factor = t / time;  // actual position in timeframe
    
    var distDif = abs(camDistance - distance); //get distance
    var nDist = camDistance;
    if(distance < camDistance)
    {
      nDist -= distDif * factor; 
    }
    else
    {
      nDist += distDif * factor;
    }

    //(...) here the nDist var is put to use, for cameraDistance
    wait(1);
  }
  // (...) here the camDistance value and the actual camera value is set to distance-functionparameter to correct small discrepancies 
}



What I want:

void MoveToOrbit(var distance, var time, var smoothTime)

I hope someone can help me with that.

Greetings,
Patrick
Posted By: Quad

Re: Mathematical Problem - 11/20/13 01:39

Code and usage just copy&paste to sed and run.
Also answers your question of knowing how much time has passed.
Code:
#include <acknex.h>
#include <default.c>
#define PRAGMA_POINTER

// t = time passed
// b = initial value
// c = change in value
// d = duration in which initial value becomes b+c

var easeInOutQuad(var t, var b,var c,var d) {
	if(t>=d)return b +c;
	t /= d/2;
	if (t < 1) return c/2*t*t + b;
	t--;
	return -c/2 * (t*(t-2) - 1) + b;
}

var easeInOutCubic(var t, var b,var c,var d) {
	if(t>=d)return b +c;
	t /= d/2;
	if (t < 1) return c/2*t*t*t + b;
	t -= 2;
	return c/2*(t*t*t + 2) + b;
}
	

void main(){
	level_load(NULL);
	ENTITY* cube = ent_create(CUBE_MDL,nullvector,NULL);
	camera->y -= 200;
	camera->pan = 90;
	
	
	var initial_value = -100;
	var delta_value = 200; // value will be 100 in the end (-100+200 = 100)
	var time_passed = 0;
	var duration = 2;
	
	while(!key_esc){
		if(key_space){
			cube->x = initial_value;
			time_passed = 0;
		}
		
		if(time_passed<duration)cube->x = easeInOutQuad(time_passed,initial_value,delta_value,duration);
		time_passed += time_step/16;//convert time_step to seconds

		wait(1);	
	}
}



Stolen from here: http://www.gizma.com/easing/#quad1
Posted By: jenGs

Re: Mathematical Problem - 11/20/13 10:48

Thank you, that was very helpful.
With some small modification I am able to create a keyframe based movement system.

Greetings,
Patrick
© 2024 lite-C Forums