This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
captsmitty77's profile picture

Help with game collision

Started by captsmitty77, 25 August 2014 - 11:03 PM
captsmitty77 #1
Posted 26 August 2014 - 01:03 AM
I'm working on a game for cc, a platformer, and I need some help with detecting whether or not the players current position is above the ground by 1, so he will stay at that y value, or if he is above by more than 1 he will fall until he is 1 above, so he can stand. I am using paintutils to load level images, and the ground/platforms are always light gray. Thanks in advance.
Cranium #2
Posted 26 August 2014 - 01:54 AM
It'd be helpful if you can post more information about what problem you're having, what it's supposed to do, and - more importantly - your code.
captsmitty77 #3
Posted 26 August 2014 - 03:02 AM
problem - there is no way i know of to detect collision between my players x and y position and the platforms
what its supposed to do - whenever my players x or y position is adjacent to a platform (which is a gray pixel) the player shouldnt be able to move any further in the direction of the platform

So I guess I need a way to know when my player is next to a certain point on the screen (a platform) to ensure he never phases through the platform, and when he isnt on a platform he should fall to give the game a sense of gravity.

So I need a way to know when my player is touching a platform.

I have no code for detecting collision, which is why I asked in hopes to get some advise to get going in the right direction.
theoriginalbit #4
Posted 26 August 2014 - 03:04 AM
How are you storing/displaying the platforms? are they simply an image you draw, or are they tracked by their x and y coordinates?
captsmitty77 #5
Posted 26 August 2014 - 03:07 AM
They are part of a paint image that is loaded and their coords arent tracked. Thats what i want to do but have a method of finding the platforms across multiple levels.
Bomb Bloke #6
Posted 26 August 2014 - 03:16 AM
Your images are loaded as "2D" tables. By inspecting the values inside them, you can determine where the player can and cannot go.
captsmitty77 #7
Posted 26 August 2014 - 03:28 AM
How do I access the tables?

for k, v in pairs(what would this be?) do
print(k.." "..v)
end ?
Edited on 26 August 2014 - 01:28 AM
Bomb Bloke #8
Posted 26 August 2014 - 03:45 AM
Well, let's say you did this:

local myLevel = paintutils.loadImage("myImage")

"myLevel" ends up as a numerically indexed table, with an amount of indexes equal to the height of the image. Each contains another numerically indexed table, with an amount of indexes equal to the width of the image. Those contain the values of the colours to be drawn at that row/column.

So say you did this:

for y = 1, #myLevel do
  for x = 1, #myLevel[y] do
    term.write(myLevel[y][x]..", "
  end

  print("")
end

… you'd expect to get a print out of the colour values used in the image.

By comparing where your player is trying to move against those colour values, you should be able to figure out whether a given move is allowed or not. Say you have your player's current position stored in the variables x and y, and the player wants to move one to the right, you'd check to see what myLevel[y][x+1] is first…
captsmitty77 #9
Posted 26 August 2014 - 04:00 AM
Ok I have used your example and i see the decimal codes, I will try this method and let you know how well it works for me. Thanks
captsmitty77 #10
Posted 26 August 2014 - 11:46 PM
I got it working now i only need to figure out a way to stop the player when his x position is 1 from light gray (256)