Page 1 of 1

Restrict Rectangle Movement

Posted: Wed Dec 07, 2011 12:07 am
by tdq11
Hey, I'm really new to LOVE and I just don't know how to restrict a simple key-controlled rectangle to the screen. I don't want it to pass the edges of the screen. There has to be an unbelievably simple way of doing this, but I couldn't find it in the API or the forums. Maybe I wasn't looking in the right place.

The other thing I need to do is restrict the rectangle from moving past the edges of other objects. I read on the API that you can do a lot without love.physics and so I'd like to be able to this without it if possible.

Basically my end goal here is a simple prototype of a square cursor moving through a maze made up of other rectangles, but with anything I draw, the square just keeps passing through the edges of any other shape. I just need to limit it to only being able to move in the empty areas of the screen.

Thanks so much!

Re: Restrict Rectangle Movement

Posted: Wed Dec 07, 2011 4:28 am
by tentus
You're going to need to create a map of passable and impassable squares: a grid of tiles. There's an uncountable number of ways you could do this, but perhaps this tutorial will help you a little. http://love2d.org/wiki/Tutorial:Tile-based_Scrolling

Re: Restrict Rectangle Movement

Posted: Wed Dec 07, 2011 8:40 am
by miko
tdq11 wrote:Hey, I'm really new to LOVE and I just don't know how to restrict a simple key-controlled rectangle to the screen. I don't want it to pass the edges of the screen. There has to be an unbelievably simple way of doing this, but I couldn't find it in the API or the forums. Maybe I wasn't looking in the right place.

The other thing I need to do is restrict the rectangle from moving past the edges of other objects. I read on the API that you can do a lot without love.physics and so I'd like to be able to this without it if possible.

Basically my end goal here is a simple prototype of a square cursor moving through a maze made up of other rectangles, but with anything I draw, the square just keeps passing through the edges of any other shape. I just need to limit it to only being able to move in the empty areas of the screen.

Thanks so much!
Don't think in terms of Love API, but how to program it in a programming language (Lua in this case).
Hint: each of the rectangle vertex/edge must be on the screen (so you need to know vertex coordinates and screen size).
Similarly, if your cursor move would collide with any other object, then the move is not allowed.

Check out the attachment

Re: Restrict Rectangle Movement

Posted: Wed Dec 07, 2011 2:39 pm
by tdq11
Thanks a lot, guys. It's great to be pointed in the right direction, because there's a lot to learn. I tried doing it in love.physics, but I think that was quite a bit too complicated for something this rudimentary.