aboutsummaryrefslogblamecommitdiffstats
path: root/src/lib/ephysics/ephysics_body.cpp
blob: b2a4ee953510dbfec4c95bb5362eb8e51f20c0d8 (plain) (tree)
1
2
3
4
5
6
7
8
9
10




                    
                  



                                         
                                                        
                                      
 

                          

                 
                             
                             
                                    




                   






                                                  
             
 
                                                              

                                                                            
                                                              
                                                        




                                                                      
                                      
                         

  



                                 
                  

  
                                     

                       
  
 
                                     
 
                           
               



                   
                    
  
 
                                
                            


                     
           










                                                               





















































                                                                                                                                                                                           
           
                                                                                                    

               
                   
                
                                                                           



                                        


                                
                           
 

                                      
 
                                                   
                                               

                                                                              
                                                          
 

                                                                     


                                                          



                                                                            

      





                                                                     
                                          
      
                                                           





                                             











                                                              




                           



                                       



                           



                                                     
 














                                                             
                  

                                                                               
 

                                                               
                           


      










                                                                         
                                      
                                                                                            

                                        
                                
 

                                                   





                                                                  

                                    
                        












                                                           


                















































                                                                                
           
                                                                                                                         







                                                                                
                                                                                              
 
                                               
                        
                    
                   
                
              
                       
 

                                                     
 
                                              
                                           
      

                                                             
                                              

                                                           
                                                                        
                                                  
                                          
                                                                       


                                                                             

                                                                          

      
             
                                                                       
 
                                                            


           







                                                            

                                           




                    
                                                        




                                        
                                           

                                                            

                                           


      


















                                                                           







                                                                    

                             
 
                                                            
                                                           



            


















                                                                         


                                                         
                                        


                                         


                                                      


                     
                        

                                             



                                
                                    
 
                               

                                     

                                    



                                                        

                                                                                
                                       


                                                                    
                                                                 
                                                     
 


                      

                                                           
 
                                          

                                                                 


                                                                          
                                       


                                                                    
                                                                 
                                                     


           
 

                                          
      



                                                                            
 

                                              
                                                    
           
                                                                  
 

                                                                 
 

                                       

      
                          








                                                
                        



                                                        
                        

 



                                                                    
 
                                              

                                                    





                                                                           











                                                                     




                                                            



                                                                    
                                   




                                                                     

 







                                                                                                                          
                                                                                                  
 

                                                                        
                                           







                                                                 

 















































                                                                                                           


                                                               



                                        

                                                                           




                                                                          
                
             















                                                                         







                                                             
                                         
                                           
                                                              
      

                                                      
                                                 
                         


                                                                                
                                            













                                                                         
                                         
                                           



                                                               
                                                 


                         


                                                                                
                                            












                                                                   


                      
                                                                                               

                       
               







                                                             
                                         


                      


                       



                       
                     




                      
                                                                                                                                                
 


                                                           

                       






                                                 
 
                                                            


                                                    
                      

      
                                             


                                               
                              

      
                                

                                                         
                                                                
                                                    






                                                               


                                             
                            

      
                                         

                                           



                                                          








                                                        
               











                          


           
                                                                                                                                    

                                                



                                                                       




                                                       











                                                                                                  









                                                                         
           
























                                                                           
                                  
                                                              


           
















                                                                             
                                                              

 






                                                               









                                                                        

                                         

 
           



                                                                   
                             

                                             

                                         








                                                                 
                            
           
                                                     





                                                          





                                                                 
                                        




           



                                                         


                                                            
                       
                                                   











                                                                    




                                                                                                                                                 
                                   














                                                                   
                                                                         


                                             
                                                               






                                                           
                                                        
                                                               


                                                       

                                                         















                                                                      


                       





                                                                         
                                                                                    
 
                         
                           
                                
                     



                                               
                 
 

                                                                      

                                             
      
                                                                            

                                                   
                                                               
                                           
 
                                                  

                                                    
                                                           
      

                                                   


                                                                          
      

       
                                                           
 
                                               
                                                                      
      
 


                    


                       
 
                                           


           
                                                                                  
 
                           

                     
                        

                                               

                                                                   

                


                                                          
 
                                              
                                          


                                               





                                                                     




                                                                     
 
                                           
 
                                                                         


           
                                                                                                                           

                                                
            

                                                     
                                                  

            
                                                                         
 
                                         
                                                   







                                                                          
                                            

 
            
                                                                                                                            

                                               
                     



                                            

                                                      






                                                       
           

                                                                   
                                                            















                                                                                 
                           



                                                




































                                                                                 
           


                                       
               
               
                                        



                                                                         
                                                                                
                                                                            
                                                                                   
 
                               

                                                                                 









                                                                               

                                      


                                                      






                                                                        
 






                                                  
                          




              
                                                                                                                              


























                                                                     

                             


















                                                                            
                                                                 




           
                                                        
 
































                                                                  
 








































                                                                                
                                                                       

                                                      




                                                   















































































































                                                                                
                                                                       

                                                      


           
                                                                             
 
                                                 
                           
                     
                     
                 
               
 
                                              

                                                                  



                                                   
 
                                                          
                                               



                                                               
                                               
 
                                    
 

                    
                                                                           


               
                                              
      

                                           

                                                                          

               
 

                                                                  
 

                              


                                                                           
                                                            
                                                                         

 
           

































                                                                         




                                             








                                                           

                                                             
                                                      
 


                                                                  








                                                                          




                                                                         
      

                                                


      
    

                                               


                                                                 



                                                                       
 
                                           


                                                                 


                                                                  


    

                                                      
                               
                             
 
                                                            
                                                          

                                                        
                                                             
                                                              



                                                             


                                                            
 
                              

            

                                                                    


                                                     


                                                              

 
         
                                                                                                                           








                                                                   
                            













                                                                                  
    
                                                                                                     
 
                                      


                          
 
                                                              

            

                                                 

                  
      


                                                        
 
                               
                                                                               
                                         
 


                                                     
                                         
 



                                                                             


             
                                                       



                           





                                                      
















































                                                                                 






































                                                                                     


                                                                           
                                
                                           
 






                                                          
 


                                                         
 


                                                            


                                                                          

















                                                                                 


                                                         

 















































                                                                                 





                                     

                                                                 

                                         
 
                                




                                             














                                                                                 
                                                                             

 


                                                                                   
                                 






                                                                       


                                                    
                      
                                                                                                             

                       
 
                                                                                
                                                  
                          
 
                            

                                                            
 
                                       
                                                 
 
                                                      
                                             
 


               


















                                                                                                                     





                                                                    

                                      
 
                                                     

                                      

                                                               

      
                                                    

                                       


                                                              

      
                                                      

                                       

                                                                      

      
                                                   

                                         

                                                               
      


                                                 


















                                                                                    
                                                 

















                                                              
                                             

 

                                                                                                     
 
                            


                                        
                                       



                                                                    

                                                              
 
                                                 
      




                                                 

 








































                                                                                    



















                                                                                
                    
                                                                                           

                       
                                        

                                   

                                                      








                                                          

                                                                 

                                                                 

                                                                              
 





                                                
                                                      


                  



                                   
                                                
 
                                                            

                                                  











                                                                              




                                                      

                                                   
 



                                                                                




                                       

                                   

                                      


                                             
 

               
          
          

                                             





                    
           
                                                                                                                    





                                                                       



                       


                                            



                                                 






                                                                 


                      

 



                                                                                                             
                     



















                                                                              
                                                                              
                                            

                 
                                





                                         
 



                                                                                 
 





                                                                             








                                            






















                                                                                                                                                           
                                















                                                            
                                                                          
                                           

















                                                                                                           





                          
                                               
                                                       




                                        
                                         









                                                                                                           












                                                                               
                                         


                                                                       
 











                                                                                                                            
 














                                                                            
                                            

 


                                                                                           






                                                 






                                                                            




                                                                          
 


                                                                
 
                                            



                
                                 
                                                                                                          


















                                                            
                                                            
                                                 
                                                           


















































                                                                                 
                                                                     











                                                             
                                                     



























                                                                              

                                         

                                                                      






                                                                              

                                                                      





                                                                             
                                            















                                             
                    
                                                      

                       
                                        


                                   

                                                                              






                                                
                                   

                                                          








                                                                   
                                                





                                                
                                                                


                  
                                              

                                                   
                                                                           









                                                                     



                                                      
 

                                                   
 



                                                                                 




                                       

                                   
                                      

               
          
          

                                             
                                        




                    
                                      


               
                    


























                                                                              
                                                 
 
                                     
                       
 
              
      
                                                  


                    
                                                                    




                                                     
 
                                   
                                                                                
                                                  
                                              

                                      


                    


                                                 
                                               


                                   


                                                                           






                                                
                                   
















                                                                               
                                                                


                  
                                         

                                                   
                                                                            









                                                                     



                                                      
 

                                                   
 



                                                                                 




                                       

                                   
                                      

               
          
              
                                                             

                                        




                    
                                      



                    

                                            
                                     
                       
 
              
      
                                             


                    
                                                              
 
                                   

                                                                           
                                         

                                      

 


                                                                     

                                                                     

                                                    

                             
                       
                     
                     
                     
                   












                                               






                                                             

                                        




                                                       


                                                             

                            







                                                                             

                                               


                         
                         



                                     
                                     

                           
                           


                                     
                       


                                          
                                        
 
                                     
                                    

      












                                                                              
                                      






                                             



                                                                                






                                                   
 
                                   


                                                                             

                                                                     
                                            

                                      

 


                                                            







                                                                              
 


                                                                             
 


                                                                            
 


                                                                           
 


                                                                             
 


                                                                            


                      
                                                                                                                                                                        
















                                                       
                              

                                                      




                                                                              
 



                    
                                                     
 
                            
                       
 
                                                                       
                                                                          
                                                               
               


                    
                                                        
 
                               

                       
                                                                     
                                                                            
                                                             
               


                    
                                                      
 
                            
                       
 
                                                                       
                                                                          
                                                              
               


                    
                                                       
 
                               

                       
                                                                     
                                                                           
























                                                                           
               




                                             

                                                                       
                                 
                            




                                      

                         





                                                      
                             


                                   
                             

                                        

 
         

                                                                                                
                                          
               



                                                                        
               




                                                                         
               


                      
      
                                                               
                                                                         
                                                                                
                                                                            
                                                                                   
                               



                                                                                 
                                                                               
           
      

                             
                                                              
                                                                                 

                       
      


                                                                                 

                                                                                 

                                                                         
      
 
                    
            
 
                                               
                                                                            
                                                                      
 
                                         
                                                                               
                                            
                                                                       
                                                                                    




                                                    
                    
 









                                                                      

                                                              
                                                                                
                                                                 
                                                                                   
                                                    
      
 
                          



                                                                                 
                                                                          
      
 















                                                                    
                                                                                                                                   


             
                                                      


               


                                                                 
                                        
      
                                                                              


               
                                         
                                                      
                                                                     
                                            


         
                                                                                   






                                                  
                                        
      
                                                                              


               
                                         
                                        
                                            


         
                                                                                 






                                                      
                                         
                                      
                                            


         
                                                                                                                                               

                     
                   








                                                      
                                              
                                                                     

                                               

                                                                   

                

                                                                       
                                      
                                                                       


                            










                                                        





                                                                  
                                         
                                                  
                     

                                            










                                                  
                     


         
                                                                                    
 





                                                             
                                                    
                                                                            
                                                                            

 














                                                                                                                     
         
                                                                                             
 
               
 
             
      
                                                        


               
                                               







                                                                         

 
         
                                                                                     






                                                         
                      
 
                                         
                                           


                                                                  
 
                                                                             
                                            

 

                                                                                              
 
             
      
                                                         
               

      
                      
 


                                                                           


         

                                                                                                            





                                                            

                      
                                         


                                                                               
                                            

 

                                                                                                                    





                                                                  
               

      

                      
                                               




                                                                               


         







                                       
                                         













                                                                 
                                            




                                

                                                                                             
             




                                                     

                      
                                         

                                                           
                                            


         
                                                                                                     
 
             




                                                

                      


                                                                              


         











                                                     
                                                                                                                                      














                                                                
                                                           




                                                                      
                                                                            










                                                                              

                                                


           
                                                                                                                    

                              
                        








                                                               




                                                     

                                                     
              

      
                  

 
           
                                                                                                                                     

                              
                        








                                                               





                                                                           

      
                  

 


                                                                       
                                                            






                                                                

 








                                                                      
                                         

                                                     
                                            










                                                         



                                               

 


                                                                 
                                                      






                                                          

 








                                                                
                                         

                                                  
                                            










                                                      



                                              













                                                           












                                                                                      
         
                                                                                      
 
               
                     
 





                                                   

                                               
                                         
                                           







                                                                   
                                            


         
                                                                                                                                    










                                                   
                                         
                                           
                                                                            
                                                                  

                                                                    
                                            


         
                                                                                                                         






                                                       

                      
                                         

                                                                      
                                            


         
                                                                                                                                  






                                                                      

                      

                                                                       
                                                                       


         
                                                                                              






                                                          
                                         
                                           
                                                                        
                                            


         
                                                                                                                          






                                                  

                      
                                         

                                                                       
                                            

 

                                                                                                                                   



                                                                 
               

      

                      


                                                                        

 
                          
                                                                                    
 
                             
                     



                                                 
                    

      







                                         
                                              




                                        
               

 




















                                                                             
         
                                                                          
 
                        
                     
                     






                                                 







                                                       
                                         
                                           
 
                                      

                                              

                       
                                                        

       


                                                                     
 
                                            


         






















                                                       
         
                                                                                    








                                                 
                                         

                                               

                                                                      
                                      
                                            


         
                                                                                                                                  









                                                 
                                         
                                    
                                                                          
                                                                

                                                                  
                                      
                                            


         
                                                                                                  
 





                                                 
                                         
                                    
                                                                             
                                      
                                            


         
                                                                                    
 
                           







                                                  
                                                          
 
                                         
                                          














                                                                                     












                                                  



                            

 
         
                                                                                         








                                                          
                          

 








                                                                
                                                  

















                                                                




















                                                                                





                                                 






















                                                                           























                                                              









                                                   
 
































                                                                          
           
                                                                                                               



























                                                                                 







                                                                                                                            
                                                                                                                                          





























                                                                               
                                                                                                                      
 
                                                     











                                                     
                                   






















                                                        
               
















                                                                 

                                                                                                                                   



                                                          

                                                      





































                                                                              
               



                                                             
               









                                                                        

                                                                                           




                                    

                                                      















                                                                 

                                                                                       




                                    

                                                      























                                                                           
                                                                                                                           
 
                                                     











                                                     
                                   
















                                                        
                               













                                                                 

                                                                                                                                        



                                                          

                                                           














































                                                                             

                                                                                                




                                    

                                                           















                                                                 

                                                                                            




                                    

                                                           






















                                                                           
           
                                                                                                                        



























                                                                           
                                                                                                                                     
 
                                        


                                          

                                                 




                                                               
                                                            
                                                                                





                                                                        

                                                          







                                                                               

                                                                          

 

                                                                                             




                                    

                                                 















                                                                 

                                                                                         




                                    

                                                 











                                                                   
                                                              









                                                                           
           
                                                                                                                              











                                                                       
                                                                                                                                                  














                                                                           
                         

                                                      
                                                                                 
                                                             

                                         


           
                                                                                                                           


                                                                   
                                                                                 
 




                                                                            
                                                                                 


           
                                                                                                                                           





                                               

                                                  


























                                                                              

                                                                              
 


                                                                  
                                                            
                                                                                 

                                                              
                                                                                 

















                                                                               
                                                                                    

                                              


                    
                                                                                                   


                                           

                                                  












                                                                               
                                                                                               




                                           

                                                  










                                                                   
                                                                    





                                                                       

















                                                                                                                              

                                                   

                                                                              

                                                

                                                                         



                                                                                 














                                                                                      

                                                   
                                                                     

                                                
                                                                


                                                                        















                                                                                  

                                                   
                                                                       

                                                
                                                                  


                                                                          




                                           


                   
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Evas.h>
#include <Ecore.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"

#include <BulletCollision/CollisionShapes/btShapeHull.h>
#include <LinearMath/btGeometryUtil.h>

#pragma GCC diagnostic pop

#include <math.h>

#include "ephysics_trimesh.h"
#include "ephysics_private.h"
#include "ephysics_body_materials.h"

#ifdef  __cplusplus
extern "C" {
#endif

#define BODY_CLOTH_CHECK() \
   do { \
     if (body->type == EPHYSICS_BODY_TYPE_CLOTH) \
       { \
          WRN("Not supported for cloth"); \
          return; \
       } \
   } while(0)

typedef struct _EPhysics_Body_Callback EPhysics_Body_Callback;
typedef struct _EPhysics_Body_Evas_Stacking EPhysics_Body_Evas_Stacking;
typedef struct _EPhysics_Body_Soft_Body_Slice EPhysics_Body_Soft_Body_Slice;
typedef struct _EPhysics_Body_Face_Obj EPhysics_Body_Face_Obj;
typedef struct _EPhysics_Quaternion EPhysics_Quaternion;

struct _EPhysics_Body_Callback {
     EINA_INLIST;
     void (*func) (void *data, EPhysics_Body *body, void *event_info);
     void *data;
     EPhysics_Callback_Body_Type type;
     Eina_Bool deleted:1;
};

struct _EPhysics_Body_Collision {
     EPhysics_Body *contact_body;
     Evas_Coord x;
     Evas_Coord y;
     Evas_Coord z;
};

struct _EPhysics_Body_Evas_Stacking {
     Evas_Object *evas;
     float stacking;
};

struct _EPhysics_Body_Soft_Body_Slice
{
     Evas_Object *evas_obj;
     int index;
     struct {
          double x;
          double y;
     } p[3];
     float stacking;
};

struct _EPhysics_Body_Face_Obj {
    EPhysics_Body_Face face;
    Evas_Object *obj;
};

static void
_ephysics_body_cloth_anchor_mass_reset(EPhysics_Body *body)
{
   double anchor_mass;
   anchor_mass = 1 / (body->soft_body->m_nodes.size() * 0.025);

   for (int i = 0; i < body->soft_body->m_anchors.size(); i++)
     body->soft_body->m_anchors[i].m_node->m_im = anchor_mass;

   DBG("Cloth anchors mass reset.");
}

EAPI void
_ephysics_body_soft_body_light_apply(Evas_Map *m, Evas_Coord lx, Evas_Coord ly, Evas_Coord lz, int lr, int lg, int lb, int ar, int ag, int ab, Evas_Coord bx, Evas_Coord by, Evas_Coord bz)
{
   double x, y, z, nx, ny, nz, ln, br;
   Evas_Coord mx, my, mz, mr, mg, mb, ma;
   int i;

   for (i = 0; i < 4; i++)
     {

        evas_map_point_coord_get(m, i, &mx, &my, &mz);
        evas_map_point_color_get(m, i, NULL, NULL, NULL, &ma);

        x = mx;
        y = my;
        z = mz;

        nx = sqrt(pow(bx - x, 2));
        ny = sqrt(pow(by - y, 2));
        nz = sqrt(pow(bz - z, 2));
        ln = nx + ny + nz;

        if (ln != 0.0)
          {
             nx /= ln;
             ny /= ln;
             nz /= ln;
          }

        x = lx - bx;
        y = ly - by;
        z = lz - bz;

        ln = pow(x, 2) + pow(y, 2) + pow(z, 2);
        ln = sqrt(ln);

        if (ln != 0.0)
          {
             x /= ln;
             y /= ln;
             z /= ln;
          }

        br = (nx * x) + (ny * y) + (nz * z);
        if (br < 0.0) br = 0.0;

        mr = ar + ((lr - ar) * br);
        mg = ag + ((lg - ag) * br);
        mb = ab + ((lb - ab) * br);

        evas_map_point_color_set(m, i, mr, mg, mb, ma);
     }
}

static void
_ephysics_body_soft_body_slices_apply(EPhysics_Body *body, Evas_Object *evas_obj, Eina_List *slices)
{
   double rate;
   void *list_data;
   Eina_List *l;
   Evas_Coord wy, wh, y0, y1, y2, x0, x1, x2, z0, z1, z2, w, h, bx, by, bz;
   Evas_Map *map;
   btVector3 p0, p1, p2;
   btSoftBody::tFaceArray faces;
   EPhysics_Body_Soft_Body_Slice *slice;
   int lr, lg, lb, ar, ag, ab;
   Evas_Coord lx, ly, lz;
   Eina_Bool light = EINA_FALSE;
   EPhysics_Camera *camera;

   int px, py, pz, foc;
   Eina_Bool perspective = EINA_FALSE;

   camera = ephysics_world_camera_get(body->world);
   rate = ephysics_world_rate_get(body->world);
   ephysics_world_render_geometry_get(body->world, NULL, &wy, NULL, NULL, &wh,
                                      NULL);
   evas_object_geometry_get(evas_obj, NULL, NULL, &w, &h);

   ephysics_body_geometry_get(body, &bx, &by, &bz, NULL, NULL, NULL);

   if ((body->light_apply) ||
       (ephysics_world_light_all_bodies_get(body->world)))
     {
        ephysics_world_point_light_position_get(body->world, &lx, &ly, &lz);
        ephysics_world_point_light_color_get(body->world, &lr, &lg, &lb);
        ephysics_world_ambient_light_color_get(body->world, &ar, &ag, &ab);
        light = EINA_TRUE;
     }

   if (ephysics_camera_perspective_enabled_get(camera))
     {
        ephysics_camera_perspective_get(camera, &px, &py, &pz, &foc);
        perspective = EINA_TRUE;
     }

   EINA_LIST_FOREACH(slices, l, list_data)
     {
        slice = (EPhysics_Body_Soft_Body_Slice *)list_data;

        faces = body->soft_body->m_faces;
        p0 = faces[slice->index].m_n[0]->m_x;
        p1 = faces[slice->index].m_n[1]->m_x;
        p2 = faces[slice->index].m_n[2]->m_x;

        slice->stacking = p0.z() + p1.z() + p2.z();

        map = evas_map_new(4);

        evas_map_point_image_uv_set(map, 0, slice->p[0].x * w,
                                    slice->p[0].y * h);
        evas_map_point_image_uv_set(map, 1, slice->p[1].x * w,
                                    slice->p[1].y * h);
        evas_map_point_image_uv_set(map, 2, slice->p[2].x * w,
                                    slice->p[2].y * h);
        evas_map_point_image_uv_set(map, 3, slice->p[2].x * w,
                                    slice->p[2].y * h);

        x0 = p0.x() * rate;
        x1 = p1.x() * rate;
        x2 = p2.x() * rate;

        y0 = wh + wy - (p0.y() * rate);
        y1 = wh + wy - (p1.y() * rate);
        y2 = wh + wy - (p2.y() * rate);

        z0 = p0.z() * rate;
        z1 = p1.z() * rate;
        z2 = p2.z() * rate;

        evas_map_point_coord_set(map, 0, x0, y0, z0);
        evas_map_point_coord_set(map, 1, x1, y1, z1);
        evas_map_point_coord_set(map, 2, x2, y2, z2);
        evas_map_point_coord_set(map, 3, x2, y2, z2);

        if (perspective)
          evas_map_util_3d_perspective(map, px, py, pz, foc);

        if (body->back_face_culling)
          {
             if (evas_map_util_clockwise_get(map))
                  evas_object_show(slice->evas_obj);
             else
               {
                  evas_map_free(map);
                  evas_object_hide(slice->evas_obj);
                  continue;
               }
          }

        if (light)
          _ephysics_body_soft_body_light_apply(map, lx, ly, lz, lr, lg, lb, ar,
                                               ag, ab, bx, by, bz);

        evas_object_map_set(slice->evas_obj, map);
        evas_object_map_enable_set(slice->evas_obj, EINA_TRUE);
        evas_map_free(map);
     }
}

static inline double
_ephysics_body_soft_body_slice_calc(double val, double delta, double max)
{
   double ret = val + delta / max;
   if (ret < 0)
     ret = 0;
   else if (ret > 1)
     ret = 1;
   return ret;
}

static EPhysics_Body_Soft_Body_Slice *
_ephysics_body_soft_body_slice_new(EPhysics_Body *body, double delta, double max, int index)
{
   EPhysics_Body_Soft_Body_Slice *slice;
   btSoftBody::tFaceArray faces;

   slice = (EPhysics_Body_Soft_Body_Slice *)calloc(
      1, sizeof(EPhysics_Body_Soft_Body_Slice));
   if (!slice)
     {
        ERR("Couldn't allocate EPhysics_Soft_Body_Slice memory.");
        return NULL;
     }

   faces = body->soft_body->m_faces;

   slice->index = index;
   slice->p[0].x = _ephysics_body_soft_body_slice_calc(
      faces[slice->index].m_n[0]->m_x.x(), delta, max);
   slice->p[0].y = 1 - _ephysics_body_soft_body_slice_calc(
      faces[slice->index].m_n[0]->m_x.y(), delta, max);
   slice->p[1].x = _ephysics_body_soft_body_slice_calc(
      faces[slice->index].m_n[1]->m_x.x(), delta, max);
   slice->p[1].y = 1 - _ephysics_body_soft_body_slice_calc(
      faces[slice->index].m_n[1]->m_x.y(), delta, max);
   slice->p[2].x = _ephysics_body_soft_body_slice_calc(
      faces[slice->index].m_n[2]->m_x.x(), delta, max);
   slice->p[2].y = 1 - _ephysics_body_soft_body_slice_calc(
      faces[slice->index].m_n[2]->m_x.y(), delta, max);

   return slice;
}

static Eina_List *
_ephysics_body_soft_body_slices_get(EPhysics_Body *body)
{
   Eina_List *l, *slices_list, *slices = NULL;
   void *ldata, *slice_data;
   EPhysics_Body_Face_Slice *face_slice;

   EINA_LIST_FOREACH(body->faces_slices, l, ldata)
     {
        face_slice = (EPhysics_Body_Face_Slice *)ldata;
        EINA_LIST_FOREACH(face_slice->slices, slices_list, slice_data)
             slices = eina_list_append(slices, slice_data);
     }

   return slices;
}

EAPI int
ephysics_body_soft_body_slice_index_get(EPhysics_Body *body, Evas_Object *slice)
{
   Eina_List *slices;
   void *ldata;
   EPhysics_Body_Soft_Body_Slice *slice_data;

   if (!body)
     {
        ERR("Can't get soft body slice index, body is null.");
        return -1;
     }

   if (body->type == EPHYSICS_BODY_TYPE_RIGID)
     {
        ERR("Can't get soft body slice index, operation not allowed for rigid"
            " bodies.");
        return -1;
     }

   slices = _ephysics_body_soft_body_slices_get(body);
   EINA_LIST_FREE(slices, ldata)
     {
        slice_data = (EPhysics_Body_Soft_Body_Slice *)ldata;
        if (slice_data->evas_obj == slice)
          return slice_data->index;
     }

   return -1;
}

static void
_ephysics_body_soft_body_slice_del_cb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   EPhysics_Body_Soft_Body_Slice *slice = (EPhysics_Body_Soft_Body_Slice *)data;
   slice->evas_obj = NULL;
   evas_object_event_callback_del(obj, EVAS_CALLBACK_DEL,
                                  _ephysics_body_soft_body_slice_del_cb);
}

