9

I'm working on an iphone game. In that i had to produce water ripples. I dont know how to get that. I heard thatit can be done with openGL. I am very new to this concept. Can any one guide me?

4

3 回答 3

14

以下是我找到的一些资源:

与语言无关的二维水波纹算法(来源:virgin.net(来源:virgin.net
替代文字
替代文字

带有水波纹的 OpenGL 项目(来源)(来源:sulaco.co.za
替代文字

您可能还想查看GameDev 的常见问题解答。向下滚动到“水渲染”部分。

于 2009-03-20T05:47:19.580 回答
2

杰克:

z=sin(x)+cos(y)

更严重的是,Quartz Composer 基本上不作为效果层之一为你做涟漪吗?还是仅针对 iPhone 3.0 SDK 发布的?

于 2009-03-20T05:47:48.627 回答
0

我找到了水波纹效应的源代码,所以下面的代码可以实现到您的项目中并解决您的问题。

导入“HelloWorldLayer.h”

// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];
    
    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];
    
    // add layer as a child to scene
    [scene addChild: layer];
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    
    if( (self=[super init])) {

        rippleImage = [ pgeRippleSprite ripplespriteWithFile:@"image_old.png" ];
        [ self addChild:rippleImage ];
        CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello Cocos2D Forum" fontName:@"Marker Felt" fontSize:16];
        label.position = ccp( 80 , 300 );
        [self addChild: label];
        [ [ CCTouchDispatcher sharedDispatcher ] addTargetedDelegate:self priority:0 swallowsTouches:YES ]; 
        
        // schedule update
        [ self schedule:@selector( update: ) ];    
                
    }
    return self;
}

float runtime = 0;

-( BOOL )ccTouchBegan:( UITouch* )touch withEvent:( UIEvent* )event {
    runtime = 0.1f;
    [ self ccTouchMoved:touch withEvent:event ];
    return( YES );
}

-( void )ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint pos;
    
    if ( runtime >= 0.1f ) {
        
        runtime -= 0.1f;
        
        // get touch position and convert to screen coordinates
        pos = [ touch locationInView: [ touch view ] ];
        pos = [ [ CCDirector sharedDirector ] convertToGL:pos ];
    
        // [ rippleImage addRipple:pos type:RIPPLE_TYPE_RUBBER strength:1.0f ];    
        [ rippleImage addRipple:pos type:RIPPLE_TYPE_WATER strength:2.0f ];  
        
        
    }
}

-( void )update:( ccTime )dt {
    
    runtime += dt;
    
    [ rippleImage update:dt ];
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)
    
    // don't forget to call "super dealloc"
    [super dealloc];
}
@end

您也可以从 Git 下载源代码

于 2012-10-23T06:10:44.067 回答