2D collisions with GUI

I am trying to make a scrolling 2d game in Roblox. One problem I am facing is collisions with the player. In my game, the tiles move instead of the player to achieve the scrolling effect. I have searched all over the devforum to a solution but I’m yet to find one.

One of the things I’ve tried is this code here.

if self.PlayerPosition.X + self.Player.AbsoluteSize.X > Blocks.AbsolutePosition.X and self.PlayerPosition.X < Blocks.AbsolutePosition.X + Blocks.AbsoluteSize.X and self.Position.Y + self.Player.AbsoluteSize.Y > Blocks.AbsoluteSize.Y and self.Position.Y < Blocks.AbsolutePosition.Y + Blocks.AbsoluteSize.Y then
    -- boom I hit a wall

PlayerPosition is a value I add to every time I move all the tiles, blocks being the tiles I’m trying to collide with.

Basically, I’m not too sure how to calculate for physics, especially with my techniques of moving the tiles.

If anyone knows how to help me, please leave an answer. Thanks!

Edit: I got the collision detection working but I don’t know how to make it like get out of the square it hit.

if self.PlayerPosition.X < FinalBlocksPosition.X + Blocks.AbsoluteSize.X and self.PlayerPosition.X + self.Player.AbsoluteSize.X > FinalBlocksPosition.X and self.PlayerPosition.Y < FinalBlocksPosition.Y + Blocks.AbsoluteSize.Y and self.PlayerPosition.Y + self.Player.AbsoluteSize.Y > FinalBlocksPosition.Y then
    --Get out of block I hit

I remember, awhile ago, there used to be a direct example on the old Roblox wiki on how to calculate for 2d UI collisions. The best I could find just now was this module by jaipack17.

I’ve tried some of the stuff from jaipack but it didn’t work sadly. I assume it’s because I do some different methods for stuff.

This caught my eye, so I’ll answer. Since you’re using a tile map for your game and since the PlayerPosition is in accordance to the positions on the tile map. It should be much easier. Say some tiles of the map are declared to be “Rigid”. These are the tiles on which you can’t step/pass through.

The idea is really simple, when the player presses the control keys on the keyboard, you’ll need to find their future position, which is what you must be doing when incrementing the PlayerPosition. If you have this future position, you just need to validate if the tile you are going to step on is Rigid or not. If yes then do not increment the PlayerPosition, if not move the player to that tile.

My problem is that I don’t think the player position is calculated correctly

Update: I got detection working but I don’t know how to force the player out of the block without making it bounce, like terraria

still cant figure it out (character limit)

Just wanted to point out I fixed this.

1 Like