Results 1 to 11 of 11
- 08-03-2011, 10:37 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Collision Detection, a little bit buggy.
I've decided to try to learn the fundamentals of making a game. So, I started off by making a background and a character that moves. But then I wanted it so that when the character tries to go out of the frame, it's not allowed to. For the movement code, I used a tutorial that I modified to my needs because I didn't even know what classes from the Java library to use. But then I decided to just make my collision detection from scratch. But it's a little bit buggy. It won't detect and stop the character for a couple of milliseconds. Here's my code:
I don't know if this is enough of my code to find the source of the problem but that's why I offered the source code.Java Code:if(key == KeyEvent.VK_UP) { if(getUpLocked() == false) { if(getCharY() != 0 && getCharY() > 0) { yChange = -1; setDownLocked(false); //allows you to move if you haven't collided } else { yChange = 0; charY = 0; setUpLocked(true); //doesn't allow you to move if you collide } } }Last edited by iamsothecoolest; 08-03-2011 at 10:41 PM.
- 08-03-2011, 10:43 PM #2
What stops the character?It won't detect and stop the character for a couple of milliseconds
Do you mean the x,y position continues to change and the paint method continues to draw the character after the collision was detected. What is the disconnect between the detection and the stopping?
Is there any relationship between when the user presses a key and when the collision is detected? Can the character continue to move if the key is not pressed?
Without seeing the code, we can only guess why the motion doesn't stop IMMEDIATELY when the flag is set.Last edited by Norm; 08-03-2011 at 11:47 PM.
- 08-03-2011, 10:52 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
The easiest way to see what I'm talking about would be to compile the code in the attachment
- 08-03-2011, 10:54 PM #4
Have you looked at the points I asked about?
- 08-03-2011, 11:15 PM #5
Add some printlns to show the values of x and y as the character moves around. Look at what the values are when it goes out of the frame. Compare those with where the edges of the frame are.
- 08-03-2011, 11:27 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Oh, I actually already had the showing coordinates idea implemented
. But the coordinates where it goes out of the frame varies each time with the furthest being around 100 pixels out of the frame.
EDIT: Like I said, the easiest way to see would be to compile the program in the .zip file I attached.
- 08-03-2011, 11:32 PM #7
You need to do some more debugging:
Add some printlns to your code to show the x,y values of the character each time it is drawn. Add printlns where the x,y position of the character changes. Add printlns where the collision is detected. See if it continues making changes to the x,y after the collision is detected.
- 08-03-2011, 11:45 PM #8
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Unrelated to the question, but this is redundant:
If the second condition is true, the first is going to be true as well, so there's no need to test for both.Java Code:if(getCharY() != 0 && getCharY() > 0) {
- 08-03-2011, 11:47 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
- 08-04-2011, 12:01 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Have you tried compiling it yet?
- 08-04-2011, 12:03 AM #11
Similar Threads
-
Java3D collision detection
By abbeywell in forum Java GamingReplies: 5Last Post: 04-27-2011, 02:33 PM -
Really Need help with some collision detection
By Harwad in forum New To JavaReplies: 1Last Post: 01-23-2011, 12:38 AM -
Collision Detection
By dotabyss in forum Java GamingReplies: 0Last Post: 03-14-2010, 06:13 PM -
Collision Detection (Game)
By mscwd in forum Sun Java Wireless ToolkitReplies: 0Last Post: 01-28-2008, 08:34 PM -
Two Problems Rotating and collision detection help
By jaferris in forum Java AppletsReplies: 2Last Post: 01-07-2008, 11:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks