• 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
phil67rpg

collision code

16 posts in this topic

Well I am studying this piece of code.I am trying to develop a bounding box collision detection algorithm.this one appears to use 2 circles.I will do more research using google.
[font="Consolas"][size="2"][font="Consolas"][size="2"][code][/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]bool detectcollision(int x1, int y1, int radis1, int x2, int y2, int radis2)
{
double xd, yd, Distance;

//2d
xd = x2-x1;
yd = y2-y1;
Distance = Math.Sqrt(xd * xd + yd * yd);


if( radis1 + radis2 >= Distance)
return true; //collision
return false; //no collision
}
[/code][/size][/font][/size][/font]
0

Share this post


Link to post
Share on other sites
What exactly are you asking? Basically, the logic is that you find the distance between the two centers of the circles using the distance formula. Then you compare this to the sum of the radii. If the distance between the centers is less than the sum of the radii than your circles have to overlap and you have a collision. The closest two circles can get is adjacent when the center to center distance would be the sum of the radii, anything less and they hit.
0

Share this post


Link to post
Share on other sites
Rectangles don't allow this easy collision detection system as they can have uniform shapes, which a circle or sphere can't have. For rectangles you are going to have to write a contains method and if one point of a rectangle lies within the area of another one it is colliding with it.
This book (http://books.google.co.uk/books?id=WGpL6Sk9qNAC&source=gbs_book_similarbooks) is collision detections bible btw.
0

Share this post


Link to post
Share on other sites
well I am working with a small piece of code,for some reason it never gets to label1.Text="true" or label1.Text='false",
[code]
const int LEFT = 0;
const int TOP = 0;
const int RIGHT = 5;
const int BOTTOM = 3;
int [] rect1 = new int[4];
int [] rect2 = new int[4];
public bool collideRectRect(int[] rect1, int[] rect2)
{
if (rect1[RIGHT] < rect2[LEFT] ||
rect1[LEFT] > rect2[RIGHT] ||
rect1[TOP] < rect2[BOTTOM] ||
rect1[BOTTOM] > rect2[TOP])
{
label1.Text = "false";
return false;
}
else
{
label1.Text = "true";
return true;
}
}
[/code]
0

Share this post


Link to post
Share on other sites
It looks like PhysicsEngine in game,about the
[left]collision detection.[/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]Last month,[/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]I had the same problem as you .Now,i still not solve this problem,i think that it surely not easy.[/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]If the thing is ball,you can use my code.[/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]I'm not sure this will help you a lot.But ,we are in the same problem ,i'm very happy to share that.[/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]This code use the ActionScript in Adobe Flash.[/size][/font][/color][/left]

There are two balls,one called ball,the other is balll.(not the same).
[code]function onEnterFrame(e:Event)
{
ball.y += ball.vy;
ball.x += ball.vx;
balll.x += balll.vx;
balll.y += balll.vy;
sina = (ball.y - balll.y)/(ball.radius + balll.radius); //their sin corner and cos corner (Rectangle Function)
cosa = (ball.x - balll.x)/(ball.radius + balll.radius);
distance = Math.sqrt((ball.x - balll.x)*(ball.x - balll.x) +(ball.y - balll.y)*(ball.y - balll.y)); //this is the distance between two balls
if (distance <= ball.radius +balll.radius)
{
double v;
v = Math.sqrt(ball.vx * balll.vx + ball.vy * balll.vy);
ball.vx = (v * cosa);
ball.vy = (v * sina);
balll.vx = -(v * cosa);
balll.vy = -(v * sina);
}
}[/code]

My problem is that if two balls overlap,the ball will not move , i can't find a good formula to not not let them overlap when "distance == ball.radius+balll.radius",
My idea is
if (distance <= ball.radius +balll.radius)
{
Remove two ball to the position where they crash.//it will be seen that they are not overlap .
.........
}
But i cannot do it!It is still a question.
0

Share this post


Link to post
Share on other sites
Sorry,I think i [color=#000000][font=Arial]give an irrelevant answer,and add a new question.But they are looked same.You can refer to it or ignore it.[/font][/color]
0

Share this post


Link to post
Share on other sites
What do you mean by
[quote name='phil67rpg' timestamp='1332621245' post='4924969']
for some reason it never gets to label1.Text="true" or label1.Text='false",
[/quote]?

How does the code not executed either? Either the conditional will be true and it show be false or it will go to the else and read true. Are you getting any errors or anything? it looks like this shouldn't work.[quote name='phil67rpg' timestamp='1332621245' post='4924969'][code]
const int RIGHT = 5;
const int BOTTOM = 3;
int [] rect1 = new int[4];
int [] rect2 = new int[4];
public bool collideRectRect(int[] rect1, int[] rect2)
{
if (rect1[RIGHT] < rect2[LEFT] ||
[/code][/quote]

You are saying rect1 and ret2 are arrays of 4 ints. Then you try to get the 5th element of the array when you call rect1[RIGHT]. Thats out of bounds
0

Share this post


Link to post
Share on other sites
[quote name='phil67rpg' timestamp='1332621245' post='4924969']
well I am working with a small piece of code,for some reason it never gets to label1.Text="true" or label1.Text='false",
[code]
const int LEFT = 0;
const int TOP = 0;
const int RIGHT = 5;
const int BOTTOM = 3;
int [] rect1 = new int[4];
int [] rect2 = new int[4];
public bool collideRectRect(int[] rect1, int[] rect2)
{
if (rect1[RIGHT] < rect2[LEFT] ||
rect1[LEFT] > rect2[RIGHT] ||
rect1[TOP] < rect2[BOTTOM] ||
rect1[BOTTOM] > rect2[TOP])
{
label1.Text = "false";
return false;
}
else
{
label1.Text = "true";
return true;
}
}
[/code]
[/quote]

perhaps it's because your declaration is incorrect?
[code]
const int LEFT = 0;
const int TOP = 0;
const int RIGHT = 5;
const int BOTTOM = 3;
[/code]

to which means when you access rect1[RIGHT], you should be either crashing your program, or getting very bad results, accessing left/top will return the same value at the same location.
i assume you want something like this:
[code]
const int LEFT = 0;
const int TOP = 1;
const int RIGHT = 2;
const int BOTTOM = 3;
[/code]
0

Share this post


Link to post
Share on other sites
[quote name='NightCreature83' timestamp='1332547016' post='4924784']
For rectangles you are going to have to write a contains method and if one point of a rectangle lies within the area of another one it is colliding with it.[/quote]

Which would be wrong, because there are plenty of ways for rectangles to intersect without this being true (unless you have known restraints like all rectangles being the same size).

The existing code should be fine by simply testing for a separating axis (for axis oriented rectangles).

My cynical answer to none of the two code blocks being executed would probably be something like "it kind of helps to actually call the function". But apart from that, the code doesn't even make any sense and suggests a strong lack of understanding of basic language features like arrays (why else would the index for top and left be the same and why would the indices be that random in general?)

Which in turn hints at a common case of "I want to make a game but can't waste any time on actually learning how to program in my language of choice". I predict about as much success as an attempt to write a Spanish novel without speaking Spanish and trying to copy/paste from a dictionary.
0

Share this post


Link to post
Share on other sites
Since you haven't mentioned anything about the array out of bounds error, my money is on the "cynical answer", where the function isn't being called.

Unless... the whole thing is in a try block and the array out of bounds was being handled without any feedback. That would cause collideRectRect() to bail and never set the value of label1.Text.

As a side note, since you're returning true or false from the function anyway why not grab the return value and write that into label1.Text, instead of setting it within the function call?
0

Share this post


Link to post
Share on other sites
well I reworked my code.I have also done some reserch on arrays and functions.
[code]
const int LEFT = 0;
const int TOP = 1;
const int RIGHT = 2;
const int BOTTOM = 3;
int [] rect1 = new int[4] {0,4,1,0};
int [] rect2 = new int[4] {3,3,3,3};
bool collideRectRect(int[] rect1, int[] rect2)
{
if (rect1[2] < rect2[0] ||
rect1[0] > rect2[2] ||
rect1[1] < rect2[3] ||
rect1[3] > rect2[1])
{
label1.Text = "false";
return false;
}
else
{
label1.Text = "true";
return true;
}
}
[/code]
0

Share this post


Link to post
Share on other sites
[quote name='phil67rpg' timestamp='1332734624' post='4925250']
I have put this code into a console applicaiton but it still does not work, no output to the screen at all
[/quote]

Probably because you're not calling the function anywhere. If this is your whole program, you're going to need a main function ([url="http://en.wikipedia.org/wiki/Main_function"]http://en.wikipedia.org/wiki/Main_function[/url]) and you'll need to call your collision function from it.
0

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