您的位置:首页 > 产品设计 > UI/UE

Ogre+ UIKit GUI 摇杆功能的简单实现

2013-01-04 22:49 357 查看
关于旋转问题,发现在横屏时 ,ogreview和viewcontrol的view方向总是相差90度,

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就是我的人物节点。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: