We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,697 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

code is right but i don't understand how it's working

this code works fine. all it's doing is setting collision around enemy. and if player touches player from the right than player moves to the right. if player touches enemy from left than player moves to the left.

playerX = player x postion
playerY = player y postion
playerW = player width
playerH = player height
x = enemy x postion
y - enemy y postion
width - enemy width
height - enemy height
p.setX = set player x postion 
...



if(playerX + playerW >= x && playerX <= x + width)
        {
            if(playerY+playerH >= y && playerY <= y+height)
            {
                if (playerX <= x)  //player on left
                {
                    System.out.println(playerX +"--"+x);
                    p.setX(x - width);
                }
                else if(playerX >= x)//player on right
                {
                    p.setX(x + width);
                }
            }
        }

this code below i understand how its working

if(playerX + playerW >= x && playerX <= x + width)
        {
            if(playerY+playerH >= y && playerY <= y+height)
            {

but this code is like magic!
ok so if player x postion is on left of enemy x, than move player to right

  if (playerX <= x)  //player on left
p.setX(x - width);

but i dont get it. bc if want to to see if player is on left of enemy than i should take playerX+playerW so collsion point is on right of player head. for ex.

if (playerX+playerW <= x) 

  //thsi i understand but its not right why??

and next line shoud be enemy x - player width.

p.setX(x - playerW) //this i understand but its not right. why??

i was thinking if playerx+playerW will get me a point at right of player head. and
this will test if player right side of head is <= enemy x(which is left of enemy head)
if (playerX+playerW <= x) //this shoudl work.........

3
Contributors
2
Replies
11 Hours
Discussion Span
6 Months Ago
Last Updated
3
Views
hwoarang69
Posting Pro
569 posts since Feb 2012
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 7

well, how do you expect us to understand it? you want us to explain variables while we have no idea of what type they are...

also: saying - the code is right - just to be followed by // this I understand but it's not right...

either it's right, or it's not, it can't be both. can you be more complete and clear about what it is you're asking and providing?

stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23

It is not about the code, it is about the concept. OK, I'm going to try to explain it graphically...

/* 
 1.if(playerX + playerW >= x && playerX <= x + width) {
 2.  if(playerY+playerH >= y && playerY <= y+height) {
 3.    if (playerX <= x)  //player on left {
 4.      System.out.println(playerX +"--"+x);
 5.      p.setX(x - width);
 6.    }
 7.    else if(playerX >= x) { //player on right
 8.      p.setX(x + width);
 9.    }
10.  }
11.}

I'm going to talk about X only
pX = playerX
pW = playerW
 pX        pX+pW
  +---------+
  | playerX |
  +---------+
If it is collide...
 pX        pX+pW
  +---------+
  |   x+----|---+ x+enemy_width
  +----|----+---|
       +--------+
So if (pX<=x), the enemey is on the right hand side.
If you use if (playerX+playerW <= x), it does not guarantee the result.
Show below is the case when playerX+playerW>x, but it is still collide
  on the same side.
 pX             pX+pW
  +-----------+
  |   x+---+  |
  +----|---|--+
       +---+

      pX        pX+pW
       +---------+
  x+---|----+    |
   |   +----|----+
   +--------+
and if (pX>x), the enemy is on the left hand side
*/

When you deal with collision, you must compare the same reference point. In this case, you are using top-left position. You must not use different position to compare (top-right from player and top-left from enemy).

Hope this help.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0661 seconds using 2.76MB