【问题标题】:How to implement water ripples?如何实现水波纹?
【发布时间】:2009-03-20 05:25:37
【问题描述】:

我正在开发一款 iPhone 游戏。在那我不得不产生水波纹。我不知道如何得到它。听说可以用openGL来做。我对这个概念很陌生。任何人都可以指导我吗?

【问题讨论】:

  • 到底有没有达到效果?

标签: iphone opengl-es


【解决方案1】:

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

Language Agnostic 2d Water Ripple Algorithm

(来源:virgin.net
(来源:virgin.net

OpenGL Project with Water Ripples (Source)

(来源:sulaco.co.za

您可能还想通过GameDev's FAQ 摇摆。向下滚动到“水渲染”部分。

【讨论】:

    【解决方案2】:

    jk:

    z=sin(x)+cos(y)
    

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

    【讨论】:

    • 这是一个非常昂贵的例程。它绝对会在游戏中杀死 OpenGL。我猜他想要一个不错的水效果,这意味着老式的纹理映射技巧。
    • 请原谅;我知道。我在开玩笑。
    【解决方案3】:

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

    导入“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
    

    Also you can download the source code from the Git

    【讨论】:

      猜你喜欢
      • 2011-11-30
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多