我有个数学问题:
我有一个函数,其中唯一的参数是当前时间。返回应该是一个用于将对象放置在某个位置上的位置。
int position(int time)
{
int x = 0; //TODO implement x depending on time
return x;
}因此,基本上,这个函数被称为每一个帧,以使物体运动。
议案应是这样(这才是真正的问题):
处停止
谢谢!
编辑:好的,换句话说:想象一辆汽车以恒定的速度开了A分钟,然后停了B分钟。然后再开车一分钟,再停一次B分钟。
X时的车在哪里?
发布于 2010-06-10 13:46:43
好吧,如果我理解正确的话:
int position(int time)
{
int count_of_AB_cycles = time / (A + B);
int time_in_current_cycle = time % (A + B);
int time_moved_this_cycle = (time_in_current_cycle < A) ? time_in_current_cycle : A;
int total_time_moving = (count_of_AB_cycles * A) + time_moved_this_cycle;
return total_time_moving * speed;
}假设整数除法,等等,如果A和B是非整数,则需要floor()s。
发布于 2010-06-10 13:39:29
由于你所提供的信息有限,没有比这更具体的了
return x * THE_SPEED可以建议。
第二个条件可能是返回值的最大值,但是没有上下文很难判断。
第三个规格使我感到困惑。
发布于 2010-06-10 13:59:27
有点像
int position(int t)
{
static int pt = -1;
static int px = 0;
static int state = 1; // running...
static int lastt = 0;
if (pt < 0) { pt = t; lastt = t; }
if ( state == 1 ) {
if ( (t-pt) > A ) {
state = 0;
pt = t;
}
} else {
if ( (t-pt) > B ) {
state = 1;
pt = t;
}
}
px += state ? SPEED*(t-lastt) : 0; // advance
lastt = t;
return px;
}编辑关于prev代码使用的注释
代码意味着要使用“运行时”:它不给出任何结果,只要时间t为一次。它被编程为“移动”几乎每一步的汽车每一次调用功能,根据多少时间是从以前的调用。适用于“游戏”,其中漏斗被称为每一个“滴答”左右,汽车的位置必须更新随着滴答增加,以便它可以画在屏幕上,滴答滴答,在当前的位置。
如果OP的问题是关于知道车在时间t,“数学上”,其他的解决方案是好的(同时,读我对这个问题的第二个评论)
https://stackoverflow.com/questions/3014844
复制相似问题