Ogre+ UIKit GUI 摇杆功能的简单实现
2013-01-04 22:49
357 查看
关于旋转问题,发现在横屏时 ,ogreview和viewcontrol的view方向总是相差90度,
我是按照上面设置的~~反正不用ogre的触摸了,ois代码该删的删,改减的减 ,就不管其他的了~~
--------------------
本人是菜鸟,对UIKit的控件较为熟悉。而且Ogre中众多的GUI对iOS设备及中文的支持都略有不足,所以暂时用UIKit的控件。
游戏场景
这里有刻度的圆盘就是joystick.
这里会发现joystick是贴在Ogre场景上面,原因如下:
在iOS中,ogre的场景就是渲染在一个UIWindows的view上,所以,我们可以获取这个UIWindow,从而获取ogre所在view,将view放到我们自己管理的viewcontroller上面。
然后在这个view上面放上joystick。
可以在ogre iPhone工程 [self go]下面加入托管
之后可以实现一个JoyStickView,仿照永恒战士覆盖设备的左半边
JoyStickView.h
JoyStickView.m
我把JoyStickView放到了我的viewcontroller中,这里viewcontroller要承接UIKit和ogre,所以要编写c++,将.m文件改为.mm后缀就行
回调方法:
这里DemoApp就是Ogre场景管理的类,源自ogre的iphone例子,扩展了单例可以在viewcontroller里使用humanNode就是我的人物节点。
self.view.transform = CGAffineTransformMakeRotation(-M_PI/2); ogreView.transform = CGAffineTransformMakeRotation(0); ogreView.center = self.view.center;
我是按照上面设置的~~反正不用ogre的触摸了,ois代码该删的删,改减的减 ,就不管其他的了~~
--------------------
本人是菜鸟,对UIKit的控件较为熟悉。而且Ogre中众多的GUI对iOS设备及中文的支持都略有不足,所以暂时用UIKit的控件。
游戏场景
这里有刻度的圆盘就是joystick.
这里会发现joystick是贴在Ogre场景上面,原因如下:
在iOS中,ogre的场景就是渲染在一个UIWindows的view上,所以,我们可以获取这个UIWindow,从而获取ogre所在view,将view放到我们自己管理的viewcontroller上面。
然后在这个view上面放上joystick。
可以在ogre iPhone工程 [self go]下面加入托管
UIWindow *ogreWindow=nil;; Ogre::RenderWindow *ogreRenderWindow = Ogre::Root::getSingleton().getAutoCreatedWindow(); ogreRenderWindow->getCustomAttribute("WINDOW", &ogreWindow); UIView *ogreView = ogreWindow.rootViewController.view; RootViewController *rootViewController = [[RootViewController alloc] init]; [ogreView removeFromSuperview]; [rootViewController addOgreView:ogreView]; [rootViewController initScene]; [ogreWindow setRootViewController:rootViewController]; [rootViewController release];
之后可以实现一个JoyStickView,仿照永恒战士覆盖设备的左半边
JoyStickView.h
// // JoyStickView.h // Qins // // Created by jeroen on 13-1-3. // // #import <UIKit/UIKit.h> @class JoyStickView; @protocol JoyStickViewDelegate <NSObject> - (void)JoyStick:(JoyStickView *)joyStickView degree:(double)degree distance:(double)distance; @end typedef enum { JoyStickModeLeft, JoyStickModeRight }JoyStickMode; @interface JoyStickView : UIView { id<JoyStickViewDelegate> delegate; UIImageView *joyStickView; JoyStickMode joyMode; CGPoint centerPoint; } @property (nonatomic, retain) UIImageView *joyStickView; @property (nonatomic, assign) CGPoint centerPoint; @property (nonatomic, assign) id<JoyStickViewDelegate> delegate; - (id)initWithFrame:(CGRect)frame withMode:(JoyStickMode)joyStickMode; @end
JoyStickView.m
// // JoyStickView.m // Qins // // Created by jeroen on 13-1-3. // // #import "JoyStickView.h" @implementation JoyStickView @synthesize joyStickView; @synthesize centerPoint; @synthesize delegate; - (id)initWithFrame:(CGRect)frame withMode:(JoyStickMode)joyStickMode { self = [super initWithFrame:frame]; if (self) { UIImageView *temp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"eye.png"]]; [temp setFrame:CGRectMake(0, 0, 160, 160)]; [temp setCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)]; [temp setAlpha:0]; CGAffineTransform at =CGAffineTransformMakeRotation(M_PI/2); [temp setTransform:at]; self.joyStickView = temp; [self addSubview:temp]; [temp release]; joyMode = joyStickMode; [self setBackgroundColor:[UIColor clearColor]]; } return self; } - (void)drawRect:(CGRect)rect { // Drawing code } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [joyStickView setAlpha:1.0]; UITouch *touch = [touches anyObject]; CGPoint curPoint = [touch locationInView:self]; centerPoint = CGPointMake(curPoint.x, curPoint.y); [joyStickView setCenter:centerPoint]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [joyStickView setAlpha:0]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [joyStickView setAlpha:0]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [joyStickView setAlpha:1.0]; UITouch *touch = [touches anyObject]; CGPoint curPoint = [touch locationInView:self]; //设置的横屏 ogre的旋转和UIKit的旋转不知道为什么没有统一 所以这里我的x y轴对调了.... 也没有解决~~ CGPoint disPoint = CGPointMake(curPoint.x-centerPoint.x, curPoint.y - centerPoint.y); double length = sqrt(disPoint.x*disPoint.x + disPoint.y*disPoint.y); double degree; if (disPoint.x >= 0) { if (disPoint.y <= 0) { degree = 180 - asin(disPoint.x/length)*180/M_PI; } else if(disPoint.y > 0) { degree = asin(disPoint.x/length)*180/M_PI; } } else if(disPoint.x < 0) { if (disPoint.y <= 0) { degree = -180 -asin(disPoint.x/length)*180/M_PI; } else if(disPoint.y > 0) { degree = asin(disPoint.x/length)*180/M_PI; } } if ([delegate respondsToSelector:@selector(JoyStick:degree:distance:)]) { [delegate JoyStick:self degree:degree distance:length]; } } - (void)dealloc { [JoyStickView release]; [super dealloc]; } @end
我把JoyStickView放到了我的viewcontroller中,这里viewcontroller要承接UIKit和ogre,所以要编写c++,将.m文件改为.mm后缀就行
回调方法:
- (void)JoyStick:(JoyStickView *)joyStickView degree:(double)degree distance:(double)distance { Ogre::SceneNode *humanNode = DemoApp::getSingletonPtr()->humanNode; DemoApp::getSingletonPtr()->humanNode->setOrientation(Ogre::Quaternion(Ogre::Degree(degree), Ogre::Vector3::UNIT_Y)); }
这里DemoApp就是Ogre场景管理的类,源自ogre的iphone例子,扩展了单例可以在viewcontroller里使用humanNode就是我的人物节点。
相关文章推荐
- Java_GUI创建单机版QQ聊天小程序并实现简单的小功能(附所有源码)
- Java编程实现游戏中的简单碰撞检测功能示例
- UDP实现简单的群聊功能代码示例
- DrawerLayout实现简单的侧滑功能
- OpenCV简单亮度校正功能实现
- C#摄像头实现拍照功能的简单代码示例
- MVC3使用MvcPager实现简单分页功能
- js实现简单div拖拽功能实例
- DeDecms中利用关键词实现简单tag功能的php代码
- 创建图形用户界面GUI和事件监听机制的简单实现(java)
- 超简单实现iOS列表的索引功能
- jquery实现简单的图像传送带功能
- 高通Vuforia+Unity——实现简单的AR功能
- 用MVC开发模式实现servlet简单的登录功能(带图片验证码)
- C#实现树型结构TreeView节点拖拽的简单功能,附全部源码,供有需要的参考
- C#实现简单的JSON序列化功能代码实例
- Python实现简单的电话簿功能
- 用zookeeper实现简单的发布订阅功能
- C语言实现简单的ATM机功能
- 一个较简单的js实现checkbox的全选与全不选功能