• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
P0jahn

Quickest way to iterate through a rectangles lines?

10 posts in this topic

I have a rectangle, with an x and y coordinate which represent the top-left corner of the rectangle. I also have the width and the height.

 

I need a piece of code that can iterate through all the four lines of the rectangle fast.

 

My crap code works but have 4 for-loops o_0

0

Share this post


Link to post
Share on other sites

Just emit the four corners by hand: (x, y), (x+w, y), (x+w, y+h) and (x, y+h). You already know the shape, the origin and the size, and the number of points to calculate is so trivial that hand calculation is easy enough.

0

Share this post


Link to post
Share on other sites

yeah, but that would require four for-loops, which is inefficient. Performance is critical.

0

Share this post


Link to post
Share on other sites

are you trying to do something like this ?

 

#define MAX_POLYVERTS    4
#define MASK_POLYVERTS    (MAX_POLYVERTS - 1)

int rect_pts[MAX_POLYVERTS][2] = { {x, y}, {x+w, y}, {x+w, y+h}, {x, y+h}};
int a, b;

for(a = 0; a <= MAX_POLYVERTS; a++)
{
    point1_x = rect_pts[a][0];
    point1_y = rect_pts[a][1];

    // get coords of next point, use bitmask to 'wrap' value back to zero
    point2_x = rect_pts[(a+1)&MASK_POLYVERTS][0];
    point2_y = rect_pts[(a+1)&MASK_POLYVERTS][1];

    // ... do something here with points 1 and 2
}

0

Share this post


Link to post
Share on other sites

Can you post what you have so that we have a clearer idea of what you're talking about? I'd tend to assume that you're trying to iterate through pixel positions, but...

0

Share this post


Link to post
Share on other sites


yeah, but that would require four for-loops, which is inefficient. Performance is critical.


Emitting four trivial vertices requires zero loops. Even radioteeth's solution has only one loop, an that loop can easily be expanded and eliminated alltogether if you don't want it.
0

Share this post


Link to post
Share on other sites

I think he is trying to draw the rectangle dot-by-dot.

Why not just draw the lines for each side?  Why draw the dots manually?

 

 

L. Spiro

0

Share this post


Link to post
Share on other sites

I am checking if the rectangle is intersecting with solid tile.

 

Here is my previous code(it was two for loops instead of one:

		int x = entity.x;
		int y = entity.y;
		int width =  entity.width;
		int height = entity.height;
		
		//Check the top and bottom line
		for(int i = 0; i < width; i++)
			if(foo(i, y) || foo(i, y + height))
				...
			
                //Check left and right line
		for(int i = 0; i < height; i++)
			if(foo(x, i) || foo(x, i + width))
				...
Edited by P0jahn
0

Share this post


Link to post
Share on other sites

I'm sure that whatever language you are using (your code snippet can be several) there is a Rectangle implementation in the core API which has an intersects( anotherRectangle ) method?

0

Share this post


Link to post
Share on other sites

I am checking if the rectangle is intersecting with solid tile.

 

Here is my previous code(it was two for loops instead of one:

		int x = entity.x;
		int y = entity.y;
		int width =  entity.width;
		int height = entity.height;
		
		//Check the top and bottom line
		for(int i = 0; i < width; i++)
			if(foo(i, y) || foo(i, y + height))
				...
			
                //Check left and right line
		for(int i = 0; i < height; i++)
			if(foo(x, i) || foo(x, i + width))
				...

 

 

AABB vs AABB :

 

http://www.miguelcasillas.com/?p=30

0

Share this post


Link to post
Share on other sites

I am checking if the rectangle is intersecting with solid tile.

Are your "tiles" in a rectangular shape? If so, you don't need to test the pixils, you can use math to see if they are intersecting.

for (int t=0;t<NUMBER_OF_TILES;t++){
	int px=entity.x-tile[t].x;
	int py=entity.y-tile[t].y;
	bool xCollide=false,yCollide=false;
	if (entity.sizeX<=tile.sizeX){
		if (((px>=0)&&(px<tile[t].sizeX))||((px+entity.sizeX>=0)&&(px+entity.sizeX<tile[t].sizeX))) xCollide=true;
	}
	else{
		//if the entity size is greater than the tile size, px needs to be reversed
		px=tile[t].x-entity.x;
		if (((px>=0)&&(px<entity.sizeX))||((px+tile[t].sizeX>=0)&&(px+tile[t].sizeX<entity.sizeX))) xCollide=true;
	}
	if (entity.sizeY<=tile[t].sizeY){
		if (((py>=0)&&(py<tile[t].sizeY))||((py+entity.sizeY>=0)&&(py+entity.sizeY<tile[t].sizeY))) yCollide=true;
	}
	else{
		//if the entity size is greater than the tile size, py needs to be reversed
		py=tile[t].y-entity.y;
		if (((py>=0)&&(py<entity.sizeY))||((py+tile[t].sizeY>=0)&&(py+tile[t].sizeY<entity.sizeY))) yCollide=true;
	}
	if (xCollide && yCollide){//they are colliding
		//do something......
	}
}

This code checks the bounds of x and y values to determine whether the rectangle of the "entity" is colliding with the rectangle of the "tile". If it collides in both x and y, then at least some part of the entity is colliding with that tile.

 

If your "entity" is not solid, then you can just check the linear bounds of x and y seperately

Edited by Hawkblood
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0