⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ball.java

📁 这是有关J2ME 3D编程的源程序
💻 JAVA
字号:
import java.io.IOException;
import java.util.Random;

import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Group;
import javax.microedition.m3g.Loader;
import javax.microedition.m3g.Node;
import javax.microedition.m3g.Object3D;
import javax.microedition.m3g.RayIntersection;
import javax.microedition.m3g.Transform;
import javax.microedition.m3g.World;

/**
 * Encapsulates our ball. Holds only the movement vector and current coordinates.
 * Also, rotates the ball.
 */
public class Ball
{
    // The ball's mesh
    Group ball = null;
    
    // The ball's movement vector
    float[] moveVec = null;
    
    // The ball's transform
    Transform trBall = new Transform();
    
    // Vector used in computations
    float[] coords;
    
    // How much the ball has been rotated
    float rotated = 0.0f;
    
    // Number of bounces
    int bounces = 0;
    
    // RayIntersection for collision
    RayIntersection ri = new RayIntersection();
    
    /** Constructs our ball and its transform */
    public Ball() throws IOException
    {
        // Use the loader to load it as a Object3D
        Object3D[] ballObj = Loader.load("/res/rediBall.m3g");
        
        // Find the World node (Group)
        int i = 0;
        while(!(ballObj[i] instanceof World))
        {
            i++;
        }
        ball = (World)ballObj[i];
        
        // Let the ball start on the paddle
        trBall = new Transform();            
        coords = VectorOps.vector(0.0f, 0.0f, 10.0f);
    }
    
    
    /**
     * Renders the ball. Also performs all neccecary computations
     * such as mirroring, rotating, etc...
     * Returns TRUE if the ball has passed the paddle and game is over
     */
    public boolean render(Graphics3D g3d, Group walls, float[][][] wallVectors, float[] paddleCoords)
    {
        // Clear transform
        trBall.setIdentity();
        
        // Check if we are moving
        if(moveVec != null)
        {
	        // First rotate the ball a bit        
	        //trBall.postRotate(rotated += 1.0f, 1.0f, 1.0f, 1.0f);

	        // Normalize the movement vector
            ri = new RayIntersection();
            float[] nMove = VectorOps.normalize(moveVec);
	        
	        // See if there is any collision
	        if(walls.pick(-1, coords[0], coords[1], coords[2], nMove[0], nMove[1], nMove[2], ri) && ri.getDistance() <= 0.5f)
	        {
	            // We have collided, get the surface we intersected
	            Node n = ri.getIntersected();
	            
	            // Correct our movement vector by mirroring
	            moveVec = VectorOps.mirror(moveVec, wallVectors[n.getUserID()][2]);
	            
	            // Let user have contol over the movement by moving the paddle
	            if(n.getUserID() == M3GCanvas.PADDLE_WALL)
	            {
	                // Add extra speed to a maximum amount depending on ball/paddle position
	                float distX = (paddleCoords[0] - coords[0]) / 10.0f;
	                float distY = (paddleCoords[1] - coords[1]) / 10.0f;
	                moveVec[0] = Math.max(-0.3f, Math.min(moveVec[0] - distX, 0.3f));
	                moveVec[1] = Math.max(-0.3f, Math.min(moveVec[1] - distY, 0.3f));
	                
	                // After 30 bounces it should be impossibly fast (HAH! A challenge!)
	                moveVec[2] = moveVec[2] + 0.01f;  
	                
	                // Increase number of bounces
	                bounces++;
	            }
	        }
	        
	        // Check for bouncing against screen
	        if(coords[2] >= -0.7f)
	        {
	            // Flip over the screen (same as for paddle)
	            moveVec = VectorOps.mirror(moveVec, wallVectors[M3GCanvas.PADDLE_WALL][2]);
	        }
	        
	        // Move the ball
	        coords[0] += moveVec[0];
	        coords[1] += moveVec[1];
	        coords[2] += moveVec[2];
        }
        
        // If we should quit (paddle let ball through)
        boolean quit = coords[2] <= -7.5f;
        rotated += 15.0f;
        
        // Fix transform
        trBall.postTranslate(coords[0], coords[1], coords[2]);
        trBall.postRotate(rotated, 1.0f, 1.0f, 1.0f);
        
        // Finally: Render the ball
        g3d.render(ball, trBall);
        
        if(quit)
        {            
            moveVec = null;
            coords = VectorOps.vector(0.0f, 0.0f, 10.0f);
        }
        
        return quit;
    }
    
    /** Starts the ball off in a reasonable direction at a random position */
    public void start()
    {
        Random r = new Random();
        bounces = 0;
        moveVec = VectorOps.vector(-0.1f + r.nextFloat() * 0.2f, -0.1f + r.nextFloat() * 0.2f, 0.1f);
        coords = VectorOps.vector(-3.0f + r.nextFloat() * 6.0f, -3.0f + r.nextFloat() * 6.0f, -5.0f);
    }
    
    /** Returns true if ball is moving */
    public boolean isMoving() { return moveVec != null; }
    
    /** Gets number of bounces (for nifty score-print) */
    public int getBounces() { return bounces; }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -