Restrict Rectangle Movement

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
tdq11
Prole
Posts: 2
Joined: Tue Dec 06, 2011 11:58 pm

Restrict Rectangle Movement

Post 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!
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Restrict Rectangle Movement

Post 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
Kurosuke needs beta testers
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Restrict Rectangle Movement

Post 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
Attachments
CollisionExample.love
(819 Bytes) Downloaded 180 times
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
tdq11
Prole
Posts: 2
Joined: Tue Dec 06, 2011 11:58 pm

Re: Restrict Rectangle Movement

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 7 guests