Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  Movement code critique?  (Read 788 times)
0 Members and 1 Guest are viewing this topic.
Online cubemaster21

Senior Member


Medals: 4
Projects: 1



« Posted 2013-02-24 03:03:20 »

Can someone take a look at this code for me and make sure it all checks out OK? I'm getting random times where yCollision is equal to false when I'm standing on a surface.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   public void move(int delta){
      xCollision = false;
      yCollision = false;
      pastX = x;
      pastY = y;
      x = nextX;
      y = nextY;
      collisionBox = new CollisionBox(x,y, sprite.getWidth(),sprite.getHeight()).rotate(rot,getCenterPoint());
      velocity.y += (G_ACCELERATION * delta)/1000.0;
      nextX = x + (velocity.x * delta)/1000.0;
      if(doesCollideOnX( new CollisionBox(nextX, y, sprite.getWidth(),sprite.getHeight()).rotate(rot, getCenterPoint()))){
         nextX = x;
         velocity.x = 0.0;
         xCollision = true;
      }
      nextY = y + (velocity.y * delta)/1000.0;
      if(doesCollideOnY(new CollisionBox(nextX, nextY, sprite.getWidth(),sprite.getHeight()).rotate(rot, getCenterPoint()))){
         nextY = y;
         velocity.y = 0.0;
         yCollision = true;
      }

   }

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Rorkien
« Reply #1 - Posted 2013-02-24 03:26:40 »

What are the types on pastX/pastY/x/y?
Is the object trembling around?
And why is delta an int?  persecutioncomplex

My shallow knowledge tells me that something in line 9 and/or 16 is messing up your stew: If delta == 0, your Y position doesn't change at all, and no collision is found (since you are checking collisions with your actual position), hence the yCollision = false statement.
Offline appel

JGO Ninja


Medals: 35
Projects: 5


I always win!


« Reply #2 - Posted 2013-02-24 03:40:40 »

Should provide the methods: doesCollideOnY

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online cubemaster21

Senior Member


Medals: 4
Projects: 1



« Reply #3 - Posted 2013-02-24 03:46:10 »

anything related to positions is a double, the object isn't trembling, and I use an int because I feel like it's easier to work with.
I can't provide the doesCollideOnY method because this is part of an abstract class so it's different for everything that extends it.
@Rorkien It's supposed to run every 16 milliseconds. delta should always be 16. What do you suppose I do to fix this then?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Rorkien
« Reply #4 - Posted 2013-02-24 04:18:25 »

No idea.  Grin

Well, assuming your delta is always 16, your velocity.y should be constant whenever yCollision returns true.
Is yCollision changed somewhere else for this object? Or just when doesCollideOnY returns true?

I usually fill the method with println() to grab and compare all the possible data (mad debug skillz).
Try grabbing the nextY value right after line 16 and check if it's changing right along with yCollision


Online cubemaster21

Senior Member


Medals: 4
Projects: 1



« Reply #5 - Posted 2013-02-24 22:07:01 »

Well, I threw some println statements in there and every once in a while, the velocity will be a little bit less than usual. the only way that that makes any sense would be if delta is lower than 16, which doesn't really make any sense.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Axeman

Senior Member


Medals: 4



« Reply #6 - Posted 2013-02-25 00:49:50 »

My bet is that there's something wierd in doesCollideOnY(). I would start there and find out why it's returning false when it's not supposed to.
Online cubemaster21

Senior Member


Medals: 4
Projects: 1



« Reply #7 - Posted 2013-02-25 00:59:56 »

I usually just use standard Rectangle.intersects collisions in doesCollideOnY(), there's nothing wrong with that section.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Axeman

Senior Member


Medals: 4



« Reply #8 - Posted 2013-02-25 13:49:05 »

It could be that the lastposition push is further away than the gravitational pull in one cycle. That would explain why it´s only happening in the y-axis. How random is "yCollide == false"? Is it just once in a while or do the object sometimes get stuck in a false state?
Offline 65K
« Reply #9 - Posted 2013-02-25 14:02:06 »

Look for x-collisions with nextY as well.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online cubemaster21

Senior Member


Medals: 4
Projects: 1



« Reply #10 - Posted 2013-02-25 23:19:23 »

It's intermittent. I've never seen the object stuck in the false stage.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Axeman

Senior Member


Medals: 4



« Reply #11 - Posted 2013-02-25 23:38:58 »

Did you have any luck with my suggestion?
Online cubemaster21

Senior Member


Medals: 4
Projects: 1



« Reply #12 - Posted 2013-02-26 02:48:48 »

From what I can tell, this occurs whenever my deltaTime is NOT 16. I really have no idea what to do about this.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline 65K
« Reply #13 - Posted 2013-02-26 08:53:59 »

If the delta gets too large and the objects are moving fast, it could also happen that they are tunneling through surfaces.
A simple solution is to break down the delta and put the collision check into a loop to check the whole movement path.

Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (73 views)
2013-05-17 21:29:12

alaslipknot (85 views)
2013-05-16 21:24:48

gouessej (114 views)
2013-05-16 00:53:38

gouessej (109 views)
2013-05-16 00:17:58

theagentd (120 views)
2013-05-15 15:01:13

theagentd (109 views)
2013-05-15 15:00:54

StreetDoggy (154 views)
2013-05-14 15:56:26

kutucuk (176 views)
2013-05-12 17:10:36

kutucuk (173 views)
2013-05-12 15:36:09

UnluckyDevil (182 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.091 seconds with 21 queries.