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

本文介绍如何在iOS应用中整合Ogre 3D引擎与UIKit,通过实例演示了如何创建并控制一个JoyStickView,使其与Ogre场景交互,并解决了横屏时旋转不一致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于旋转问题,发现在横屏时 ,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就是我的人物节点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值