📄 j2me开发手机游戏物理模型之抛物线-开发者网络-j2me-天极yesky.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0038)http://dev.yesky.com/232/2267232.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>J2ME开发手机游戏物理模型之抛物线-开发者网络-J2ME-天极Yesky</TITLE>
<META content=J2ME开发手机游戏物理模型之抛物线, name=description>
<META content=J2ME开发手机游戏物理模型之抛物线, name=keywords>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN><LINK media=all
href="J2ME开发手机游戏物理模型之抛物线-开发者网络-J2ME-天极Yesky.files/y_mobile_content.css"
type=text/css rel=stylesheet>
<SCRIPT src="J2ME开发手机游戏物理模型之抛物线-开发者网络-J2ME-天极Yesky.files/showlay.js"></SCRIPT>
<META content="MSHTML 6.00.2900.3132" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV id=topmenu><A href="http://www.yesky.com/">Yesky首页</A>| <A
href="http://product.yesky.com/">产品报价</A>| <A
href="http://cseek.yesky.com/">行情</A>| <A href="http://mobile.yesky.com/">手机</A>
| <A href="http://digital.yesky.com/">数码</A> | <A
href="http://notebook.yesky.com/">笔记本</A> | <A
href="http://pc.yesky.com/">台式机</A> | <A href="http://diy.yesky.com/">DIY硬件</A>
| <A href="http://oa.yesky.com/">外设</A> | <A href="http://net.yesky.com/">网络</A>
| <A href="http://dh.yesky.com/">数字家庭</A> | <A
href="http://lab.yesky.com/">评测</A> | <A href="http://soft.yesky.com/">软件</A> |
<A href="http://e.yesky.com/">e时代</A> | <A href="http://game.yesky.com/">游戏</A>
| <A href="http://pic.yesky.com/">图片</A> | <A
href="http://desktop.yesky.com/">壁纸</A> | <A href="http://q.yesky.com/">群乐</A> |
<A href="http://my.yesky.com/">社区</A> | <A href="http://blog.yesky.com/">博客</A>
| <A href="http://www.mydown.com/">下载</A></DIV>
<DIV id=topbox>
<DIV class=tleft><A href="http://www.yesky.com/"><IMG alt=天极Yesky_全球中文IT第一门户
src="J2ME开发手机游戏物理模型之抛物线-开发者网络-J2ME-天极Yesky.files/logo.gif" border=0></A></DIV>
<DIV class=tad><SPAN id=ad1></SPAN></DIV>
<DIV id=search>
<DIV>
<FORM name=searchform action=http://search.chinabyte.com/search method=get
target=_blank><INPUT id=searchzz onmouseover=this.focus() onfocus=this.select()
name=q><INPUT type=hidden value=GB2312 name=encoding><INPUT type=hidden
value=connect name=dir><INPUT type=hidden value=20002 name=cid><INPUT id=search_btn type=submit value=天极搜索> </FORM></DIV>
<DIV class=tdq>
<DIV class=rightarea><A href="http://bj.yesky.com/">北京</A> <A
href="http://hd.yesky.com/">上海</A> <A href="http://gd.yesky.com/">广东</A> <A
href="http://xa.yesky.com/">西安</A><BR><A href="http://cq.yesky.com/">重庆</A> <A
href="http://cd.yesky.com/">成都</A> <A href="http://sy.yesky.com/">沈阳</A> <A
href="http://js.yesky.com/">江苏</A> </DIV>
<DIV class=rightarea_right><A
href="http://my.yesky.com/">用户<BR>登录</A></DIV></DIV></DIV>
<DIV class=tright>
<DIV class=box1>
<SCRIPT src="J2ME开发手机游戏物理模型之抛物线-开发者网络-J2ME-天极Yesky.files/content_text_ad.js"
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="J2ME开发手机游戏物理模型之抛物线-开发者网络-J2ME-天极Yesky.files/flash.js"
type=text/javascript></SCRIPT>
<DIV id=childmenu><A href="http://soft.yesky.com/">软件频道></A><A
href="http://dev.yesky.com/">程序开发></A><A
href="http://dev.yesky.com/devjava/">Java</A><A
href="http://dev.yesky.com/ysdevvb">VB</A><A
href="http://dev.yesky.com/ysdevvc">VC</A><A
href="http://dev.yesky.com/ysdevdelphi">Delphi</A><A
href="http://dev.yesky.com/ysdevc">C/C++</A><A
href="http://dev.yesky.com/web/">Web开发</A><A
href="http://dev.yesky.com/msdn/">微软专栏</A><A
href="http://dev.yesky.com/devydkf/">移动</A><A
href="http://dev.yesky.com/devsjk/">数据库</A><A
href="http://dev.yesky.com/ysdevcxrs">程序人生</A><A
href="http://dev.yesky.com/ysdevrjgc">软件工程</A>|<A
href="http://soft.yesky.com/lesson/">产品中心</A><A
href="http://www.mydown.com/soft/develop/news.shtml">下载</A><A
href="http://q.yesky.com/group/179"><FONT
color=#ff0000>群乐:大家耍QQ</FONT></A></DIV><!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/">天极Yesky</A> > <A
href="http://soft.yesky.com/">软件频道</A> > <A
href="http://dev.yesky.com/">开发者网络</A> > J2ME开发手机游戏物理模型之抛物线</DIV>
<DIV class=adright><SPAN id=ad2></SPAN></DIV></DIV><!--内容块开始-->
<DIV id=conbox><!--左边开始-->
<DIV id=conleft>
<DIV id=contopla1>
<DIV id=__001>
<DIV class=qkong></DIV>
<DIV class=qw1>全文</DIV>
<DIV class=zkong>
<DIV class=kl></DIV>
<DIV class=bj><A href="http://dev.yesky.com/232/2267232.shtml#pls"
target=_self>评论</A></DIV>
<DIV class=kr></DIV>
<SCRIPT type=text/javascript>
var titiepic ="#";
if (titiepic!="#")
{
document.write("<div class=\"kl\"></div><div class=\"bj\"><a href=\"#\" target=\"_blank\">图片</a></div><div class=\"kr\"></div>");
}
</SCRIPT>
</DIV></DIV></DIV>
<DIV class=topadbg><SPAN id=ad10></SPAN></DIV><A name=top></A>
<DIV id=contitle>
<H1>J2ME开发手机游戏物理模型之抛物线</H1></DIV>
<DIV id=conauthor><SPAN>2006-01-10 08:32 </SPAN><SPAN>作者: 罗切斯特 </SPAN><SPAN>出处:
J2ME开发网 </SPAN><SPAN>责任编辑:<A title=向本编辑提问
href="http://comments.yesky.com/t/·½ÖÛ/6,324/2267232.shtml" target=_blank>方舟</A>
</SPAN></DIV>
<DIV class=topadbg><SPAN id=ad9></SPAN></DIV>
<DIV id=conneirong><SPAN id=ad3></SPAN>
<DIV class=guanggao><SPAN id=contentAdv></SPAN></DIV> 物体做抛物线运动是游戏中基本运动物理模型之一!
在PC游戏中可以由重力公式轻易模拟,但在手机游戏中 ,由于多数手机不支持浮点运算因此不能用sin
,cos,来分解初速度。所以只能用近似模拟的方法!我所采用的是:先放大后缩小的模拟方式,并且为了更精确加入了一定的偏移量。
<BR><BR> 先用哈希表列出0-90度的正弦值,并且把值放大100000倍,例如:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>Hashtable anglevalue;<BR>public void
loadAnglevalue()<BR>{<BR>anglevalue = new
Hashtable();<BR>anglevalue.put(String.valueOf(0),new
Integer(0));<BR>anglevalue.put(String.valueOf(30),newInteger(50000));
<BR>anglevalue.put(String.valueOf(60),new
Integer(86603));<BR>anglevalue.put(String.valueOf(90),new
Integer(100000));<BR>……<BR>}</TD></TR></TBODY></TABLE><BR> 这样就可以得出各种角度的正余弦值。<BR><BR> 设初速度为V0
物体当前坐标为x=0,y=0; t为时间 g重力=10;<BR><BR> 根剧力学公式 <BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>Vx=V0*cos&;<BR>Vy=V0*sin&;</TD></TR></TBODY></TABLE><BR> 再根据重力公式:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>x=Vx*t; <BR>y=Vy*t –5*t*t;</TD></TR></TBODY></TABLE><BR> 由于cos&
sin&都是放大了100000倍的所以 再得到手机屏幕坐标的时候应该缩小100000倍<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>x=Vx*t/100000;<BR>y=(Vy*t
–5*t*t)/100000;</TD></TR></TBODY></TABLE><BR> 现在公式中除了t之外都解决了!
现在来解决时间t;<BR><BR> 我们可以在游戏主循环的 中有不断增加t的值 但是因为主循环非常快!以毫秒计算,所以我们应该加入缓冲:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>while
(true){<BR> thisThread.sleep(10);<BR> if(bFire){<BR> tTemp++;<BR> if
(tTemp >10) {<BR> t+=1;<BR> tTemp =
0;<BR> }<BR>}</TD></TR></TBODY></TABLE><BR> 代码中的if (tTemp
>10) 这个值的判断就调整了时间的增长频率!你也可以用if (tTemp
>2)来使时间增长加快 或则用其他数值让时间变慢。注意的一点就是我们的时间也要放大!至于放大多少倍则要看游戏的节奏!我这里暂且放大20000倍,因此公式为:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>x=Vx*t/100000;<BR>y=(Vy*t –5*t*t*20000)/100000;
</TD></TR></TBODY></TABLE><BR> 还有我们需要把 物体初始位置放在 屏幕的下放那就需要加个初始位置常量,公式变为:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>x=Vx*t/100000;<BR>y=(100000*(getHeight()-20))-(Vy*t
–5*t*t*20000)/100000;
</TD></TR></TBODY></TABLE><BR> getHeight()在手机中为得到手机屏幕的高度。好了,来看看用了这个公式后的运行效果(<A
class=bluekey href="http://product.yesky.com/catalog/294/"
target=_blank>NOKIA</A> 7650模拟器 或则unijava模拟器)<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><IMG onmousewheel="return bbimg(this)" height=280
src="J2ME开发手机游戏物理模型之抛物线-开发者网络-J2ME-天极Yesky.files/d6w03v1qh847.gif"
width=214 onload=javascript:resizepic(this) border=0><BR>图1
</DIV></TD></TR></TBODY></TABLE><BR> 这是45度角情况下的抛物线轨迹。<BR><BR> 是不是觉得高度不够呢!运算不够精确!那么我们在Y上加个偏移量来增加高度,公式改为:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>x= (vx+windSpeed )*t /100000; ;<BR>int
pianyi=(t*400000);<BR>if(vy==0){<BR> pianyi=0;<BR>}
<BR>y=(100000*(getHeight()-imgWu.getHeight())-(vy*t-100000*t*t+pianyi))/100000;</TD></TR></TBODY></TABLE><BR> 这里的X轴还加了风速
windSpeed 现实中物体定受风速影响!当然这里的风速也是放大的了; <BR> <BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>if(vy==0){<BR> pianyi=0;<BR>}
</TD></TR></TBODY></TABLE><BR> 这个代码是在平抛的时候就不需要加入高度偏移了。现在再看45度角的 抛物线 <BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><IMG onmousewheel="return bbimg(this)" height=264
src="J2ME开发手机游戏物理模型之抛物线-开发者网络-J2ME-天极Yesky.files/dze93f2ily71.gif"
width=182 onload=javascript:resizepic(this)
border=0><BR>图2</DIV></TD></TR></TBODY></TABLE><BR> 如果你还不满意还可以改动偏移数值来让模拟更精确。下面来看一些角度在不同力度和风速下的轨迹快照:<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><IMG onmousewheel="return bbimg(this)" style="ZOOM: 80%"
height=428
src="J2ME开发手机游戏物理模型之抛物线-开发者网络-J2ME-天极Yesky.files/667ilfm63o5d.gif"
width=389 onload=javascript:resizepic(this) border=0></DIV></TD>
<TD>
<DIV align=center></DIV></TD></TR>
<TR>
<TD>
<DIV align=center></DIV></TD>
<TD>
<DIV align=center></DIV></TD></TR></TBODY></TABLE></DIV><!--广告代码开始-->
<SCRIPT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -