Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Solved libGDX weird player moving away from camera! + collision dont work  (Read 2393 times)
0 Members and 1 Guest are viewing this topic.
Offline VIrtueeL
« Posted 2015-09-30 12:12:49 »

here is video showing you + collisions dont work

<a href="http://www.youtube.com/v/hZs_OJbPi6Q?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/hZs_OJbPi6Q?version=3&amp;hl=en_US&amp;start=</a>

the player box moves down on screen when its suppose to stay in the middle O.o never seen it before

My Tick Method
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
   public void tick(){
     
      float moveAmountu = (float) (jumpingSpeed * fixDt);
      float moveAmountd = (float) (fallingSpeed * fixDt);
      float moveAmountl = (float) (speedLeft * fixDt);
      float moveAmountr = (float) (speedRight * fixDt);
     
     
      if(Gdx.input.isKeyJustPressed(Keys.SPACE) && !toggleJump && !jumping){
         jumping = true;
         toggleJump = true;
         falling = false;
      }
     
      if(falling){
         fallPlayer(moveAmountd);  
      }
      if(jumping){
         jumping(moveAmountu);
      }
     
      camera.position.x = pos.x;
      camera.position.y = pos.y;
      camera.update();
   }



Jump and Falling Code where i check Collision
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
   public void jumping(float speed) {
      if(!Collision.CollisionPlayerBlock(
           
            new Point((int) (pos.x) ,
                    (int) (pos.y - speed)),
                   
            new Point((int) (pos.x + width) ,
                    (int) (pos.y - speed))  )){
         
         
         if(jumpingSpeed > 0){
            jumpingSpeed -= .09;
         }
         if(jumpingSpeed <= 0){
            falling = true;
            jumping = false;
            jumpingSpeed = defaultjumpingSpeed;
         }
         
         pos.y+=speed;
         
      }else{
         jumpingSpeed = defaultjumpingSpeed;
         falling = true;
         jumping = false;

      }
   }
   
   public void fallPlayer(float speed) {
      if(!Collision.CollisionPlayerBlock(
           
            new Point((int) (pos.x ) ,
                    (int) (pos.y + height + speed)),
                   
            new Point((int) (pos.x + width) ,
                    (int) (pos.y + height + speed)) )){
   
         
         
         if(fallingSpeed != maxSpeed*16){
            fallingSpeed += .1;
         }
         if(fallingSpeed >= maxSpeed*16){
            fallingSpeed = maxSpeed*16;
         }
         
         pos.y-=speed;

      }else{
     
         toggleJump = false;
         fallingSpeed = 0;
      }
   }



My Collision Class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   public static boolean CollisionPlayerBlock(Point p1, Point p2){
     
      for(int x = 0; x < 50;x++){
         for(int y = 0; y < 50;y++){
            Block[][] blocks = World.getPlayer().getWorld().getLoadedChunkBlocks();
           
            if(blocks[x][y].isSolid()){
               if(blocks[x][y].contains(p1) || blocks[x][y].contains(p1)){
                  return true;
               }
            }
           
            System.out.println(blocks.length+"");
         }
      }
      return false;
     
     
   }



My Contains Method <-- might be the one that fails me Sad
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   public boolean contains(Point point) {

      double px = point.getX();
      double py = point.getY();
     
      if(px < blockPos.x){
         if(px > blockPos.x){
            if(py < blockPos.y){
               if(py > blockPos.y){
                  System.out.println("TRUE");
                  return true;
               }
            }
         }
      }
     
      return false;
   }


My Whole Player Class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
public class Player implements InputProcessor {

   private Vector2 pos = new Vector2(50*50, 100*80);
   private Vector2 vol = new Vector2();
   
   
   private World world;
   private OrthographicCamera camera;
   private int width = 16, height = 32;
   
   public float fixDt = 60f/60F;
   public static float maxSpeed = 2.9F;
   
   public boolean falling,isInAir,jumping,isAlive,toggleJump,moving, space, running = false;
   
   public float speedLeft = 0;
   public float speedRight = 0;
   public float fallingSpeed = 3;
   public float defaultjumpingSpeed = 3.56f;
   public float jumpingSpeed = defaultjumpingSpeed;
   
   public Player(OrthographicCamera camera, World world) {
      this.camera = camera;
      this.world = world;
   }
   
   
   public void draw(SpriteBatch batch){
     
   }
   
   public void tick(){
     
      float moveAmountu = (float) (jumpingSpeed * fixDt);
      float moveAmountd = (float) (fallingSpeed * fixDt);
      float moveAmountl = (float) (speedLeft * fixDt);
      float moveAmountr = (float) (speedRight * fixDt);
     
     
      if(Gdx.input.isKeyJustPressed(Keys.SPACE) && !toggleJump && !jumping){
         jumping = true;
         toggleJump = true;
         falling = false;
      }
     
      if(falling){
         fallPlayer(moveAmountd);  
      }
      if(jumping){
         jumping(moveAmountu);
      }
     
      camera.position.x = pos.x;
      camera.position.y = pos.y;
      camera.update();
   }
   
   
   public void jumping(float speed) {
      if(!Collision.CollisionPlayerBlock(
           
            new Point((int) (pos.x) ,
                    (int) (pos.y - speed)),
                   
            new Point((int) (pos.x + width) ,
                    (int) (pos.y - speed))  )){
         
         
         if(jumpingSpeed > 0){
            jumpingSpeed -= .09;
         }
         if(jumpingSpeed <= 0){
            falling = true;
            jumping = false;
            jumpingSpeed = defaultjumpingSpeed;
         }
         
         pos.y+=speed;
         
      }else{
         jumpingSpeed = defaultjumpingSpeed;
         falling = true;
         jumping = false;

      }
   }
   
   public void fallPlayer(float speed) {
      if(!Collision.CollisionPlayerBlock(
           
            new Point((int) (pos.x ) ,
                    (int) (pos.y + height + speed)),
                   
            new Point((int) (pos.x + width) ,
                    (int) (pos.y + height + speed)) )){
   
         
         
         if(fallingSpeed != maxSpeed*16){
            fallingSpeed += .1;
         }
         if(fallingSpeed >= maxSpeed*16){
            fallingSpeed = maxSpeed*16;
         }
         
         pos.y-=speed;

      }else{
     
         toggleJump = false;
         fallingSpeed = 0;
      }
   }
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   @Override
   public boolean keyDown(int arg0) {
      return false;
   }

   @Override
   public boolean keyTyped(char arg0) {
      return false;
   }

   @Override
   public boolean keyUp(int arg0) {
      return false;
   }

   @Override
   public boolean mouseMoved(int arg0, int arg1) {
      return false;
   }

   @Override
   public boolean scrolled(int scrolls) {
     
      if(scrolls < 1){
         camera.zoom -= 0.1f;
         camera.update();
      }else{
         camera.zoom += 0.1f;
         camera.update();
      }
     
      return false;
   }

   @Override
   public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
      return false;
   }

   @Override
   public boolean touchDragged(int arg0, int arg1, int arg2) {
      return false;
   }

   @Override
   public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
      return false;
   }


   public void drawShapes(ShapeRenderer shapeR) {
      shapeR.rect(pos.x , pos.y, width, height);
   }


   public World getWorld() {
      return world;
   }

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline hwinwuzhere
« Reply #1 - Posted 2015-09-30 13:11:53 »

The video is private, perhaps you should change that might you want us to see it Wink

There are two kinds of people in this world: Those who can extrapolate from incomplete data,
Offline VIrtueeL
« Reply #2 - Posted 2015-09-30 13:17:22 »

The video is private, perhaps you should change that might you want us to see it Wink

lol sy xD there you go

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline hwinwuzhere
« Reply #3 - Posted 2015-09-30 13:22:25 »

When do you call the fallPlayer method?

There are two kinds of people in this world: Those who can extrapolate from incomplete data,
Offline VIrtueeL
« Reply #4 - Posted 2015-09-30 13:23:13 »

When do you call the fallPlayer method?

after jumping i set falling = true and falling is always waiting to be true

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline hwinwuzhere
« Reply #5 - Posted 2015-09-30 13:27:53 »

1  
2  
3  
if(blocks[x][y].contains(p1) || blocks[x][y].contains(p1)){
    return true;
}


Might it have something to do with this? You're calling
1  
blocks[x][y].contains(p1)
twice instead of calling p1 first and then p2 like this:

1  
2  
3  
if(blocks[x][y].contains(p1) || blocks[x][y].contains(p2)){ <-- HERE :)
    return true;
}


EDIT: You're calling this in the Collision class. Sorry for not mentioning.

There are two kinds of people in this world: Those who can extrapolate from incomplete data,
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (108 views)
2017-09-30 02:57:34

theagentd (135 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (240 views)
2017-08-02 12:19:43

cybrmynd (238 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!