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

Code Help - Pong

3 posts in this topic

Hey fellow aspiring gamedevs,

I am having a weird problem with my Pong game, I am sure my it's something wrong with my update but I don't know what. The ball goes along the same path and it doesn't bounce when it hits a wall. This is is my ball.cs Class and I am trying to get it to bounce of the walls and the player.cs Class.

[source lang="csharp"] public void CheckForCollisonWithWall()
{
if (position.X < 0)
{
position.X = 0;
motion.Y *= -1;
}
if (position.X + texture.Width > screenBounds.Width)
{
position.X = screenBounds.Width - texture.Width;
motion.Y *= -1;
}
if (position.Y < 0)
{
position.Y = 0;

}
if (position.Y + texture.Height > screenBounds.Height)
{
position.Y = screenBounds.Height - texture.Height;
}
}[/source]
-----------------------------------------------------------------
More of my Code
[source lang="csharp"] public void CheckForCollisionWithPaddle(Rectangle paddleLocation)
{
Rectangle ballLocation = new Rectangle(
(int)position.X, (int)position.Y, texture.Width, texture.Height);
if (paddleLocation.Intersects(ballLocation))
{
position.Y = paddleLocation.Y - texture.Height;
motion.Y *= -1;
}
}[/source]

Anyone have any ideas on what I am doing wrong?
0

Share this post


Link to post
Share on other sites
I see a 'motion' vector which I would take to mean your velocity.

Merely altering the position of the ball on collision will do nothing in changing the velocity (which determines the moving direction) of the ball.

I'm unclear on how you're determining how the ball moves through the game world. Are you simply adding to position every update tick or are you doing proper movement with distance = velocity * time (assuming constant velocity since this is pong)?

When the ball collides with a surface, you will need to [b]reflect[/b] the velocity vector. Merely changing the sign from negative to positive or vice versa will give you weird results. You would notice this any time the ball is going in a diagonal motion. XNA vectors provide a [url="http://msdn.microsoft.com/en-us/library/bb975900%28v=xnagamestudio.20%29.aspx"]Reflect method[/url] to do just that. Edited by caldiar
0

Share this post


Link to post
Share on other sites
When you're checking for collision with X variables (the sides of the box) you should reverse the X motion, currently you're reversing the Y motion. This means that if the ball hits either side all that will happen is that the ball bounces back and forth up and down as it continues to move off screen to the left or right.

You haven't changed the velocity of the ball for collision with the top or bottom of the screen at all, so there won't be any effect there.

Based on your code I'm thinking the solution should look something like this:

[CODE]
public void CheckForCollisonWithWall()
{
if (position.X < 0)
{
position.X = 0;
motion.X *= -1;
}
if (position.X + texture.Width > screenBounds.Width)
{
position.X = screenBounds.Width - texture.Width;
motion.X *= -1;
}
if (position.Y < 0)
{
position.Y = 0;
motion.Y *= -1;

}
if (position.Y + texture.Height > screenBounds.Height)
{
position.Y = screenBounds.Height - texture.Height;
motion.Y *= -1;

}
}
[/CODE]

Assuming that the paddles are on the top and bottom of the screen, then you're correctly reversing the Y speed. If the paddles are on the left and right then you need to reverse the X speed. Looking at it I presume the paddles are on the top and bottom and you didn't include collision with the top and bottom of the screen since that would mean someone has scored a point. Though I don't know why you are resetting the position of the ball in that case.

[quote name='caldiar' timestamp='1349461061' post='4987196']
I'm unclear on how you're determining how the ball moves through the game world. Are you simply adding to position every update tick or are you doing proper movement with distance = velocity * time (assuming constant velocity since this is pong)?

When the ball collides with a surface, you will need to reflect the velocity vector. Merely changing the sign from negative to positive or vice versa will give you weird results.
[/quote]

I don't think he's using a constant speed vector. I'm assuming that he's using two variables for X and Y speed and incrementing them onto the ball for each tick to move the ball. If that's true then just reversing the sign of the appropriate variable would produce correct motion.

Hopefully the assumptions I've made are close to true and something here helps. Let us know how you get on.
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