Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Few questions - collision detection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Aurioch   Crossbones+   -  Reputation: 1304

Like
0Likes
Like

Posted 19 June 2012 - 02:43 PM

Hello

First, sorry if I opened this thread in wrong place.
My name is Josip and I recently found about this site from my friend. I became interested in game developing since I was kid, and 6 years ago I found out a way to start developing my own games. Problem is that there weren't anyone who I can ask how to do something; only literature I could get were Intellisense (yeah), some tutorials on the Net and MSDN Library,

For now, I'm using XNA Framework for start to get a grip on basics. I started writing several small, useless projects to test stuff and handle basic algorithms and XNA's classes and methods. Some of them were shooting ducks with mouse, simple catching game and simple movement with top-down view (like Battle City on NES).

Now, my biggest project (which is currently on hold and I could resume or rewrite it) is a playable Arkanoid clone. For now, I only finished a bat, basic ball movement using simple textures, collision detection and bouncing. Bricks are next Posted Image
So, since it's my current project I'll use it for my examples and to concretize questions.

Now, several questions; also, when possible, I'd like only an idea so that I can try it first for myself:

1) Hitboxes: how to define them? For rectangular-shaped objects is fine since I can use simple Rectangle as a texture holder; however, it seems that having an array of Rectangles is not memory effective. Another problem appears when using irregular shapes (which WILL be used in each 2D game); having Rectangle as a placeholder to check for collision will detect collision on a place where the sprite is transparent.
I was checking for some ideas and found out about "underlay sprites"; using a sprite of the same shape and checking for it's position (via color?) and just drawing regular sprite over it. I forgot the exact site where it is, and don't have an idea how to perform it. Is it possible to draw textures on different levels (I saw that one overload of Spritebatch.Draw has something like "level" parameter) and to check pixel by color on that level?

2) I'm having little problems with Bat-Ball collision detection. I implemented ball moving with trigonometry which works well. Only problem is, depending on ball speed and bat texture height, ball sometimes enter the bat or completely pass through it. Here is the code:
		//Definitions
		private Texture2D Ball;
		private Vector2 BallCenter;
		private Vector2 BallOffset;
		private bool BallLaunch;
		private double BallAngle;
		private int BallSpeed;
		private int BallRadius;

			//Collision with bat
			if (BallLaunch)
			{
				for (x = 0; x < 2 * Math.PI; x += Math.PI / (Math.Pow(2.0d, (double)BallRadius)))
				{
					Helper.X = (int)(BallCenter.X + (double)BallRadius * Math.Cos(x));
					Helper.Y = (int)(BallCenter.Y + (double)BallRadius * Math.Sin(x));
					if (BatBorder.Contains(Helper))
						if ((Helper.Y > BatBorder.Y + 2) && (Helper.Y < BatBorder.Y + BatBorder.Height - 2))
							BallAngle = 3 * Math.PI - BallAngle;
						else
							BallAngle = 2 * Math.PI - BallAngle;
				}
			}
			//Ball movement
			if (BallLaunch)
			{
				BallCenter.X += BallSpeed / (float)Math.Cos(BallAngle);
				BallCenter.Y += BallSpeed / (float)Math.Sin(BallAngle);
			}

Collision with bat and Ball Movement are inside the Update(gametime GameTime) method. Should I do that differently?

3) Is it good for me to continue working with XNA Framework or should I transition to another engine? I know C# and VB well and have some problems with C++ (mainly due to lack of Intellisense in VS 2010 for C++) since I don't know all functions and procedures; yet, I can transition to it without problems. If I should work with another engine, which one would You recommend?

I'll ask more questions when they pop to my mind or I need help with something

Thanks for reading and even more for answering.

Sponsor:

#2 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 19 June 2012 - 03:06 PM

well... first of all XNA is fine to make games.

As for your questions XNA has classes in place for collision detection.

Search for BoundingBoxes, BoundingSphere and ray they have very handy methods for comparing coallitions, you will define a box or a sphere over your bats or bricks or what ever, and you just do a clean swipe on them all...

for each hitbox as Boundingsphere in listofobjects
	 if hitbox.intersects(player.boundingsphere) then
	 	 HIT!!! do stuff here
	 end if
end for

Hope it helps.

Edited by winsrp, 19 June 2012 - 03:07 PM.


#3 winsrp   Members   -  Reputation: 273

Like
1Likes
Like

Posted 19 June 2012 - 03:09 PM

also start here, it will teach you most of the basics. I started here too.

#4 6677   Members   -  Reputation: 1058

Like
0Likes
Like

Posted 19 June 2012 - 03:13 PM

I can't help on the first 2 questions but for now stick it out in XNA. When you feel you are ready there are 2 great C#/VB.net compatible options in MOgre and slimdx. XNA offers alot of other sound features etc that you don't get in ogre or slim but ogre and slim are probably more powerful choices if your willing to use other libraries to cover what they don't provide but XNA does.

Theres also OpenTK which is also a C#/VB.net render engine but unlike mogre. XNA and slimdx it runs on mono instead of .net so opentk games can be compiled for windows, mac or linux rather than just windows. I believe they will also compile under monotouch and monodroid for iOS and android support. OpenTK doesnt seem to have as much documentation or look as pretty though, only use it if you want cross platform support.


The Unity game engine also uses C# (made possible by it actually using mono under the hood). Games made in unity will run on windows, mac or the unity web player (which I think is also only available for windows and mac). For a $400 each fee you can compile your projects to iOS or android aswell and linux support has been long rumored and one of the recent experimental releases it appears one of the devs left a linux reference in although more recent releases this has been missing but shows it must be coming soon. One of my favourite games happens to use unity, The Kerbal Space Program. Its very flexible in what it does and is probably a good choice one day. All you need to provide is the code and the assets (models, textures. It even makes getting more easy though, they have their own web store of them).

