123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- class Character
- {
- private PVector position;
- private PVector lookAt;
- private float charHeight = 2.0f;
- private float yaw;
- private float pitch;
- private float speed = 0.05f;
- private float jumpHeight = 0.05f;
- private PVector velocity;
- private PVector acceleration;
- private boolean isFalling = false;
- public Character() {
- this.position = new PVector();
- this.lookAt = new PVector();
- yaw = 0;
- pitch = 0;
- velocity = new PVector(0, 0, 0);
- acceleration = new PVector(0, 0.09, 0);
- }
- public void updateCamera() {
- velocity.add(acceleration);
- position.add(velocity);
- if (position.y >= -charHeight) {
- position.y = -charHeight;
- velocity.x = 0;
- velocity.y = 0;
- velocity.z = 0;
- isFalling = false;
- }
- lookAt.x = (float)(1 * Math.cos(Math.toRadians(yaw)) + position.x);
- lookAt.y = (float)(1 * Math.tan(Math.toRadians(pitch)) + position.y);
- lookAt.z = (float)(1 * Math.sin(Math.toRadians(yaw)) + position.z);
- camera(position.x, position.y, position.z,
- lookAt.x, lookAt.y, lookAt.z,
- 0, 1, 0);
- }
- public void move(float delta) {
-
- if (input['W']) {
- walkForward(speed * delta);
- } else if (input['S']) {
- walkBackwards(speed * delta);
- }
- if (input['A']) {
- strafeLeft(speed * delta);
- } else if (input['D']) {
- strafeRight(speed * delta);
- }
- if (input[' ']) {
- jump(jumpHeight * delta);
- }
- }
- public void setPosition(float x, float y, float z) {
- this.position.x = x;
- this.position.y = y;
- this.position.z = z;
- }
- public void walkForward(float distance)
- {
- position.x += distance * (float)Math.cos(Math.toRadians(yaw));
- position.z += distance * (float)Math.sin(Math.toRadians(yaw));
- }
- public void walkBackwards(float distance)
- {
- position.x -= distance * (float)Math.cos(Math.toRadians(yaw));
- position.z -= distance * (float)Math.sin(Math.toRadians(yaw));
- }
- public void strafeLeft(float distance)
- {
- position.x += distance * (float)Math.cos(Math.toRadians(yaw-90));
- position.z += distance * (float)Math.sin(Math.toRadians(yaw-90));
- }
- public void strafeRight(float distance)
- {
- position.x += distance * (float)Math.cos(Math.toRadians(yaw+90));
- position.z += distance * (float)Math.sin(Math.toRadians(yaw+90));
- }
- public void jump(float distance)
- {
- if (!isFalling) {
- velocity.y -= distance;
- isFalling = true;
- }
- }
- public void look(float screenX, float screenY) {
- yaw = 180 * (screenX - (width / 2)) / (width / 2);
- pitch = 90 * (screenY - (height / 2)) / (height / 2);
- println(yaw);
- println(pitch);
- }
- }
|