Skip to main content
GameDev.net gamedev.net
🔒 Locked

DirectX 2D Intersect

Started by Hoggel Apr 16, 2009 at 12:41 PM 2 replies 950+ views
Original Post
Hoggel
Hoggel
Hi! I'm trying to do i small game, but now i've rendred the background and will be making the character soon, but I have no clue how I'm supposed to do the intersect between the character and the background, since it's round (http://hoggel.webs.com/photos/The%20Legend%20of%20Zelda%20Ocarina%20of%20Time%202D/Game.PNG) the whole thing, except the floor, is ONE texture, so i can't do something like (if (character.Xcoord > bla..)) and onward.. so can someone please give me some tips on how to do it? thanks in advance! // Hogge
Erik Rufelt
Erik Rufelt
If you want it exactly a circle, the you can simply check the distance from the center. If that distance is larger than the radius, then it's too far away and must be moved back. (If you're a beginner I would start with this).
If you don't want it exactly a circle, but rather the exact pixel line as the edge, then it's more complicated. You must either keep the image in memory and compare the player position against those pixels, or create some kind of list of lines that define the edge.

Another option is to divide your map into smaller tiles, and mark every tile as open or closed, then do simple rectangle collision checks against those that are closed. This if often used in games.
Hoggel
Hoggel
do you think that you could write a small code example, I really dont know how to do it, sure a rectangle is easy, but a circle... i dunno

nu när jag ser att du dessutom är från sverige, kan du skriva på svenska? :)
Erik Rufelt
Erik Rufelt
I assume you have your player position, and have done a collision check against a rectangle. Modifying it to a circle is not very hard, you just instead of checking if 'character.x > maxX', check if 'distance to center > maxDist'.
point character;point center;// Check the distance from character to centerfloat xdistance = character.x - center.x;float ydistance = character.y - center.y;float distance = sqrt(xdistance*xdistance + ydistance*ydistance);if(distance > maxDistance) { // Move character back to the maximum distance from the center character.x = center.x + xdistance * maxDistance / distance; character.y = center.y + ydistance * maxDistance / distance;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.