EDIT: I just heard about the 4.0 release of unity thats upcoming. Linux support is being added. It also turns out that with additional licenses unity can target xbox 360, playstation 3 and the wii so that would be true cross platform gaming with the correct licenses.

Edited by 6677, 19 June 2012 - 04:28 PM.


#5 Aurioch   Crossbones+   -  Reputation: 1304

Like
0Likes
Like

Posted 24 June 2012 - 07:48 AM

Thanks for answers, both of you.

winsrp, I'm making 2D game so BoundingBox and BoundingSphere are out of question... I'll use the tutorial you gave me. It will require complete code rewrite then...

Well then, I'll stick to the XNA for now and keep Unity in mind. Few 2D projects first, then I'll move on to 3D... I'll need to learn to use Blender or Maya too >.<

#6 antiHUMANDesigns   Members   -  Reputation: 58

Like
0Likes
Like

Posted 24 June 2012 - 09:33 AM

I did some 3D modeling for a small game that used XNA, and the collision detection/response seemed to be lacking. It acted really weird once the speeds increased. But of course, it might have been the programmer's fault, but it seemed to improve once he switched to another library for physics. ("Bullet" library, I think.)

#7 antiHUMANDesigns   Members   -  Reputation: 58

Like
0Likes
Like

Posted 24 June 2012 - 09:34 AM

Thanks for answers, both of you.

winsrp, I'm making 2D game so BoundingBox and BoundingSphere are out of question... I'll use the tutorial you gave me. It will require complete code rewrite then...

Well then, I'll stick to the XNA for now and keep Unity in mind. Few 2D projects first, then I'll move on to 3D... I'll need to learn to use Blender or Maya too >.<


There's a good and free 2D physics library called Box2D (right?). You could check that out.

#8 antiHUMANDesigns   Members   -  Reputation: 58

Like
0Likes
Like

Posted 24 June 2012 - 11:23 AM

meh? This wasn't supposed to be a new post, just an Edit. 8|

Edited by antiHUMANDesigns, 24 June 2012 - 11:24 AM.


#9 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 25 June 2012 - 12:02 AM

well you know everything you do in XNA is actually 3d.. so bounding boxes/spheres are really easy to use

#10 Aurioch   Crossbones+   -  Reputation: 1304

Like
0Likes
Like

Posted 02 August 2012 - 11:43 AM

Hello again

Sorry for necroing, problem is thread-related and I didn't want to open a new thread.

I recently restarted one of my projects (Battle Tanks clone, I can make journal about making it) and I used rectangles for collision detection since I didn't need anything more complicated.

That is, until with this code:

bool Collision(Rectangle Object, bool Projectile)
	    {
		    if (Object.Left == RectPlayable.Left) return true;
		    for (int x = 0; x < BrickWallTiles.Length; x++) if (Object.Intersects(BrickWallTiles[x])) return true;
		    return false;
	    }

I ended with this:
Posted Image

And I don't have any idea why's that; according to code, that shouldn't happen. My question is: why is this happening?

Remaining revelant code:
			Texture2D PlayableArea;
			Texture2D Player1;
			Texture2D Bullet;
			Rectangle RectPlayable;
			// Tank characteristics - Player1 is index 0, Player2 is index 1, rest are AI
			Rectangle[] TankRectangle = new Rectangle[1];
			Vector2[] TankPosition = new Vector2[1];
			Vector2[] TankOrigin = new Vector2[1];
			float[] TankRotation = new float[1];
			// Bullet characteristics - Player1 is index 0, Player2 is index 1, rest are AI
			Rectangle[] BulletRectangle = new Rectangle[1];  
			Vector2[] BulletPosition = new Vector2[1];
			Vector2[] BulletOrigin = new Vector2[1];
			float[] BulletRotation = new float[1];
			float[] BulletScaling = new float[1];
			bool[] BulletLaunched = new bool[1];
			// Brick walls
			Texture2D BrickWall;
			Rectangle[] BrickWallTiles;
----------------------------------------------------------------------
			// Load all textures
			Player1 = this.Content.Load<Texture2D>("Player1");
			PlayableArea = this.Content.Load<Texture2D>("PlayableArea");
			Bullet = this.Content.Load<Texture2D>("Bullet");
			BrickWall = this.Content.Load<Texture2D>("Brick-Wall");
			// Set up playable area
			RectPlayable.X = 0;
			RectPlayable.Y = 0;
			RectPlayable.Width = PlayableArea.Width;
			RectPlayable.Height = PlayableArea.Height;
			// Set up brick walls
			BrickWallTiles = new Rectangle[8];
			for (int i = 0; i < 8; i++)
			{
				BrickWallTiles[i].X = 0;
				BrickWallTiles[i].Y = i * 40;
				BrickWallTiles[i].Width = 40;
				BrickWallTiles[i].Height = 40;
			}
			// Set up Player 1
			TankRectangle[0].X = 250;
			TankRectangle[0].Y = 250;
			TankRectangle[0].Width = 40;
			TankRectangle[0].Height = 40;
			TankOrigin[0].X = Player1.Bounds.Center.X;
			TankOrigin[0].Y = Player1.Bounds.Center.Y;
			TankRotation[0] = (float) Math.PI / 2;

Thanks for answers

Edited by Aurioch, 02 August 2012 - 11:44 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS