Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 1604 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2013-02-24 02: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 02: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 Wizard


Medals: 69
Projects: 4


I always win!


« Reply #2 - Posted 2013-02-24 02: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
Offline cubemaster21
« Reply #3 - Posted 2013-02-24 02: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 03: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


Offline cubemaster21
« Reply #5 - Posted 2013-02-24 21: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 Devvie


Medals: 7



« Reply #6 - Posted 2013-02-24 23: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.
Offline cubemaster21
« Reply #7 - Posted 2013-02-24 23: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 Devvie


Medals: 7



« Reply #8 - Posted 2013-02-25 12: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 13: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
Offline cubemaster21
« Reply #10 - Posted 2013-02-25 22: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 Devvie


Medals: 7



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

Did you have any luck with my suggestion?
Offline cubemaster21
« Reply #12 - Posted 2013-02-26 01: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 07: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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (23 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (96 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (152 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!