Jump to content
  • Advertisement
Sign in to follow this  
JavaMava

Rectangle collision, with gravity and tiles...

This topic is 4705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay so I had collsision working perfectly when it was just two rectangles and you could control one. Then I had the same two rectangles and some gravity, and had it working perfectly. Then I tried to add a tile based level, and everything went wrong. First my gravity stoped working, but don't worry about that yet. Most important is my collsion seems to be off. It works when the rectangle(you control)is not exactly lined up with the tiles, but if it is collision doesn't work. EDIT : Okay so I off set the characters collision box by 1, and collision will work fine. I still would like it to work properly though and not with this quick fix. Also with this new fix if the box is ontop of a tile it can't move left. It can move right but not left, no matter if I offset it by 1 or -1, it does the same. Also gravity still doesn't work, unless while its moving up it hits a tile then gravity magically works. Heres the code, it in turing so it might look different import GUI var win := Window.Open ("graphics:704;448") View.Set ("offscreenonly") %These are variables for input from a controller var JoyX, JoyY : int var JoyButton1, JoyButton2, JoyButton7, JoyButton8 : boolean %Variables used in Move Collided Object var x, y : array 1 .. 2 of int %These variables are the surrounding collision box cordinates of the character var tilex1, tilex2, tiley1, tiley2 : int var col : boolean := false var penetrationLeft, penetrationRight, penetrationUp, penetrationDown : int var collision : int := 0 %This is the values for the collision box around the character x (1) := 100 y (1) := 170 x (2) := x (1) + 32 y (2) := y (1) + 32 %These are variables used for gravity and jumping var count, limit, vel, grav, power : int count := 0 limit := 20 %How long you can keep moving up power := 20 %The power you jump up at grav := 4 %The Gravity of the world vel := 0 %Your current velocity const MAPTILEX := 100 %amount of x tiles in the entire map const MAPTILEY := 25 %amount of y tiles in the entire map const TILESIZEX := 32 %Size of x tile in pixels const TILESIZEY := 32 %Size of x tile in pixels const SCREENHIEGHT := 14 %number of tile up on screen at once const SCREENWIDTH := 22 %number of tile across on screen at once var map : array 0 .. MAPTILEX of array 0 .. MAPTILEY of int %saves what tile is in each location var tile, tile1 : int := 0 %current tile being drawn var disX, disY : int := 0 % This is the displacement for when the world is scrolled over var fileNo : int tile1 := Pic.FileNew ("tile.bmp") %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% proc Open %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% var fileName : string := "TestMap" % Name of file var fileNo : int % Number of file open : fileNo, fileName, read read : fileNo, map close : fileNo end Open %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% proc Draw_Map %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for x : 0 .. SCREENWIDTH - 1 %Goes through every tile on the x on the screen for y : 0 .. SCREENHIEGHT - 1 %Goes through every tile on the y on the screen tile := map (x + disX) (y + disY) %Makes current tile being drawn equal to corresponding tile in map if tile > 0 then %Skips drawing if the tile is not walkable Pic.Draw (tile, (x * TILESIZEX), (y * TILESIZEY), picCopy) %draws tile end if end for end for end Draw_Map %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%% proc DRAW %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Draw.Box (x (1), y (1), x (2), y (2), brightred) %This is the character right now, a box end DRAW %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%% proc INPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Joystick.GetInfo (joystick1, JoyX, JoyY, JoyButton1, JoyButton2, JoyButton7, JoyButton8) %Movement on x axis if JoyX > 0 then x (1) += 10 x (2) := x (1) + 32 elsif JoyX < 0 then x (1) -= 10 x (2) := x (1) + 32 end if %Jumping input(y axis) if JoyButton1 = true and count < limit then %If the character is jumping and can still go higher vel := (power - grav) %Upwards velocity is = to power of jump - gravity of the level count += 1 %when count is > than limit the character can't go anyhigher and starts to fall elsif JoyButton1 = true and count >= limit then %If the character is jumping but reached his limit %Upwards velocity is = to current velocity - gravity, eventually this will be a negative and pull you down vel := (vel - grav) if vel < -15 then %this is the limit of the speed at which you can fall vel := -15 end if elsif collision = 4 and JoyButton1 = false then %lets you jump again if you have landed and let go of jump count := 0 %if you land you will stop falling, but if your still holding jump you wont jump until you let it go, then press it elsif collision = 4 then vel := 0 elsif JoyButton1 = false then %if you let go of jump you will begin to fall vel := (vel - grav) count := limit if vel < -15 then %This is the limit of the speed that you can fall vel := -15 end if end if %after gravity takes effect this gives you your y y (1) += vel y (2) := y (1) + 32 end INPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% proc Rectangle_Collission (x1, y1, x2, y2, x3, y3, x4, y4 : int) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if x2 < x3 then %Checks if rect is off to the left col := false collision := 0 elsif x1 > x4 then %Checks if rect is off to the right col := false collision := 0 elsif y2 < y3 then %Checks if rect is off to the bottom col := false collision := 0 elsif y1 > y4 then %Checks if rect is off to the top col := false collision := 0 else col := true %anything else than collission must be true end if end Rectangle_Collission %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% procedure Move_Collided_Object (x1, y1, x2, y2, x3, y3, x4, y4 : int) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% penetrationLeft := x2 - x3 penetrationRight := x4 - x1 penetrationUp := y2 - y3 penetrationDown := y4 - y1 %Checks if Left is the smallest, then aplies displacement, also tells the side it collided on if penetrationLeft < penetrationRight and penetrationLeft < penetrationUp and penetrationLeft < penetrationDown then x (1) -= penetrationLeft collision := 1 %Checks if right is the smallest, then aplies displacement, also tells the side it collided on elsif penetrationRight < penetrationLeft and penetrationRight < penetrationUp and penetrationRight < penetrationDown then x (1) += penetrationRight collision := 2 %Checks if top is the smallest, then aplies displacement, also tells the side it collided on elsif penetrationUp < penetrationRight and penetrationUp < penetrationLeft and penetrationUp < penetrationDown then y (1) -= penetrationUp collision := 3 %Checks if bottom is the smallest, then aplies displacement, also tells the side it collided on elsif penetrationDown < penetrationRight and penetrationDown < penetrationUp and penetrationDown < penetrationLeft then y (1) += penetrationDown collision := 4 end if end Move_Collided_Object %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% procedure Control_Functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /*This works by taking each corner of your characters surrounding box and seeing what tile its in. Then it sees if the tile is walkable or not. If it's not then it gets the tiles surroundign box(x1,y1,x2,y2) and moves your character out of it. */ for h : 1 .. 2 %Goes through characters y1, and y2 for w : 1 .. 2 %Goes through characters x1, and x2 tile := map (floor (x (w) / TILESIZEX)) (floor (y (h) / TILESIZEY)) %This ses what tile it in if tile > 0 then %this checks if its walkable or not tilex1 := (floor (x (w) / TILESIZEX) * TILESIZEX) tiley1 := (floor (y (h) / TILESIZEY) * TILESIZEY) tilex2 := (ceil (x (w) / TILESIZEX) * TILESIZEX) tiley2 := (ceil (y (h) / TILESIZEY) * TILESIZEY) Move_Collided_Object (x (1), y (1), x (2), y (2), tilex1, tiley1, tilex2, tiley2) end if end for end for end Control_Functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Open loop cls INPUT Control_Functions x (2) := x (1) + 32 y (2) := y (1) + 32 Draw_Map DRAW View.Update delay (50) end loop Thats the code, if you can help, please dont post other code thats not turing. It would be best you you didn't post any code and just helped me with my logic. Ask if you have any questions. [Edited by - JavaMava on January 21, 2006 8:09:15 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:

%Jumping input(y axis)
if JoyButton1 = true and count < limit then %If the character is jumping and can still go higher
vel := (power - grav) %Upwards velocity is = to power of jump - gravity of the level
count += 1 %when count is > than limit the character can't go anyhigher and starts to fall
elsif JoyButton1 = true and count >= limit then %If the character is jumping but reached his limit
%Upwards velocity is = to current velocity - gravity, eventually this will be a negative and pull you down
vel := (vel - grav)
if vel < -15 then %this is the limit of the speed at which you can fall
vel := -15
end if
elsif collision = 4 and JoyButton1 = false then %lets you jump again if you have landed and let go of jump
count := 0
%if you land you will stop falling, but if your still holding jump you wont jump until you let it go, then press it
elsif collision = 4 then
vel := 0
elsif JoyButton1 = false then %if you let go of jump you will begin to fall
vel := (vel - grav)
count := limit
if vel < -15 then %This is the limit of the speed that you can fall
vel := -15
end if
end if


All of these elseif's in your code might be your problem, look through them and see if they any of the elseif's are triggered before you reach the last gravity applying statement. Beucase in this code.

Quote:


%Checks if Left is the smallest, then aplies displacement, also tells the side it collided on
if penetrationLeft < penetrationRight and penetrationLeft < penetrationUp and penetrationLeft < penetrationDown then
x (1) -= penetrationLeft
collision := 1
%Checks if right is the smallest, then aplies displacement, also tells the side it collided on
elsif penetrationRight < penetrationLeft and penetrationRight < penetrationUp and penetrationRight < penetrationDown then
x (1) += penetrationRight
collision := 2
%Checks if top is the smallest, then aplies displacement, also tells the side it collided on
elsif penetrationUp < penetrationRight and penetrationUp < penetrationLeft and penetrationUp < penetrationDown then
y (1) -= penetrationUp
collision := 3
%Checks if bottom is the smallest, then aplies displacement, also tells the side it collided on
elsif penetrationDown < penetrationRight and penetrationDown < penetrationUp and penetrationDown < penetrationLeft then
y (1) += penetrationDown
collision := 4
end if


collision may always be set to 4 for some reason.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!