2009-03-20 9 views
9

私はiPhoneのゲームに取り組んでいます。その中で私は水の波紋を生み出さなければならなかった。私はそれを得る方法を知らない。私はそれがopenGLでできることを聞いた。私はこのコンセプトには非常に新しいです。誰も私を導くことができますか?水の波紋をどのように実装するのですか?

+0

あなたは結局、効果を実現しましたか? – Thanks

答えて

14

はここで私が見つけたいくつかのリソースです:

Language Agnostic 2d Water Ripple Algorithm
alt text http://freespace.virgin.net/hugo.elias/graphics/x_water4.gifalt text http://freespace.virgin.net/hugo.elias/graphics/ripples.gif

OpenGL Project with Water Ripples (Source)
alt text http://www.sulaco.co.za/opengl/water.jpg

またGameDev's FAQでスイングすることをお勧めします。 「水の描画」セクションまでスクロールします。

3

JK:

z=sin(x)+cos(y) 

もっと真剣に、クォーツComposerは、基本的な効果層の一つとして、あなたのために波紋をしませんか?それとも、iPhone 3.0 SDKのみで発表されたのですか?

+0

それは本当に高価なルーチンです。それはゲームでOpenGLを絶対に殺すだろう。私は彼がまともな水の効果を望んでいると推測しています。これは古い学校のテクスチャマッピングの仕掛けを意味します。 – pestilence669

+0

Pardon;知っている。私は冗談を言っていた。 – dlamblin

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 

Also you can download the source code from the Git

関連する問題