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

Pong Development, Confusing with the Bouncing *After Collision <SFML 2.0>

3 posts in this topic

 

 

#include "Engine.h"
//Screen Resolution properties for proper values among positioning for renders.
sf::Vector2f Screen = sf::Vector2f(800,600);
//************************Constructor****************************
Engine::Engine()
{
//
}
//************************Deconstructor****************************
Engine::~Engine()
{
//
}
//************************What is rendered upon initialization****************************
bool Engine::init()
{
//Point to the window and create one for our use.
window = new sf::RenderWindow(sf::VideoMode(Screen.x,Screen.y,32), "Pong");
//Define the player Shapes
defineShapes();
//if the window fails return a negative response
if(!window)
return false;
//else continue
return true;
}
//************************Determine what keys perform Events****************************
void Engine::ProcessEvents()
{
sf::Event event;
//perform Events while the window is open.
while(window->pollEvent(event))
{
switch(event.type)
{
//Upon the selection of the close buttons on a window Close the window
case sf::Event::Closed:
window->close();
break;
//upon a Key that is pressed close the window
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Escape)
window->close();
break;


}


}


//--------------------------Call upon player Functions---------------------------


//movement settings for the first rectangle on the Left
movePlayerOne();
//movement settings for the Second rectangle on the Right
movePlayerTwo();
//Determine Collison factor and move the ball
movCollision();


}
//************************Draw to the Screen [Update]****************************
void Engine::Update()
{
//clear the window
window->clear(sf::Color(40,40,40));
//draw the first rectangle on the Left
window->draw(*playerOne);
//draw the Second rectangle on the Right
window->draw(*playerTwo);
//draw the Ball in the Center
window->draw(*Ball);


}
//************************Window Display Settings****************************
void Engine::RenderFrame()
{


window->display();


}
//************************Determine the actions after Start****************************
void Engine::Main()
{
while(window->isOpen())
{
ProcessEvents();
Update();
RenderFrame();
}
}
//************************Start the Engine****************************
void Engine::Go()
{
//if it doesn't initialize send an error message
if(!init())
throw "Could not initialize game loop";
//continue to main loop
Main();


}
//************************Movement for Player One****************************
void Engine::movePlayerOne()
{
//set the speed of the player
int speed = 10000;
//Create a clock
sf::Clock clock;
//Restart the clock
clock.restart();
//if W is pressed on the keyboard move Up
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
playerOne->move(0,clock.getElapsedTime().asSeconds() * -speed);
}
//if S is pressed on the Keyboard move Down
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
playerOne->move(0,clock.getElapsedTime().asSeconds() * speed);
}


}
//************************Movement for Player Two****************************
void Engine::movePlayerTwo()
{
//set the speed of the player 2
int speed = 10000;
//Create another clock
sf::Clock clock;
//Restart the clock
clock.restart();
//if Up is pressed on the Keyboard Move Up
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
playerTwo->move(0,clock.getElapsedTime().asSeconds() * -speed);
}
//if Down is pressed on the Keyboard Move Down
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
playerTwo->move(0,clock.getElapsedTime().asSeconds() * speed);
}


}




//************************Begin Defining Shapes****************************
void Engine::defineShapes()
{


//Define Player One Rectangle
playerOne = new sf::RectangleShape();
//Set the Player One Rectangle Size
playerOne->setSize(sf::Vector2f(16,64));
//Set the Player One Rectangle Color
playerOne->setFillColor(sf::Color(255,255,255,255));
//Set the Player One Rectangle Position
playerOne->setPosition(sf::Vector2f(40,20));


//Define Player Two Rectangle
playerTwo = new sf::RectangleShape();
//Set the Player Two Rectangle Size
playerTwo->setSize(sf::Vector2f(16,64));
//Set the Player Two Rectangle Color
playerTwo->setFillColor(sf::Color(255,255,255,255));
//Set the Player Two Rectangle Position
playerTwo->setPosition(sf::Vector2f(740,520));


//Define Ball
Ball = new sf::RectangleShape();
//Set the Ball Size
Ball->setSize(sf::Vector2f(16,16));
//Set the Ball Color
Ball->setFillColor(sf::Color(0,255,0,255));
//Set the Ball Position
Ball->setPosition(740, 520);
}




//************************Begin Defining Collision for the Ball and Players****************************
void Engine::movCollision()
{


//Store Player Positions into Vectors
sf::Vector2f playerOnePosition = sf::Vector2f(playerOne->getPosition());


sf::Vector2f playerTwoPosition = sf::Vector2f(playerTwo->getPosition());


sf::Vector2f BallPosition = sf::Vector2f(Ball->getPosition());


//Define Rectangle around the players
sf::IntRect playerOneRect(playerOnePosition.x,playerOnePosition.y, playerOne->getSize().x, playerOne->getSize().y);


sf::IntRect playerTwoRect(playerTwoPosition.x,playerTwoPosition.y, playerTwo->getSize().x, playerTwo->getSize().y);


sf::IntRect BallRect(BallPosition.x,BallPosition.y, Ball->getSize().x, Ball->getSize().y);


//set the clock for speed
sf::Clock clock;


//Restart the clock
clock.restart();








//determine position and velocity.
sf::Vector2f pos;
sf::Vector2f velocity;


velocity.x = clock.getElapsedTime().asSeconds() * -12000;
velocity.y = clock.getElapsedTime().asSeconds() * -600;


//Detect Collison
bool collisionPlayerTwo = BallRect.intersects(playerTwoRect);
bool collisionPlayerOne = BallRect.intersects(playerOneRect);


//If Collision 
if(collisionPlayerTwo && !collisionPlayerOne)
velocity.x = velocity.x; 


if(collisionPlayerOne && !collisionPlayerTwo)
velocity.x = -velocity.x;


pos += velocity;
Ball->move(pos);






}
 

 

 
Edited by Jdfskitz
0

Share this post


Link to post
Share on other sites

You're not handling pHit correctly. Firstly, you're not initializing it. It does get set either way, but it gets set incorrectly because you're fiddling with it instead of initializing it. 

 

Secondly, there are three possible collision cases here:

1) Ball hit paddle one.

2) Ball hit paddle two.

3) Ball didn't hit anything.

 

You need to handle all three cases.

 

Does

 

Ball->move(?, ?)

 

Change the position of the ball or its direction?

0

Share this post


Link to post
Share on other sites

both. I originally had 3 cases but I switched it to try and just get a half of the system working at least but failed. can you show me how I might properly initialize it? sorry, I'm fairly new to SFML and my C++ knowledge is kinda well, Everywhere. this is certainly teaching me the use of pointers finally though, hard as heck to understand from the books demonstrations for C++, but I can't really figure this one out :\ I suppose I may be able to go about this a different route.

 

instead of using ball->move(x,y);

 

I could use ball->setPosition(x++,y++) || (x--,y--);

 

I'm not even attempting for the randomness right now. All I am trying to achieve is a bounce.

0

Share this post


Link to post
Share on other sites

If you are able to detect which sort of collision occurs, then all you have to do is reverse the velocity on that specific axis. You can achieve this by always adding velocity to position, then, if a collision occurs, multiply that value by negative one to reverse the sign of that speed.

 

After this you can implement a speed multiplier to make it go faster, but that can be something to research. ;)

 

Oh and there is also collision with the pong arena boundaries to handle.

 

Hope this helps,

 

Stitchs.

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