📄 projectile.java
字号:
package balistic;
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Projectile implements Runnable {
double x, y, x0, y0;
double vx, vy, vx0, vy0;
double velocityInit, angle,dist;
double t, t0;
double dt = 0.008;
double kappa, gamma;
double maxHeight;
static final double gravity = -9.81;
double k = 0.0;
double m = 10.0;
int sleeptime = 20;
int iMarkFrequency = 8;
boolean hit;
boolean please_stop;
Thread animator;
Vector Listeners;
public Projectile() {
hit = true;
please_stop = true;
Listeners = new Vector();
velocityInit = 60.0;
angle = 0.9*Math.PI/4;
}
public void setSleepTime(int sleep)
{
if ((sleep>0) && (sleep<1000)) {
sleeptime = sleep;
} else {
System.out.println("The sleep time needs to be between 0 and 1000.");
}
}
public void setMarkFrequency(int mark)
{
if (mark>0) {
iMarkFrequency = mark;
} else {
System.out.println("The mark frequency should be greater than zero.");
}
}
public void setNumSteps(int steps)
{
if ((steps>5) && (steps < 10000)) {
dt = 1/((double)steps);
} else {
System.out.println("The number of steps should be between 5 and 10000.");
}
}
public void run() {
ProjectileListener tListen;
boolean mark;
while (!please_stop && !hit) {
mark = false;
if ( (Math.round(t/dt) % iMarkFrequency) == 0) mark = true;
for (int i=0; i<Listeners.size(); i++) {
tListen = (ProjectileListener) Listeners.elementAt(i);
tListen.setPosition(x*x0,y*y0,vx*vx0,vy*vy0,t*t0,mark);
}
calcPosition(t);
t = t+dt;
if (y>maxHeight) maxHeight = y;
if (y<0.0 || x<0) hit = true;
if (y<0.0) y = 0.0;
try { Thread.sleep(sleeptime); } catch (InterruptedException e) { ; }
}
if (hit) {
t = t-dt;
double[] shotStats = new double[4];
shotStats[0] = x*x0;
shotStats[1] = Math.sqrt(vx*vx*vx0*vx0+vy*vy*vy0*vy0);
shotStats[2] = maxHeight*y0; shotStats[3] = t*t0;
for (int i=0; i<Listeners.size(); i++) {
tListen = (ProjectileListener) Listeners.elementAt(i);
tListen.setPosition(x*x0,y*y0,vx*vx0,vy*vy0,t*t0,false);
tListen.endFiring(shotStats);
}
}
animator = null;
}
private void calcPosition(double time)
{
double ekt;
double gk;
gk = gamma/kappa;
if (kappa>0.0) {
ekt = Math.exp(-kappa*time);
vy = (1+gk)*ekt-gk;
y = (1+gk)*(1-ekt)/kappa-gk*time;
vx = 1*ekt;
x = (1-ekt)/kappa;
} else {
vy = 1-gamma*time;
y = t-0.5*gamma*time*time;
vx = 1;
x = time;
}
}
public static double[] calcPosition(double kap, double gam, double time,
double vxScale, double vyScale, double tScale)
{
double gk, ekt, tt, xScale, yScale;
double[] coord = new double[4];
gk = gam/kap;
tt = time/tScale;
xScale= vxScale*tScale; yScale = vyScale*tScale;
if (kap>0.0000001) {
ekt = Math.exp(-kap*tt);
coord[3] = vyScale*((1+gk)*ekt-gk);
coord[1] = yScale*((1+gk)*(1-ekt)/kap-gk*tt);
coord[2] = vxScale*ekt;
coord[0] = xScale*(1-ekt)/kap;
} else {
coord[3] = vyScale*(1-gam*tt);
coord[1] = yScale*(tt-0.5*gam*tt*tt);
coord[2] = vxScale;
coord[0] = xScale*tt;
}
return coord;
}
public interface ProjectileListener {
public void setPosition(double x,double y,double vx,double vy,double time,boolean mark);
public void beginFiring(double velocity, double angle, double[] endStats);
public void endFiring(double[] shotStats);
}
public void addListener(ProjectileListener ting)
{
Listeners.addElement(ting);
}
public void start() {
if ((animator==null) && (hit == false)) {
please_stop = false;
animator = new Thread(this);
animator.start();
}
}
public void setVelocity(double dVal)
{
if (dVal>0) velocityInit = dVal;
}
public void setAngle(double dVel, double dDist)
{
angle=1/2*Math.asin(9.8*dDist/dVel*dVel);
}
public void setMass(double dVal)
{
if (dVal>0.1) {
m = dVal;
}
}
public void setDist(double dVal)
{
if (dVal>0.1) {
dist = dVal;
}
}
public void setResistance(double dVal)
{
if (dVal>=0.0) {
k = dVal;
}
}
public void fire() {
if (animator != null) {
please_stop = true;
try {
animator.join(1000);
} catch (Exception e) { ; }
}
if (animator==null) {
hit = false;
ProjectileListener tListen;
vx0 = velocityInit*Math.cos(angle);
vy0 = velocityInit*Math.sin(angle);
vx = 1; vy = 1;
t = 0;
t0 = 2*vy0/Math.abs(gravity);
x0 = vx0*t0;
y0 = vy0*t0;
x = 0; y = 0;
kappa = k*t0/m;
gamma = -gravity*t0/vy0;
maxHeight = 0;
double[] endStats = new double[6];
endStats[0] = kappa; endStats[1] = gamma;
endStats[2] = vx0; endStats[3] = vy0; endStats[4] = t0;
for (int i=0; i<Listeners.size(); i++) {
tListen = (ProjectileListener) Listeners.elementAt(i);
tListen.beginFiring(velocityInit,angle,endStats);
}
please_stop = false;
animator = new Thread(this);
animator.start();
}
}
public void stop() { please_stop = true; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -