It is better for performance to use a lot of rectangles or a lot of vectors?

Started by
18 comments, last by KenWill 8 years, 10 months ago


like kids are interested in anything else rather than justing biber...ya know you could've actually answer my question :3

There are hundreds of thousands of youth who are gaming programmers. Many of them surf the web looking for game development information.

The well being of children and other youth is higher priority than all the programming in the world. High priority is also the ability of parents to trust that they don't have to supervise their children 100% of the time as kids view websites which are expected to be safe for them.

When or if you ever have children as a parent yourself, then you will begin to understand.

Sorry to break it to you, but teenagers can take a swear and i don't think little kids will be interested in coding but rather in programs like GameMaker or Construct2. But anyway i understand your point so i'm sorry for using those words on this forum.:)

Advertisement


Here's an old post I made that has some pseudo-code example moving along 1 axis, then checking for collisions, then moving along the other axis.
Link

Thanks a lot, i'll look to it and see if i can do that in my game using LibGDX.


Tell me where you're confused, and maybe I can help you better understand.

I don't know how to do that in Libgdx...how do i check if its colliding? i don't know what method i have to use.....

can you give me an example in libgdx or something?

Ok so let me see if i get it...


if(!enemyRec.overlaps(playerRec.getX() - 200, playerRec.getY()){
//do this
}
//200 is the amount of pixels it moves each frame

Now...won't this work only for like the x and y which are located in the lower left corner of the rectangle and i'll have to write another if statement for the upper and middle point of the rectangle side?

How do i get it to be like the whole height? Because if i for example put the code like this it will work (or not i didn't try it) but if i for example touch the enemyRec with the upper left corner then it will go through it..and thats why i am confused because i don't know how to get like the whole area between the lower and upper left corner...if you understand what i want to say...

Moving by 200 px per frame is way way way way too fast. Considering the game probably updates at 60hz you are moving your sprites at 12,000 pixels per second. There is a very good chance your two objects passed over each other so fast they never overlapped.

Ok so let me see if i get it...


if(!enemyRec.overlaps(playerRec.getX() - 200, playerRec.getY()){
//do this
}
//200 is the amount of pixels it moves each frame

Now...won't this work only for like the x and y which are located in the lower left corner of the rectangle and i'll have to write another if statement for the upper and middle point of the rectangle side?

How do i get it to be like the whole height? Because if i for example put the code like this it will work (or not i didn't try it) but if i for example touch the enemyRec with the upper left corner then it will go through it..and thats why i am confused because i don't know how to get like the whole area between the lower and upper left corner...if you understand what i want to say...

A quick google for "libgdx rectangle collision detection" should return a bunch of links to help you out.

However, I have to imagine libgdx has a simple overlaps function, where you simply call Rect1.overlaps(Rect2) and it returns true is the Rect1 and Rect2 are intersecting (ie, colliding). I don't know what you're doing with a single x and y value, that is simply a point, not a rectangle. So, just try:

if (player.Rect.overlaps(object.Rect)) {
  // the player has collidied with the object, do something ...
}
...

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


Moving by 200 px per frame is way way way way too fast. Considering the game probably updates at 60hz you are moving your sprites at 12,000 pixels per second. There is a very good chance your two objects passed over each other so fast they never overlapped.

It is 200 * Gdx.graphic.getDeltaTime() so i don't think thats the problem, they overlap anyway.


However, I have to imagine libgdx has a simple overlaps function, where you simply call Rect1.overlaps(Rect2) and it returns true is the Rect1 and Rect2 are intersecting (ie, colliding). I don't know what you're doing with a single x and y value, that is simply a point, not a rectangle. So, just try:
if (player.Rect.overlaps(object.Rect)) {
// the player has collidied with the object, do something ...
}
...

Yes but how do i get to know where its colliding? Ahh i'll look online fine i did it already and nothing...well i'll try again.

Now i kind of think that rectangle on each side collision thingy its the best option...


Yes but how do i get to know where its colliding? Ahh i'll look online fine i did it already and nothing...well i'll try again.

I already answered this:


You really should use 1 rectangle for collisions, and, the easiest way to check which side collided with what is to move your objects along 1 axis, then check for collisions, then move along the other axis and check for collisions.

This allows you to, not only know where the collisions occurred (move objects along x-axis, and if collided, you know which side caused the collisions

When you 1st just move the object along the x axis (see my example), if there is collision, then you know it collided on the side (and, you can know which side by checking what the velocity is). You can then move the object along the y-axis, and do the same thing.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


Yes but how do i get to know where its colliding? Ahh i'll look online fine i did it already and nothing...well i'll try again.

A quick google search for "libgdx collision detection" returned tons of interesting links.

Also, the java docs for libgdx show that Intersector.intersectRectangles returns the intersection of the two rectangles. That intersection rectangle will tell you where the intersection occurred.


Yes but how do i get to know where its colliding? Ahh i'll look online fine i did it already and nothing...well i'll try again.

I already answered this:


You really should use 1 rectangle for collisions, and, the easiest way to check which side collided with what is to move your objects along 1 axis, then check for collisions, then move along the other axis and check for collisions.

This allows you to, not only know where the collisions occurred (move objects along x-axis, and if collided, you know which side caused the collisions

When you 1st just move the object along the x axis (see my example), if there is collision, then you know it collided on the side (and, you can know which side by checking what the velocity is). You can then move the object along the y-axis, and do the same thing.

Well i did it...Now it looks for a collision every frame and if there is no collision it will move, if there is a collision it will not. And i also fixed the problem of getting stuck because it checks if the rectangle has collide not a side of it...am i making sense? i don't know...i had a hard day at school with exams and stuff...so here its my code, but its only for up and down....too lazy to do the rest now i'm gonna be back to it later but with this i'm sure someone can understand how it works and maybe help them too.


//the variables we will need for this (and of course you'll have to create the rectangles but i think you already know that o.o)
float plusVX = 100;//the +X axis
float minusVX = 100;//the -X axis
float plusVY = 100;//the +Y axis
float minusVY = 100;//the -Y axis
float currentV;//the current axis
boolean isO; //the boolean we will use to see if there is a collision or not


//checking to see if there is a collision
if(player.overlaps(enemy)){
		isO = true;//if yes then set to true
}
if(!player.overlaps(enemy)){
		isO = false;//if not set it to false
}
//if there is no collision then move		
if(Gdx.input.isKeyPressed(Keys.UP) && isO == false){
	currentV = plusVY;//set the current axis you're moving on
	player.y += plusVY * Gdx.graphics.getDeltaTime();//and the value * Gdx.graphics.getDeltaTime() so it will be smoother 		
}
//if there has been a collision while moving on the +Y axis then stop the object
if(isO == true && currentV == plusVY){
	player.y = player.y;

       if(Gdx.input.isKeyPressed(Keys.DOWN)){
		currentV = minusVY;
		player.y -= minusVY * Gdx.graphics.getDeltaTime();
		
}/* the if statement above will check if down arrow key is pressed and then move it down by minusVY. 
this will run for a frame or so until the object is not touching the other object anymore, and the
 next frame the other code that take cares of down movement will take its place. we set the
 currentV to minusVY when it runs so there is no need to press the button again to make the transition */
	
}
		
//and here we do the same, but this time we move on the -Y axis		
if(Gdx.input.isKeyPressed(Keys.DOWN) && isO == false){
	currentV = minusVY;
	player.y -= minusVY * Gdx.graphics.getDeltaTime();
}
		
if(currentV == minusVY && isO == true){
	player.y = player.y;
			
	if(Gdx.input.isKeyPressed(Keys.DOWN)){
		currentV = plusVY;
		player.y += plusVY * Gdx.graphics.getDeltaTime();
}

}

Hope this will help others:D

I'm glad i finally did it because it was starting to piss me off....and i didn't understand what you guys meant by "move and check" because i didn't know how i would check which side it is on. Anyway glad i did it lol...thanks a lot!

This topic is closed to new replies.

Advertisement