About shooting bouncing ball

1 reply [Last post]
Offline
Joined: 01/06/2011

I wonder, could Jason share his code for the shooting bouncing ball?

Offline
Joined: 12/13/2010
This is the class I use for

This is the class I use for the boncy bullet.  I use the terrain's getNormal method to get the normal of the landing point of the boncy ball.

import java.util.ArrayList;
import env3d.advanced.EnvTerrain;

public class Bullet extends Creature
{
    private double yaw, pitch;
    private double speed, initSpeed;
    private double gravity;
    
    private double dir_x, dir_y, dir_z;

    public Bullet(double x, double y, double z, double yaw, double pitch, EnvTerrain terrain)
    {
        super(x, y, z, terrain);
        setScale(1);
        setTexture("textures/granite.gif");        
        this.initSpeed = 1;
        this.speed = initSpeed;
        this.setRotateY(yaw);
        this.yaw = yaw;
        this.pitch = pitch;
        gravity = 0.02;
                
        dir_x = Math.sin(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch));
        dir_z = Math.cos(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch));
        dir_y = Math.sin(Math.toRadians(pitch+20));    
    }
    
    public void move(ArrayList<Creature> creatures, ArrayList<Creature> dead_creatures)
    {       
        for (Creature c : creatures) {
            if (this != c && !dead_creatures.contains(c) && this.distance(c) < c.getScale()+getScale()) {
                dead_creatures.add(this);
                dead_creatures.add(c);
            }
        }
        
        dir_y -= gravity;
        gravity *= 1.005;
        double newX = getX()-speed*dir_x;
        double newY = getY()+speed*dir_y;
        double newZ = getZ()-speed*dir_z;
        
        if (Double.isNaN(terrain.getHeight(newX, newZ)) || newY < terrain.getHeight(newX, newZ)) {
            if (!Double.isNaN(terrain.getHeight(newX, newZ)) && speed > 0.5) {
                dir_x = -1*terrain.getNormal(newX, newZ).x;
                dir_y = terrain.getNormal(newX, newZ).y;
                dir_z = -1*terrain.getNormal(newX, newZ).z;
                speed -= 0.1;
            } else {
                dead_creatures.add(this);
            }
        } else {
            setX(newX);
            setY(newY);
            setZ(newZ);
            
            setRotateX(getRotateX()-1);
        }
    }        
}