Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualKhatharr

Posted 12 December 2012 - 06:07 PM

If you collide with a wall then rather than undoing that frame's motion you should probably place yourself at a position 'against' the wall.

Rather than stepping back by your velocity, detect the wall edge that you're colliding with and set yourself at the most extreme valid position:

[source lang="ruby"]#ruby flavored pseudocodedef playerMovingLeft(player, amount) player.box.x -= amount collidedObject = stage.testCollide(player.box) if collidedObject != nil #hit the left wall (since we're moving left) player.box.x = collidedObject.rightEdge #so set position to a valid one endend[/source]

As for your code, another way of writing exactly what you have there would be:

[source lang="cpp"]if(touches_wall(player.box)) { //don't know which direction(s) caused collision. player.box.x = player.prev_x; player.box.y = player.prev_y;}[/source]

Which makes it easier to see the problem. You say in your post that you already know the problem though. I don't understand what you're confused about then. If a single 'if' won't work then don't use a single 'if'.

Get some rest. I think it will come to you when you're freshened up a bit. If you code yourself into a corner taking a break can often simplify difficult problems.

#5Khatharr

Posted 12 December 2012 - 06:07 PM

If you collide with a wall then rather than undoing that frame's motion you should probably place yourself at a position 'against' the wall.

Rather than stepping back by your velocity, detect the wall edge that you're colliding with and set yourself at the most extreme valid position:

[sourcelang="ruby"]#ruby flavored pseudocode
def playerMovingLeft(player, amount)
player.box.x -= amount
collidedObject = stage.testCollide(player.box)
if collidedObject != nil #hit the left wall (since we're moving left)
player.box.x = collidedObject.rightEdge #so set position to a valid one
end
end
[/source]

As for your code, another way of writing exactly what you have there would be:

[source lang="cpp"]if(touches_wall(player.box)) { //don't know which direction(s) caused collision. player.box.x = player.prev_x; player.box.y = player.prev_y;}[/source]

Which makes it easier to see the problem. You say in your post that you already know the problem though. I don't understand what you're confused about then. If a single 'if' won't work then don't use a single 'if'.

Get some rest. I think it will come to you when you're freshened up a bit. If you code yourself into a corner taking a break can often simplify difficult problems.

#4Khatharr

Posted 12 December 2012 - 06:05 PM

If you collide with a wall then rather than undoing that frame's motion you should probably place yourself at a position 'against' the wall.

Rather than stepping back by your velocity, detect the wall edge that you're colliding with and set yourself at the most extreme valid position:

#ruby flavored pseudocode

def playerMovingLeft(player, amount)

  player.box.x -= amount

  collidedObject = stage.testCollide(player.box)

  if collidedObject != nil #hit the left wall (since we're moving left)

	player.box.x = collidedObject.rightEdge #so set position to a valid one

  end

end



As for your code, another way of writing exactly what you have there would be:

if(touches_wall(player.box)) { //don't know which direction(s) caused collision.

  player.box.x = player.prev_x;

  player.box.y = player.prev_y;

}


Which makes it easier to see the problem. You say in your post that you already know the problem though. I don't understand what you're confused about then. If a single 'if' won't work then don't use a single 'if'.

Get some rest. I think it will come to you when you're freshened up a bit. If you code yourself into a corner taking a break can often simplify difficult problems.

#3Khatharr

Posted 12 December 2012 - 06:03 PM

If you collide with a wall then rather than undoing that frame's motion you should probably place yourself at a position 'against' the wall.

Rather than stepping back by your velocity, detect the wall edge that you're colliding with and set yourself at the most extreme valid position:

#ruby flavored pseudocode

def playerMovingLeft(player, amount)

  player.box.x -= amount

  if player.box.x < 0 #hit the left wall (since we're moving left)

	player.box.x = 0 #so set position to a valid one

  end

end



As for your code, another way of writing exactly what you have there would be:

if(touches_wall(player.box)) { //don't know which direction(s) caused collision.

  player.box.x = player.prev_x;

  player.box.y = player.prev_y;

}


Which makes it easier to see the problem. You say in your post that you already know the problem though. I don't understand what you're confused about then. If a single 'if' won't work then don't use a single 'if'.

Get some rest. I think it will come to you when you're freshened up a bit. If you code yourself into a corner taking a break can often simplify difficult problems.

#2Khatharr

Posted 12 December 2012 - 05:59 PM

If you collide with a wall then rather than undoing that frame's motion you should probably place yourself at a position 'against' the wall.

Rather than stepping back by your velocity, detect the wall edge that you're colliding with and set yourself at the most extreme valid position:

#ruby flavored pseudocode
def playerMovingLeft(player, amount)
  player.box.x -= amount
  if player.box.x < 0 #hit the left wall (since we're moving left)
	player.box.x = 0 #so set position to a valid one
  end
end


As for your code, another way of writing exactly what you have there would be:

if(touches_wall(player.box)) { //in which direction are we touching the wall???
  player.box.x = player.prev_x;
  player.box.y = player.prev_y;
}


I think it may be easier to see your problem in that one. Posted Image

#1Khatharr

Posted 12 December 2012 - 05:58 PM

If you collide with a wall then rather than undoing that frame's motion you should probably place yourself at a position 'against' the wall.

Rather than stepping back by your velocity, detect the wall edge that you're colliding with and set yourself at the most extreme valid position:

#ruby flavored pseudocode
def playerMovingLeft(player, amount)
  player.box.x -= amount
  if player.box.x < 0 #hit the left wall (since we're moving left)
    player.box.x = 0 #so set position to a valid one
  end
end


As for your code, another way of writing exactly what you have there would be:

if([b]touches_wall(player.box)[/b]) {
  player.box.x = player.prev_x;
  player.box.y = player.prev_y;
}


I think it may be easier to see your problem in that one. Posted Image

PARTNERS