📄 checkcollisionvisitor.java
字号:
/*
*M3DEA - MOBILE 3D ENGINE API
*Copyright (C) 2006 Alexandre Watanabe alexandre_sw@yahoo.com.br
* Diego de Freitas zenon_cc@yahoo.com.br
* Paulo Rodrigo Priszculnik paulorp@paulorp.trix.net
* Rodrigo Arthur Lopes raspl@terra.com.br
*
* This library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite
* 330, Boston, MA 02111-1307 USA
*
*/
package m3dea.physics;
import java.util.Enumeration;
import m3dea.entities.*;
import m3dea.util.BBox;
import m3dea.util.Vector3D;
import javax.microedition.m3g.*;
/**
* Visitor que faz checagem de colis鉶 entre as entidades da cena
*/
public class CheckCollisionVisitor extends EntityVisitor
{
/**
* Cria uma nova inst鈔cia de CheckCollisionVisitor
*/
public CheckCollisionVisitor()
{
}
/**
* Recebe um Sprite para fazer a checagem de Colis鉶
* @param node Sprite para verificar colis鉶
*/
public void visitMobileEntity(Sprite node)
{
if( !node.isCollidable() ) return;
Entity entity = null;
Enumeration en = EntityManager.getInstance().getEntities();
while(en.hasMoreElements())
{
entity = (Entity) en.nextElement();
float[] vec = node.getMoveVector();
if(vec[0] == 0 && vec[1] == 0 && vec[2] == 0) continue;
// calcula colisao 1 frame a frente
BBox box = node.getBoundingBox();
box.translate(vec);
float[] normal = box.checkCollision(entity.getBoundingBox(), vec);
if( normal != null )
{
if(entity instanceof Portal)
{
((Portal) entity).onEnter(node);
//node.setMoveVector( new float[]{0,0,0} );
}
// ocorreu colisao
else if( entity.isCollidable() )
{
float[] resp = Vector3D.mirror(vec, normal);
float[] bounce = node.getBouncefactor();
BBox b = new BBox( box );
b.translate( Vector3D.scalarMul(-1, vec) );
resp = new float[]{resp[0]*bounce[0],resp[1]*bounce[1],resp[2]*bounce[2]};
b.translate( resp );
if( b.checkCollision(entity.getBoundingBox(), resp) == null )
node.setMoveVector( resp );
}
node.onCollision(entity);
}
// restaura valores originais do box
box.translate( Vector3D.scalarMul(-1, vec) );
}
}
/**
* M閠odo n鉶 utilizado pois entidades est醫icas n鉶 colidem
* @param node Entity
*/
public void visitEntity(Entity node){ /* objetos est醫icos n鉶 fazem detec玢o de colis鉶 */ }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -