Math 数学:运动取决于时间
我有一道数学题: 我有一个函数,其中唯一的参数是当前时间。 返回应该是用于将对象放置在某个位置的位置Math 数学:运动取决于时间,math,motion,Math,Motion,我有一道数学题: 我有一个函数,其中唯一的参数是当前时间。 返回应该是用于将对象放置在某个位置的位置 int position(int time) { int x = 0; //TODO implement x depending on time return x; } 基本上,每一帧都调用这个函数,让物体运动起来 议案应该是这样的(这是实际问题): 一次线性运动,物体以恒定速度运动 时间B无运动,对象停止 重复第1步 谢谢 编辑:好,换句话说:想象一辆汽车以恒定速度行驶一分钟
int position(int time)
{
int x = 0; //TODO implement x depending on time
return x;
}
基本上,每一帧都调用这个函数,让物体运动起来
议案应该是这样的(这是实际问题):
时间X的汽车在哪里?由于您提供的信息有限,没有什么比这更具体的了
return x * THE_SPEED
可以建议
第二个条件可能是返回值的最大值,但如果没有上下文,很难判断
第三种规格令我困惑。对于前两个时间间隔,您可以这样做:
time = min(time, TIME_INTERVAL_A);
return time * CONST_SPEED;
这将导致您的对象在时间间隔a内以恒定速度移动,然后停止。我不确定是否正确理解了您的问题,但您想要这样的东西吗 (假设C)
好的,如果我理解正确:
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;
}
int位置(int时间)
{
循环的整数计数=时间/(A+B);
当前周期内的整数时间=时间%(A+B);
int time\u moved\u this\u cycle=(当前周期中的时间
假设整数除法,等等-如果A和B是非整数等等,你需要
floor()
s。如果我理解正确,你有一个物体以恒定的速度移动一段时间,然后停止,然后再次开始(以相同的速度?),然后停止,然后开始,等等?它沿1D路径移动,因此您感兴趣的唯一输出是与原点的距离
我建议您定义一个名为speedAt(time T)
的辅助函数,它可能类似于:
if 0 < T <= 25 then 5;
if 25 < T <= 32 then 0;
if 32 < T <= 47 then 3;
if 47 < T <= 49 then 0;
if 49 < T <= 125 then 1;
if 125 < T then 0.
你可能觉得这有点乱。一种更复杂的方法是从数据结构中得出speedAt的值,但我希望您能了解情况。类似于
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;
}
int位置(int t)
{
静态int pt=-1;
静态int px=0;
静态int state=1;//正在运行。。。
静态int lastt=0;
如果(pt<0){pt=t;lastt=t;}
如果(状态==1){
如果((t-pt)>A){
状态=0;
pt=t;
}
}否则{
如果((t-pt)>B){
状态=1;
pt=t;
}
}
px+=状态?速度*(t-lastt):0;//前进
lastt=t;
返回px;
}
编辑上一代码用法的注释
该代码旨在“运行时”使用:只要时间t一劳永逸,它就不会给出任何结果。它被编程为在每次调用函数时,根据上次调用所经过的时间,“移动”汽车一步。适用于“游戏”,其中func每“滴答”左右调用一次,并且必须随着滴答的增加更新汽车的位置,以便可以在屏幕上以当前位置逐个滴答地绘制
如果OP问题是关于“从数学上”知道时间t时汽车的位置,那么其他解决方案是好的(同样,请阅读我对该问题的第二条评论)家庭作业?如果是这样的话,就贴上这样的标签。不,不是家庭作业。个人项目。一个人期望时间总是增加。。。所以A只发生一次,B也发生一次。。。或者A和B是间隔?即使是间隔,重复也不起作用,因为间隔只发生一次(除非一个间隔朝向无穷大)。。。指定更好的等待。。。要回答最后一个问题,你不需要编程,只需要简单的数学!给定时间X(从0开始计数),X/(a+B)是X中有多少个“AB”段;对于其中的每一个(让我们来做一个整体),汽车都是通过Av移动的;“提醒”必须分开处理:设为Y。如果YA,分形(X/(A+B))*A,否则。。。像这样……我想他说的是,移动/停止循环每A+B重复一次,即循环计数cc=t/(A+B),循环中的时间tc=t%(A+B),这个循环中移动的时间tmc=(tc
time\u in\u current\u cycle=time-(count\u of\u AB\u cycles*(A+B))
,如果你处理的是浮动,而不是我这里的mod,这将是正确的方法,再次感谢你的回答。是否可以私下与您联系以了解后续问题D
distanceTravelled = 0
for t = 1 to T
distanceTravelled = distanceTravelled + speedAt(t)
end
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;
}