lwjgl converting mouseLook to keys - java

Hey guy's I'm working on a 3D project using lwjgl. At the moment the players camera view is moved by the mouse. Movement is done using the arrow keys & i'd like to convert the mouse view to w,a,s,d so you no longer have to use the mouse.
Why am I doing this? I've got an interface system that I can't use because the application currently grabs the mouse to move the view, so the player can not interact with the interfaces.
What I've got:
public void cameraView() {
if (Mouse.isGrabbed()) {
float mouseDX = Mouse.getDX() * 0.8f * 0.16f;
float mouseDY = Mouse.getDY() * 0.8f * 0.16f;
if (rotation.y + mouseDX >= 360) {
rotation.y = rotation.y + mouseDX - 360;
} else if (rotation.y + mouseDX < 0) {
rotation.y = 360 - rotation.y + mouseDX;
} else {
rotation.y += mouseDX;
}
if (rotation.x - mouseDY >= -89 && rotation.x - mouseDY <= 89) {
rotation.x += -mouseDY;
} else if (rotation.x - mouseDY < -89) {
rotation.x = -89;
} else if (rotation.x - mouseDY > 89) {
rotation.x = 89;
}
}
}
If anyone can help me out that'd be awesome. Thanks in advance!

if(Keyboard.isKeyDown(Keyboard.KEY_A)) {rotation.y -= 1;}
if(Keyboard.isKeyDown(Keyboard.KEY_D)) {rotation.y += 1;}
This should do the same thing as Mouse.getDX()
Alternatively, you could use:
while(Keyboard.next()) {
if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
if(Keyboard.getEventKeyState()) {
Mouse.setGrabbed(!Mouse.isGrabbed());
Mouse.setCursorPosition(Display.getWidth()/2, Display.getHeight()/2);
}}}
if(Mouse.isGrabbed()) {
//Use mouse for moving
} else {
//Use mouse for interface
}

Related

Java libgdx: limiting circular velocity (swinging entity)

I am currently working on a 2D side scroller and have implemented the techniques use in this article for a grapple hook, and it works really well. My problem is I want my player to be able to swing around the rope a little bit to gain a bit of momentum, but currently I can't stop the player from moving all the way up to 90 degrees either side. What techniques can be applied to force this limit?
I have tried using a separate player speed for swinging but this only slows the process down I can still swing up to 90 deg each side.
Here's my update function in the player
public void update(float dt){
//handle friction and air resistance
if(dx !=0){
if(touchingGround) {
// apply friction
if (dx > 0) {
dx -= retardation;
} else {
dx += retardation;
}
} else {
//applied air resistance
if (dx > 0) {
dx -= airResistance;
} else {
dx += airResistance;
}
}
}
// handle gravity
dy -= Constants.GRAVITY * dt;
if(dy < -terminalVelocity){
dy = -terminalVelocity;
}
/*
Handle Player movement
*/
if(right){
if(dx <= maxSpeed){
dx += acceleration;
}
dx = maxSpeed;
}
if(left){
if(dx <= -maxSpeed){
dx -= acceleration;
}
dx = -maxSpeed;
}
if(isGrappling){
//If we collide with something we need to stop grappling
if(hasCollided){
isGrappling = false;
} else {
// This algorithm from here:
// http://gamedev.stackexchange.com/questions/61596/player-rope-swing
float currentD = (float) Math.sqrt(((grappleX - x) * (grappleX - x)) + ((grappleY - y) * (grappleY - y)));
float prevX = getX(), prevY = getY();
if (currentD > grappleRadius) {
Vector2 hookPos = new Vector2(grappleX, grappleY);
Vector2 testPos = (new Vector2(x, y).sub(hookPos)).nor();
y = (hookPos.y + testPos.y * grappleRadius);
x = (hookPos.x + testPos.x * grappleRadius);
// s = d / t
dx += (x - prevX) / dt;
dy += (y - prevY) / dt;
}
}
}
/*
Collision Detection, handle last always!
*/
float oldX = getX(), oldY = getY();
boolean collisionX = false, collisionY = false;
// move on x
x += dx * dt;
// calculate the increment for step in #collidesLeft() and #collidesRight()
increment = collisionLayer.getTileWidth();
increment = getWidth() < increment ? getWidth() / 2 : increment / 2;
if(dx < 0) // going left
collisionX = collidesLeft();
else if(dx > 0) // going right
collisionX = collidesRight();
// react to x collision
if(collisionX) {
setX(oldX);
dx = 0;
}
// move on y
y += dy * dt;
// calculate the increment for step in #collidesBottom() and #collidesTop()
increment = collisionLayer.getTileHeight();
increment = getHeight() < increment ? getHeight() / 2 : increment / 2;
if(dy < 0) {
touchingGround = collisionY = collidesBottom();
// we can only jump 2 times before we have to touch the floor again
if(collisionY){
numberOfJumps = 2;
}
} else if(dy > 0) {
collisionY = collidesTop();
}
// react to y collision
if(collisionY) {
setY(oldY);
dy = 0;
}
hasCollided = collisionX || collisionY;
}
As I am not using any physics engine I chose to just emulate the physics by limiting the angle at which the player can apply force to the swing.
// check if angle permits movement
if(grappleAngle < Math.PI/9 && grappleAngle > -Math.PI/9) {
// handle momentum gaining on rope
if (right) {
dx += swingAcceleration * dt;
}
if (left) {
dx -= swingAcceleration * dt;
}
}

Java LibGDX Multi Touch Issue

for (byte i = 0; i < 20; i++) {maxDistance = 10 * Gdx.graphics.getDeltaTime();
if (Gdx.input.isTouched(i) && Gdx.input.getY()<= 400) {
player1TouchPosition.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
camera.unproject(player1TouchPosition);
}
player1Tmp.set(player1TouchPosition.x, player1TouchPosition.y).sub(player1Rectangle.x, player1Rectangle.y);
if (player1Tmp.len() <= maxDistance) {
player1Rectangle.x = player1TouchPosition.x;
player1Rectangle.y = player1TouchPosition.y;
} else {
player1Tmp.nor().scl(maxDistance);
player1Rectangle.x += player1Tmp.x;
player1Rectangle.y += player1Tmp.y;
}
if (Gdx.input.isTouched(i) && Gdx.input.getY() >= 401) {
player2TouchPosition.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
camera.unproject(player2TouchPosition);
}
player2Tmp.set(player2TouchPosition.x, player2TouchPosition.y).sub(player2Rectangle.x, player2Rectangle.y);
if (player2Tmp.len() <= maxDistance) {
player2Rectangle.x = player2TouchPosition.x;
player2Rectangle.y = player2TouchPosition.y;
} else {
player2Tmp.nor().scl(maxDistance);
player2Rectangle.x += player2Tmp.x;
player2Rectangle.y += player2Tmp.y;
}
}
Hello I'm using this code to move to touch position. But I need multi touch. It's not working. When I add player2, it doesn't work. I didn't understand how is multi touch. How can I fix it?
Why do you not use InputProcessor?
One example of one method from the interface
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(pointer =< 2){
touches.get(pointer).touchX = screenX;
touches.get(pointer).touchY = screenY;
touches.get(pointer).touched = true;
}
return true;
}
In the above example you can use maximum 2 touches.
Actually 1 pointer is 1 touch.
Documentation
screenX and screenY are the touch position. Be aware you have to scale this position compared to your orthographic camera. A pointer is the pointer for the event.
If you create the InputProcessor you can launch it with
Gdx.input.setInputProcessor(/*Your class*/);
EDIT:
Example from the comments:
for (Button button : /*ArrayList*/{
if (positionX >= button1.getX() && positionX <= button1.getX() + button1.getWidth() &&
positionY >= button1.getY() && positionY <= button1.getY() + button1.getHeight()){
//Update the position from the specific button
}
You use this code inside the method from the interface touchDragged().

How to make character continue moving after releasing key, in Slick2D?

I am new to Slick2D and I am trying movements with my character. I can make it move smoothly while holding movement key, but I also want the character to finish its movement so it stops exactly on the next tile (I have simple map with tiles 32x32). And that is a problem for me, because it moves to the next tiles, but it teleports there - the movement is instant and I want my character to just continue with its movement with the same speed.
I tried for example something like this in my update() method:
else if (input.isKeyPressed(Input.KEY_D))
{
characterAnimation = characterAnimationRight;
characterAnimation.update(delta);
xCoord = (int) xCoord;
while (xCoord%32 != 0)
{
xCoord += 1;
characterAnimation.update(delta);
if (xCoord > Window.WIDTH - 32)
{
xCoord = Window.WIDTH - 32;
}
}
}
but I canĀ“t make it work.
Why don't you try to have a "xSpeed" and "ySpeed" and set those values according to where the 'characterAnimation' is set to and to whether you are on an exact tile or not?
Something like:
else if (input.isKeyPressed(Input.KEY_D)) {
characterAnimation = characterAnimationRight;
}
// ...
if (characterAnimation == characterAnimationRight){
xSpeed = 1;
}
else if (characterAnimation == characterAnimationLeft){
xSpeed = -1;
}
xCoord += xSpeed;
characterAnimation.update(delta);
if(xCoord % 32 == 0) {
xSpeed = 0;
}
// ...
I do not actually know how your code (or slick2d) works, so I assumed the xCoord value is magically taken into account when characterAnimation.update(delta) is called. Otherwise do whatever you need to update your character's position according to xCoord.
The solution is not to calculate xCoord in while() in the update() method. The reason is that it is calculated in single run of update() method and after that the render() method is called to render the character. That means character is rendered after that while() and it teleports.
Here is my solution:
#Override
public void update(GameContainer gc, StateBasedGame s, int delta)
throws SlickException {
// .......
if (moving)
{
if (movingDirection == DIR_RIGHT)
{
if (xCoord >= targetCoord)
{
xCoord = targetCoord;
moving = false;
}
else
{
xCoord += delta * 0.1f;
characterAnimation.update(delta);
if (xCoord > Window.WIDTH - 32)
{
xCoord = Window.WIDTH - 32;
}
}
}
else if (movingDirection == DIR_LEFT)
{
if (xCoord <= targetCoord)
{
xCoord = targetCoord;
moving = false;
}
else
{
xCoord -= delta * 0.1f;
characterAnimation.update(delta);
if (xCoord < 0)
{
xCoord = 0;
}
}
}
else if (movingDirection == DIR_UP)
{
if (yCoord <= targetCoord)
{
yCoord = targetCoord;
moving = false;
}
else
{
yCoord -= delta * 0.1f;
characterAnimation.update(delta);
if (yCoord < 0)
{
yCoord = 0;
}
}
}
else if (movingDirection == DIR_DOWN)
{
if (yCoord >= targetCoord)
{
yCoord = targetCoord;
moving = false;
}
else
{
yCoord += delta * 0.1f;
characterAnimation.update(delta);
if (yCoord > Window.WIDTH - 32)
{
yCoord = Window.WIDTH - 32;
}
}
}
}
}
Variable moving is set true after pressing movement key. Now after every call of render() the character is moved a little bit in the update() and rendered at this new position until it is exactly on the tile.

checking if 2 rectangles WILL overlap

I have been trying to do player intersection with a small project i'm doing, and I can't seem to make it work. I got the Intersection to work with the player and the wall, but it's very buggy, by buggy I mean, it draws the player in the wall then moves him back instantly. (Check Gyazo for gif of this). I'm pretty sure the problem is that it only checks if the player is in the wall, and not WILL be in the wall, but I can't seem to figure out how to check this. This is what I have so far:
public void intersectsBox2(Rectangle r, Rectangle r2) {
P1 = new Point((int) r.getMinX(), (int) r.getMinY());
P2 = new Point((int) r.getMaxX(), (int) r.getMaxY());
P3 = new Point((int) r2.getMinX(), (int) r2.getMinY());
P4 = new Point((int) r2.getMaxX(), (int) r2.getMaxY());
if ((P2.y < P3.y || P1.y > P4.y || P2.x < P3.x || P1.x > P4.x)
&& !intersectsBox(playerRectangle(), noWalls[0])) {
isInsideWalls = true;
}
}
// Gets the players rectangle
public Rectangle playerRectangle() {
return new Rectangle(9 + dx, 23 + dy, 54, 90);
}
This is to make the player Move:
public void playerMovement() {
if (isInsideWalls) {
System.out.println("YOU ARE IN THE BOX!");
if (animation == down) {
dy -= moveSpeed;
isInsideWalls = false;
} else if (animation == up) {
dy += moveSpeed;
isInsideWalls = false;
} else if (animation == left) {
dx += moveSpeed;
isInsideWalls = false;
} else if (animation == right) {
dx -= moveSpeed;
isInsideWalls = false;
}
} else {
// Moves the player
if (moving == downMove) {
dy += moveSpeed;
moving = 0;
} else if (moving == upMove) {
dy -= moveSpeed;
moving = 0;
} else if (moving == leftMove) {
dx -= moveSpeed;
moving = 0;
} else if (moving == rightMove) {
dx += moveSpeed;
moving = 0;
}
}
This is to check for the intersection:
//Checks for intersection
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
intersectsBox2(walls[i][j], playerRectangle());
}
}
Not really sure if this is needed but heres the full Game.java if you need to see this: http://pastebin.com/GrDy689d
Also here is the Gif of the problem: http://i.gyazo.com/1f31f739897af78f81e61cf22ac772db.mp4
P.S: It's on purpose I can only go into 1 box at the moment for testing purposes.
You could move the player, then check to see if it is in a wall and, if it is, undo the move (or better, calculate a new location as the result of a move, check it, and if it is good, only then move the player there). Note that this assumes a single move can't put you all the way on the other side of a wall, but then it looks like your code does, too.

Collision bug in a 2D platform game

I'm new to java, and game programming and I'm starting my first big project which is a 2D platform puzzle game.
This is my player movement code
if (speedX > 0 && centerX <= 400){
centerX += speedX;
}
if (speedX < 0 && centerX >= 400){
centerX += speedX;
}
if (speedX > 0 && centerX >= 400){
bg1.setSpeedX(-MOVESPEED);
bg2.setSpeedX(-MOVESPEED);
}
if (speedX < 0 && centerX <= 400){
bg1.setSpeedX(MOVESPEED);
bg2.setSpeedX(MOVESPEED);
}
if (speedX == 0){
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
if(movingRight == true && movingLeft == true ){
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
// Handles Jumping
if (jumped == true) {
speedY += 1;
}
// Prevents going beyond X coordinate of 0
if (centerX + speedX <= 60) {
centerX = 61;
}
rect.setRect(centerX - 47, centerY - 65, 32, 87);
centerY += speedY;
}
public void moveRight() {
speedX = MOVESPEED;
}
public void moveLeft() {
speedX = -MOVESPEED;
}
public void stopRight() {
movingRight = false;
stop();
}
public void stopLeft() {
movingLeft = false;
stop();
}
private void stop() {
if (movingRight == false && movingLeft == false) {
speedX = 0;
}
if (movingRight == false && movingLeft == true) {
moveLeft();
}
if (movingRight == true && movingLeft == false) {
moveRight();
}
}
public void jump() {
if (jumped == false) {
speedY = JUMPSPEED;
jumped = true;
}
}
and this is the collision code
public void checkCollision(Rectangle rect){
if (rect.intersects(r)){
if(Player.movingRight){
Player.centerX = tileX + 11;
Player.speedX =0;
}
if(Player.movingLeft){
Player.centerX = tileX + 89;
Player.speedX = 0;
}
if(Player.speedY > 0){
Player.centerY = tileY - 25;
Player.speedY = 0;
Player.jumped = false;
}
}
}
There are two problems.The first one is that if I press one of the movement keys when landing the character "teleports" to the right or left.
I know this happens because I programmed it that if the character intersects with the ground while movingRight or movingLeft are true he moves right or left.(I made it this way so the horizonal collision will work) and I cant think of any other way to do it or how to fix it.
The second problem is that if the character moves of a platfrom he does not fall down.
I tryed to fix it by adding to the collision method
else{
speedY += 1;
}
But it made the character disappear for some reason.
Thanks a lot!
This code was originally written in C++ for a 3D platformer. I rewrote it, but there might be some bugs. I can draw a picture later if it's difficult to understand.
public void checkCollision(Rectangle rect){
if(player.intersects(rect)) {
//the rectangles intersect, time to move the player out of the block
if(rect.y+rect.height >= player.y && rect.y+rect.height-0.7f < player.y) { //if the player is at most 0.7 units (you should change this!) below top side
player.y = rect.y+rect.height; //set player to stand on top
speed.y = 0f; //stop the movement
onGround = true;
} else if(rect.y+rect.height > player.y && rect.y < player.y+player.height) { //if the playeer is on the side, but not below
float xEscMinus = (float)Math.abs((rect.x+rect.width)-player.x); //find the distance to the side
float xEscPlus = (float)Math.abs(rect.x-(player.x+player.width));
if(xEscMinus<xEscPlus) {
player.x = rect.x+rect.width;
} else {
player.x = rect.x-player.width;
}
}
}
}

Categories