LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1597|回复: 28

求助,物体运动程序设计问题

[复制链接]
发表于 2004-8-9 10:53:32 | 显示全部楼层 |阅读模式
一个物体在一定范围内左右移动着向下运动,也就是说物体的y坐标是递增的,x的坐标是一定范围内递增递减。
比如,先向左运动,然后再从左向右运动,然后再从右到左,如此反复
运动过程看上去要平滑,下面的公式只是突然产生向左向右的结果
enemys.x = (int)cos((double)enemys.y * 0.0174532925 ) * 100 + 400;
我是参考下面的程序写的
          // Move them left (their speed+1)*0.5*dt units
          enemies.xadd(-((float)espeed+1)*dt*0.5);
          // If the sprites are colored blue then
          if(einfo>0)
          {
            // Adjust their y coordinate. I wanted smooth motion so
            // we make their y position equal the cosinus of
            // their x coordiante+some random number that prevents
            // everyone from following the same path
            // Since we have to convert the degrees into radians, we
            // multiply the value with 0.0174532925 (pi/180)
            // The cosinus function returs a value from -1 to +1, so we
            // multiply it with 100 to get some reasonable value
            // We also add 230 to the end result to center everything
            // on the screen
            enemies.yset(
             ( cos((enemies.getx()+einfo)*0.0174532925) *100)+230);
他的效果是上下运动从左向右移动。
谁能提供一个左右运动从上到下移动的公式?我的数学基础差,又没课本在手,想了很久都没想出来,各位牛人帮帮忙?
发表于 2004-8-9 11:30:51 | 显示全部楼层
那不就是把X和Y方向的位移,速度和加速度都互换一下吗?
发表于 2004-8-9 17:22:28 | 显示全部楼层
只用两个速度分量vx和vy就可以了,运算时用浮点,显示时换成整数。

你在写横版或纵版打飞机游戏么? 还是雪花飞降呀?
 楼主| 发表于 2004-8-9 19:50:54 | 显示全部楼层
小弟真得很笨,可以举出公式么?
我正在写仿金山打字游戏中的太空大战
 楼主| 发表于 2004-8-9 19:52:19 | 显示全部楼层
To doubleelec,我上面就是互换了的公式,不行啊
发表于 2004-8-9 20:03:54 | 显示全部楼层
正弦波啊。

(把X和Y互换)
发表于 2004-8-9 20:13:28 | 显示全部楼层
最初由 doubleelec 发表
那不就是把X和Y方向的位移,速度和加速度都互换一下吗?


有加速度吗?
发表于 2004-8-9 20:14:30 | 显示全部楼层
这个看具体要什么效果了。从屏幕左上角到右下角有n种曲线的。选个喜欢的,把用参数方程表示出来不就可以了,限定一下参数的取值范围就可以了。
发表于 2004-8-9 20:16:49 | 显示全部楼层
x、y是坐标,w1、w2是x的范围,ax、ay是x、y的速度,
每次增加时:

  1. y += ay;
  2. x += ax;
  3. if(x <= w1 || x >= w2)
  4.   ax = -ax;
复制代码

要平滑的话就要把时间间隔设小一点,或ax、ay设小一点。
发表于 2004-8-9 20:26:36 | 显示全部楼层
sorry!

严格来说一个周期内会在两瞬间产生加速度(左右方向改变那一刻!)。

除此,都是匀速运动。

这个公式没看懂是什么意思:enemys.x = (int)cos((double)enemys.y * 0.0174532925 ) * 100 + 400;

真接用sin()再放大不好吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表