static void
_ephysics_body_soft_body_slices_init(EPhysics_Body *body, Evas_Object *obj, Eina_List *slices)
{
   EPhysics_Body_Soft_Body_Slice *slice = NULL;
   btVector3 p0, p1, p2;
   void *slice_data;
   Evas_Coord w, h;
   Eina_List *l;
   Evas *evas;
   Evas_Object *parent;

   evas = evas_object_evas_get(obj);
   evas_object_geometry_get(obj, NULL, NULL, &w, &h);

   parent = evas_object_smart_parent_get(obj);
   EINA_LIST_FOREACH(slices, l, slice_data)
     {
        slice = (EPhysics_Body_Soft_Body_Slice *) slice_data;
        slice->evas_obj = evas_object_image_filled_add(evas);
        evas_object_layer_set(slice->evas_obj,
                              evas_object_layer_get(obj));
        evas_object_image_source_set(slice->evas_obj, obj);
        evas_object_image_source_events_set(slice->evas_obj, EINA_TRUE);
        evas_object_resize(slice->evas_obj, w, h);
        evas_object_show(slice->evas_obj);
        evas_object_image_smooth_scale_set(slice->evas_obj, EINA_TRUE);
        evas_object_event_callback_add(slice->evas_obj, EVAS_CALLBACK_DEL,
                                       _ephysics_body_soft_body_slice_del_cb,
                                       slice);

        if (parent) evas_object_smart_member_add(slice->evas_obj, parent);
     }

   if (slice)
     evas_object_image_source_visible_set(slice->evas_obj, EINA_FALSE);

   _ephysics_body_soft_body_slices_apply(body, obj, slices);
}

