Home > Database > Mysql Tutorial > body text

box2D斜面摩擦力和sprite朝向的兼顾取舍

WBOY
Release: 2016-06-07 15:13:55
Original
1598 people have browsed it

问题起源: 一般游戏的主角player的b2Body的fixedRotation属性都设置为true,即不能旋转,这样可以保证player在受到物理冲力的时候不会扭来扭去,比如超级玛丽他的人物模型始终是直立的。当player遇到斜面物体时,它和斜面之间只有一个接触点,这样就没有摩

问题起源:

        一般游戏的主角player的b2Body的fixedRotation属性都设置为true,即不能旋转,这样可以保证player在受到物理冲力的时候不会扭来扭去,比如超级玛丽他的人物模型始终是直立的。当player遇到斜面物体时,它和斜面之间只有一个接触点,这样就没有摩擦力,这会造成player运动状态的失真。

box2D斜面摩擦力和sprite朝向的兼顾取舍

  • 如果将player的fixedRotation设置为false,即可以旋转,这时摩擦力正常,但player的sprite图片也会跟着旋转,有时却希望sprite仍保持直立。

box2D斜面摩擦力和sprite朝向的兼顾取舍

解决方法:

一、 始终fixedRotation=true,

        当player在斜面上时,将player的body构造成底部为斜边的polygon,并且倾斜角度和斜面角度相同。在平面上时,body底部改为平面。

        缺点:1. 不能适应各种角度的斜面。

                    2. 由于Box2D中b2Fixture不能变形,b2Body对象想要改变形状,其中包含的b2Fixture对象要反复创建,销毁,工作繁复,效率不高。

box2D斜面摩擦力和sprite朝向的兼顾取舍

、当player在平面上时,fixedRotation=true;

        当player在斜面上时,fixedRotation=false;然后player的sprite图片根据斜面的角度转动相应的角度。

  •  具体做法大概是:建一个斜面类继承自CCSprite,类成员包括斜面的b2Body,斜面的角度degree,类方法包括斜面的创建方法createInclinePlane(),在这个方法中得到斜面的degree。
  • 在需要用到degree的时候,用degree的getor()可以获取。

box2D斜面摩擦力和sprite朝向的兼顾取舍

box2D斜面摩擦力和sprite朝向的兼顾取舍

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!