Author Topic: Problem with controlling a box  (Read 2928 times)

Offline Creste

  • byte
  • *
  • Posts: 5
    • View Profile
Problem with controlling a box
« on: June 19, 2014, 10:13:12 pm »
Hey there,

I have kind of a weird problem. I have no idea, how to first person control a box...

I have a class structured like this:
Code: [Select]
class FirstPersonControler {
SimpleVector moving = new SimpleVector(0f, 0f, 0f);
SimpleVector facingangle = new SimpleVector(0f,0f,0f);

public void update() {
double nowtick = GameTicks.getSystemTick();

/* Unimportant logic having todo with fps view */

facingangle = c.getDirection();
}

public void strafeLeft() {
moving.add(new SimpleVector(-2f, 0f, 1-Math.abs(facingangle.z)));
}

public void strafeRight() {
moving.add(new SimpleVector(2f, 0f, (facingangle.x*2f)/facingangle.z));
}

public void forward() {
moving.add(new SimpleVector(facingangle.x, 0, facingangle.z));
}

public void backward() {
moving.add(new SimpleVector(-facingangle.x, 0, -facingangle.z));
}

public SimpleVector getMoveVector() {
return moving;
}
}
It controls the way, the box is moving, woth looking how the camera is turned. Then you should be able to control the box with "WASD" and turn around with the mouse, but that doesn't work well   :(
I need to find out, how to strafe left - if I try with that solution, it acts weird, going to front, back, side and any way that looks nicer then left. Same with right. I tried every math trick I know, scalar product, simple addition, ...
Forward and backwards work well, just the left and right thing don't.

Would be quite cool, if someone could help me. I am getting depressed by that...

Hope I don't left any information...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Problem with controlling a box
« Reply #1 on: June 19, 2014, 10:53:36 pm »
You might want to look at the get?Axis()-methods of either Object3D or Camera (Whatever applies to your case). With those, you can get your strafing vectors pretty easily.

Offline Creste

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Problem with controlling a box
« Reply #2 on: June 20, 2014, 12:01:35 am »
That was a pretty fast answer, thanks ^^

I am confused; why do they return 3 coordinates?
Saying I want to know the degrees to the vector 0|0|1, so I know the degrees the camera is turned. Which vector do I need to use?

Sorry, if that question sounds stupid, but I got used to the checkCameraCollision and never thought of leaving that one.

Offline Creste

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Problem with controlling a box
« Reply #3 on: June 20, 2014, 06:43:08 am »
Okay, I'd like to explain me problem again ^^

We are moving in a 3d space, so we need only 3 coordinates to find out, how camera is facing (like in screenshot below).

With cam.getXAxis(), cam.getYAxis() and cam.getZAxis() we can read 3 coordinates each - what are these 9 coordinated for? What are they showing?

[attachment deleted by admin]

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Problem with controlling a box
« Reply #4 on: June 21, 2014, 12:58:15 pm »
I'm not sure I understood your question, but I think that you're trying to get the camera's position. If so, just call Camera.getPosition(). camera..getZAxis() is meant to get the forward axis of the camera (the method could be renamed Camera.getFront(), while Camera.getXAxis() could be renamed Camera.getRight()). Is that what you meant?

Offline Creste

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Problem with controlling a box
« Reply #5 on: June 22, 2014, 12:23:44 pm »
Thanks fpr your reply.

I've been a bad explainer like my whole life ^^
I want to move an object in the world depending on the rotation if the camera.

I have this code right now:
Code: [Select]
public class FirstPersonControler {
private Camera c;

SimpleVector moving = new SimpleVector(0f, 0f, 0f);

private SimpleVector xangle = new SimpleVector(0f, 0f, 0f);
private SimpleVector zangle = new SimpleVector(0f,0f,0f);

public FirstPersonControler(Camera c) {
this.c = c;
lasttick = GameTicks.getSystemTick();
}

public void update() {
double nowtick = GameTicks.getSystemTick();

moving = new SimpleVector(0f, 0f, 0f);

/* Code for turning the camera with the mouse */

xangle = c.getXAxis();
zangle = c.getZAxis();

tickcount = nowtick - lasttick;

lasttick = nowtick;
}

public void strafeLeft() {
moving.add(new SimpleVector(xangle.x * (-1), 0, xangle.z * (-1)));
}

public void strafeRight() {
moving.add(new SimpleVector(xangle.x, 0, xangle.z));
}

public void forward() {
moving.add(new SimpleVector(zangle.x * (-1), 0, zangle.z * (-1)));
}

public void backward() {
moving.add(new SimpleVector(zangle.x, 0, zangle.z));
}

public void jump() {
gravity = 1.2f;
}

public void forceMove(SimpleVector sv) {
this.forcedmoves.add(sv);
}

public SimpleVector getMoveVector() {
SimpleVector ret = new SimpleVector(moving.x, moving.y, moving.z);
if(ret.length() > 0) ret.scalarMul((1f/ret.length())*(float)(tickcount/2f));
return ret;
}
}
Every tick (~5ms) I check for the pressed key
w = FirstPersonControler.forward()
a = FirstPersonControler.strafeLeft()
s = FirstPersonControler.backward()
d = FirstPersonControler.strafeRight()
Then I move an object with the Vector FirstPersonControler.getMoveVector() and put the camera right on top of it.

Using the code above make the camera move weird; when pressing "a", it doesn't move to how it should.
I want to calculate the vector I need to translate, if I want to move the camera to the left (with calculating its rotation)

Offline Creste

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Problem with controlling a box
« Reply #6 on: June 22, 2014, 12:44:55 pm »
Okay, I am such an idiot. Did some things, everyone does when he has no idea what went wrong - change a few parameters and hope it works. It shure did :D

For everyone having similar problems; the code must be like
Code: [Select]
public void strafeLeft() {
moving.add(new SimpleVector(xangle.x * (-1), 0, xangle.z));
}

public void strafeRight() {
moving.add(new SimpleVector(xangle.x, 0, xangle.z * (-1)));
}

public void forward() {
moving.add(new SimpleVector(zangle.x * (-1), 0, zangle.z));
}

public void backward() {
moving.add(new SimpleVector(zangle.x, 0, zangle.z * (-1)));
}
Only one parameter needs to be reverted.

Thanks for helping me so much :)