Water Ripple Effect Source Code
Posted: 12 March 2013 12:46 AM   [ Ignore ]
Rank
Joined  2013-02-22
Total Posts:  5
New Member

Hi Guys,

I want to create water ripple for one of my project using MT4J but the performance is too slow and also the pixels are not updated properly, please help me out to solve this. Below is my source code plz have a look on that any help will be appreciable.

public class NewWater extends AbstractScene{
    
private MTComponent physicsContainer;
    private 
PImage rocks;
    private 
PImage vidrio;
    
MTComponent compo;
    
Ripple ripple;
    
int counter 0;

    public 
NewWater(AbstractMTApplication mtApplicationString name{
        super
(mtApplicationname);
        
// TODO Auto-generated constructor stub
        
if (!MT4jSettings.getInstance().isOpenGlMode()) {
            System
.err
                    
.println("Scene only usable when using the OpenGL renderer! - See settings.txt");
            return;
        
else
            
System.out.println("Using OpenGL renderer");

        
    
}

    
@Override
    
public void init() {
        
// TODO Auto-generated method stub
        
super.init();
        
rocks getMTApplication().loadImage("rocks.jpg");
        
getMTApplication().background(rocks);
        
ripple = new Ripple();
        
getMTApplication().frameRate(60);
        
//attach gesture
        
getCanvas().addInputListener(new IMTInputEventListener() {
                
public boolean processInputEvent(MTInputEvent inEvt{
                    
if (inEvt instanceof AbstractCursorInputEvt{
                        AbstractCursorInputEvt ce 
= (AbstractCursorInputEvtinEvt;
                        switch(
ce.getId()){
                          
case AbstractCursorInputEvt.INPUT_STARTED:
                            if(
ce.getCursor()==nullSystem.out.println("cursor es null");
                              for (
int j = (int) (ce.getCursor().getCurrentEvtPosY() - ripple.riprad); ce.getCursor().getCurrentEvtPosY() + ripple.ripradj++) {
                                    
for (int k = (int) (ce.getCursor().getCurrentEvtPosX() - ripple.riprad); ce.getCursor().getCurrentEvtPosX() + ripple.ripradk++) {
                                      
if (>= && rocks.height && k>= && rocks.width{
                                        ripple
.ripplemap[ripple.oldind + (rocks.width) + k] += 512;
                                      
}
                                    }
                                  }
                              System
.out.println("Started");
                              break;
                          case 
AbstractCursorInputEvt.INPUT_UPDATED:
                              
                              for (
int j = (int) (ce.getCursor().getCurrentEvtPosY() - ripple.riprad); ce.getCursor().getCurrentEvtPosY() + ripple.ripradj++) {
                                    
for (int k = (int) (ce.getCursor().getCurrentEvtPosX() - ripple.riprad); ce.getCursor().getCurrentEvtPosX() + ripple.ripradk++) {
                                      
if (>= && rocks.height && k>= && rocks.width{
                                        ripple
.ripplemap[ripple.oldind + (rocks.width) + k] += 512;
                                      
}
                                    }
                                  }
                             
                              System
.out.println("Updated");
                              break;
                          case 
AbstractCursorInputEvt.INPUT_ENDED:
                            
                              
System.out.println("Ended");
                              break;
                          default:
                              break;
                        
}
                    }
                    
return false;
                
}
            }
);
    
}
        
    

    
@Override
    
public void drawAndUpdate(PGraphics graphicslong timeDelta{
        
// TODO Auto-generated method stub
        
super.drawAndUpdate(graphicstimeDelta);
        
ripple.newframe();
        
getMTApplication().loadPixels();
        
rocks.loadPixels();
        for (
int loc 0loc rocks.width
                
rocks.heightloc++) {
            rocks
.pixels[loc] ripple.col[loc];
        
}
        rocks
.updatePixels();    
        
getMTApplication().background(rocks);            
    
}

    
class Ripple {
        int i
ab;
        
int oldindnewindmapind;
        
short ripplemap[]// the height map
        
int col[]// the actual pixels
        
int riprad;
        
int rwidthrheight;
        
int ttexture[];
        
int ssize;

        
Ripple() {
            
// constructor
            
riprad 3;
            
rwidth rocks.width >> 1;
            
rheight rocks.height >> 1;
            
ssize rocks.width * (rocks.height 2)
                    * 
2;
            
ripplemap = new short[ssize];
            
col = new int[rocks.width rocks.height];
            
ttexture = new int[rocks.width
                    
rocks.height];
            
oldind rocks.width;
            
newind rocks.width * (rocks.height 3);
        
}

        void newframe
() {
            
// update the height map and the image
            
oldind;
            
oldind newind;
            
newind i;

            
0;
            
mapind oldind;
            for (
int y 0rocks.heighty++) {
                
for (int x 0rocks.widthx++) {
                    short data 
= (short) ((ripplemap[mapind
                            
rocks.width]
                            
ripplemap[mapind rocks.width]
                            
ripplemap[mapind 1] ripplemap[mapind 1]) >> 1);
                    
data -= ripplemap[newind i];
                    
data -= data >> 5;
                    if (
== || == 0// avoid the wraparound effect
                        
ripplemap[newind i] 0;
                    else
                        
ripplemap[newind i] data;

                    
// where data = 0 then still, where data > 0 then wave
                    
data = (short) (1024 data);

                    
// offsets
                    
= ((rwidth) * data 1024) + rwidth;
                    
= ((rheight) * data 1024) + rheight;

                    
// bounds check
                    
if (>= rocks.width)
                        
rocks.width 1;
                    if (
0)
                        
0;
                    if (
>= rocks.height)
                        
rocks.height 1;
                    if (
0)
                        
0;

                    
col[i] rocks.pixels[a + (rocks.width)];
                    
mapind++;
                    
i++;
                
}
            }
        }
    }

    

    
}


Any new code using MT4j for water ripple will be an advantage for my project.

Thanks,
Anish

Profile
 
 
Posted: 12 March 2013 02:20 AM   [ Ignore ]   [ # 1 ]
RankRankRank
Joined  2010-01-28
Total Posts:  332
Sr. Member

Try this one. When you have the code for mt4j please send me a pm or reminder

http://www.openprocessing.org/sketch/43543

 Signature 

MARS New Creations
My twitter account
47 inch pure design multitouch
Info & Zo - YouTube

Profile
 
 
Posted: 12 March 2013 08:06 AM   [ Ignore ]   [ # 2 ]
Rank
Joined  2013-02-22
Total Posts:  5
New Member

Hey Rogiermars,

That code is also produce the same affect as like the previous code, do u have any source code for MT4J compatible ripple effect then please let me know.

Thanks,
Anish

Profile