static void
_ephysics_body_soft_body_slices_free(Eina_List *slices)
{
   EPhysics_Body_Soft_Body_Slice *slice;
   void *slice_data;

   EINA_LIST_FREE(slices, slice_data)
     {
        slice = (EPhysics_Body_Soft_Body_Slice *)slice_data;
        if (slice->evas_obj)
          evas_object_del(slice->evas_obj);
        free(slice);
     }
}

static void
_ephysics_body_soft_body_slices_clean(Eina_List *slices)
{
   EPhysics_Body_Soft_Body_Slice *slice;
   void *slice_data;
   Eina_List *l;

   EINA_LIST_FOREACH(slices, l, slice_data)
     {
        slice = (EPhysics_Body_Soft_Body_Slice *)slice_data;
        if (slice->evas_obj)
          evas_object_del(slice->evas_obj);
     }
}

static btTransform
_ephysics_body_transform_get(const EPhysics_Body *body)
{
   btTransform trans;
   btVector3 center;
   btScalar radius;

   if (body->type == EPHYSICS_BODY_TYPE_RIGID)
     {
        body->rigid_body->getMotionState()->getWorldTransform(trans);
        return trans;
     }

   body->soft_body->getCollisionShape()->getBoundingSphere(center, radius);
   trans.setIdentity();
   trans.setOrigin(center);
   return trans;
}

static int
_ephysics_body_evas_stacking_sort_cb(const void *d1, const void *d2)
{
   const EPhysics_Body_Evas_Stacking *stacking1, *stacking2;

   stacking1 = (const EPhysics_Body_Evas_Stacking *)d1;
   stacking2 = (const EPhysics_Body_Evas_Stacking *)d2;

   if (!stacking1) return 1;
   if (!stacking2) return -1;