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

Java: Looking for tips on Square to Square stationary collision response.

1 post in this topic

[color=#FF0000][b]DISCLAIMER: WALL OF TEXT!![/b][/color]

Here's a diagram:

[img]http://i.imgur.com/uyz1w.png[/img]

I'll give out a little more information about the diagram. The bend I designed is basically squares within squares. The green ball you see above is controlled by an accelerometer, which it's not like Pong or Breakout (where an object moves in a straight line until it hits an obstacle.) This means that a different type of collision response is needed.

You can also see that I treat the green ball as a green box. At the bottom-most, right-most corner, I put a red square underneath the green object. It means when the green object touches the blue obstacle, it stops entering. This is my ultimate goal.

I do not want the object to bounce off the obstacle, as it's not my main intention. Rather, I want to stop the object from entering the obstacle, or as in other words, "stay outside of the obstacle, but the object can move alongside the obstacle wall".

And here's the code I came up with:

[CODE]
public void collideInner(Ball b) {
// width and height of the inner square is PIPE_SIZE *4

// Inner box
float width = PIPE_SIZE*4;
float height = PIPE_SIZE*4;
float left = dstRect.left;
float top = dstRect.top;
float tempX = b.position[0];
float tempY = b.position[1];

if (top + height > b.position[1] - b.radius){
if (left + width > b.position[0] - b.radius){
//b.position[0] = left + width + b.radius;
//b.position[1] = top + height + b.radius;
if (left + width <= b.position[0] - b.radius - b.speed[0]){
b.position[0] = left + width + b.radius;
b.position[1] = tempY;
}
Log.d("Corner", "Phase 2");
return;
}
else {
b.position[0] = left + width + b.radius;
b.position[1] = tempY;
Log.d("Corner", "Phase 1");
return;
}
}
else {
if (left + width > b.position[0] - b.radius){
//b.position[0] = tempX;
//b.position[1] = top + height + b.radius;
Log.d("Corner", "Phase 3");
return;
}
else {
//b.position[0] = tempX;
//b.position[1] = tempY;
Log.d("Corner", "Phase 4");
return;
}
}

}
[/CODE]

[font=courier new,courier,monospace]dstRect [font=arial,helvetica,sans-serif]is a class that contains the margin size from all 4 sides of the monitor screen. The members are type float, each named as [font=courier new,courier,monospace]top, bottom, left, [font=arial,helvetica,sans-serif]and [font=courier new,courier,monospace]right. [font=arial,helvetica,sans-serif]When [/font]top [font="Arial"]= 4, it means that the distance from the top of the monitor screen is 4.0 pixels away. And so forth for the rest of the members.[/font][/font][/font][/font][/font][/font]

I split the logic up into 4 equal parts, Phase 1 through 4, each symbolizing the respective quadrants, I, II, III, and IV. From there, I used the design where the obstacle is located in the quadrant II portion of the bend.

The main problem in my code, is that I am unable to keep the object outside of the obstacle. It either moves freely into the obstacle, or it jumps top to down, or left to right. It will never ceases to jump around.

Therefore, I'm looking for tips/hints. If anyone has finished reading this sentence, you are god among many other gods! I'm sorry for the wall of text. Thanks in advance.
0

Share this post


Link to post
Share on other sites
I'm not sure if I understanding entirely the game mechanics you are going for, so forgive me. It seems like you're struggling with basic concepts of movement and collision response, and I think there is a much simpler (and more stable) way of handling this. Below is what I've implemented in my simple 2D game project. It's pretty straightforward. It may not work so well in more complex physics simulations, but for simple stuff it's okay.
[list=1]
[*]Instead of trying to prevent two objects from overlapping, let them. This overlap is your collision area. The area has a size and shape (which is, in this case, just another rectangle). I'll explain below how to deal with this.
[*]When your ball moves through 2D space, it should have an X velocity and a Y velocity. These values indicate how far (in pixels, for example) your ball needs to move on a given axis per update. Various forces (gravity, user input, etc.) can change these values.
[*]When you do a game update, move one axis at a time. Check for collisions on that axis, respond to any that occurred, and do the next axis. (It's doesn't really matter which axis you do first, except in some extreme cases. For now, don't worry about it.)
[*]When you move on a given axis, the moving object has either a positive or negative velocity on that axis. The sign of this values tells you which direction the object is moving (up vs. down on the Y axis, left vs. right on the X axis).
[*]For each object that your moving object could possibly collide with, check for an intersection. Hint: If you don't want to code this yourself, have a look at http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Rectangle2D.Double.html#createIntersection(java.awt.geom.Rectangle2D).
[*]As I mentioned before, each intersection has a size. This tells you how much your moving object is overlapping with the object it hit.
[*]Given this size, you know exactly how far you need to move (in the opposite direction) to get out of the intersection. Again, the axis is relevant.
[/list]
This method of responding to collisions (called "projection") is briefly mentioned in this article: http://www.metanetsoftware.com/technique/tutorialA.html#section0

[quote]
Projection [Jakobsen] is one physically-plausible method for dealing with overlapping ("penetrating") objects. The basic idea is to move the objects out of penetration using the smallest possible displacement. Solving the collision then becomes equivalent to finding the vector v which moves the two objects out of penetration by the shortest distance possible.
[/quote]

Hope that helps.
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