import vrml.*;
import vrml.node.*;
import vrml.field.*;
public class ball extends Script {
//eventouts
private SFVec3f ball_translation;
//eventins
//private SFVec3f ball_location;
private SFRotation ball_angle;
private SFBool ball_start;
//variables
public float ball_current_translation[];
private float ball_ang;
private float ball_speed;
private float ball_speed_x;
private float ball_speed_y;
private float gravity;
private boolean ball_thrown = false;
private int target_angle_i = 40;
public void initialize () {
ball_current_translation = new float[3];
// get handles to eventOuts from the VRML environment
ball_translation = (SFVec3f)getEventOut("ball_translation");
//temp init
ball_current_translation[1] = 4f;
ball_speed = 2.3f;
gravity = .11f;
//ball_ang = (35f*3.14f/180f);
//ball_speed_x = (float)Math.abs(Math.cos(ball_ang))*ball_speed;
//ball_speed_y = (float)Math.abs(Math.sin(ball_ang))*ball_speed;
}
public void processEvent(Event e) {
String EventName = e.getName();
if (EventName.equals("set_fraction"))
set_fraction((ConstSFFloat) e.getValue());
if (EventName.equals("get_angle"))
set_angle((ConstSFRotation) e.getValue());
if (EventName.equals("start"))
start_ball((ConstSFBool) e.getValue());
}
private void set_angle (ConstSFRotation new_angle) {
print ("getting angle for ball");
float total_angle[] = new float[4];
new_angle.getValue(total_angle);
//ball_ang = total_angle[3];
}
private void start_ball (ConstSFBool ball_starting) {
ball_thrown = true;
ball_ang = 3.14f/4f;
ball_speed_x = (float)Math.abs(Math.cos(ball_ang))*ball_speed;
ball_speed_y = (float)Math.abs(Math.sin(ball_ang))*ball_speed;
ball_ang = 3.14f/4f;
//ball_thrown = ball_starting.getValue();
}
private void move_ball() {
//see if the ball has hit the ground
if (ball_current_translation[1] <= 0) {
//print ("ball is on the ground");
ball_current_translation[1] = 0;
ball_speed_y = 0;
ball_speed_x = 0;
} else {
//ball is still moving
//print ("ball should be moving");
ball_speed_y = ball_speed_y - gravity;
ball_current_translation[1] = ball_current_translation[1] + ball_speed_y;
ball_current_translation[0] = ball_current_translation[0] - ball_speed_x;
}
ball_translation.setValue(ball_current_translation);
}
private void set_fraction(ConstSFFloat frac) {
if (ball_thrown) move_ball();
}
//helper
// print
private void print(String s) {
System.out.println(s);
}
private int nextPos(int x, int y) {
while (x > y) {
x = x - y;
}
while (x < 0) {
x = x + y;
}
return x;
}
}