Good Evening!
I'm back with another question that hopefully someone can help me out with. This week I was required to create a ball that would be launched from the bottom left corner of the screen, and continuously bounce off the surfaces of the screen. The longer I hold Spacebar, the faster the ball would get. I managed to complete my assignment a lot quicker than I thought I would, so I thought I would contact my teacher and see if I can't do something extra. He told me to add Gravity to my code. This is all he gave me.
Implement gravity into your ball movement:
- This will apply acceleration to your physics now.
- Remember, Velocity is the change in position per second, while acceleration is the change in Velocity per second.
- Gravity in our "Game" should be between 500 - 1500 Pixels/s^2
I have been trying to do this for a couple of days now, however I have been unable to implement it into my code. I know their are multiple ways of doing everything in coding, but from everything I have seen online and on tutorial videos, none of the methods have been working. So, I was hoping someone here has run into the same problems as me, and will be able to help me out.
Thank you all in advance for taking the time to look at my code, and helping me out!
// Engine.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
void applyGravity(float, float);
float yVelocity;
int main()
{
float Power = 0.0f;
bool toMove = false;
float Time;
sf::RenderWindow window(sf::VideoMode(1024, 512), "Welcome to SDVA 203!");
sf::CircleShape shape(25);
shape.setPosition(0, 486);
shape.setOrigin(shape.getRadius(), shape.getRadius());
shape.setFillColor(sf::Color::White);
sf::Clock clock;
float angleInDegrees = 225;
float angleInRadians = angleInDegrees * 2 * 3.1415 / 360;
float xVelocity = cos(angleInRadians) * 100;
yVelocity = sin(angleInRadians) * 100;
while (window.isOpen())
{
sf::Time dt = clock.restart();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyReleased)
{
if (event.key.code == sf::Keyboard::Space)
{
toMove = true;
}
}
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Num1)
{
xVelocity = cos(angleInRadians) * 100;
yVelocity = sin(angleInRadians) * 100;
}
if (event.key.code == sf::Keyboard::Num2)
{
xVelocity = cos(angleInRadians) * 300;
yVelocity = sin(angleInRadians) * 300;
}
if (event.key.code == sf::Keyboard::Num3)
{
xVelocity = cos(angleInRadians) * 600;
yVelocity = sin(angleInRadians) * 600;
}
if (event.key.code == sf::Keyboard::Num4)
{
xVelocity = cos(angleInRadians) * 1200;
yVelocity = sin(angleInRadians) * 1200;
}
if (event.key.code == sf::Keyboard::Space)
{
toMove = false;
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
Power += dt.asSeconds();
if (Power > 2.0f)
{
xVelocity = cos(angleInRadians) * 100;
yVelocity = sin(angleInRadians) * 100;
std::cout << "Power Level is at 100!" << std::endl;
}
if (Power > 4.0f)
{
xVelocity = cos(angleInRadians) * 300;
yVelocity = sin(angleInRadians) * 300;
std::cout << "Power Level is at 300!" << std::endl;
}
if (Power > 6.0f)
{
xVelocity = cos(angleInRadians) * 600;
yVelocity = sin(angleInRadians) * 600;
std::cout << "Power Level is at 600!" << std::endl;
}
if (Power > 8.0f)
{
xVelocity = cos(angleInRadians) * 1200;
yVelocity = sin(angleInRadians) * 1200;
std::cout << "Power Level is at 1200!" << std::endl;
}
if (Power > 10.0f)
{
xVelocity = cos(angleInRadians) * 9001;
yVelocity = sin(angleInRadians) * 9001;
std::cout << "Power Level Over 9000!" << std::endl;
}
}
if (toMove == true)
{
shape.move(xVelocity * dt.asSeconds(), yVelocity * dt.asSeconds());
cout << xVelocity << yVelocity << endl;
applyGravity(500.0, 500.0);
}
sf::Vector2f position = shape.getPosition();
if (position.y <= shape.getRadius())
{
yVelocity *= -1;
shape.setPosition(position.x, shape.getRadius());
}
if (shape.getRadius() + position.y >= 512)
{
yVelocity *= -1;
shape.setPosition(position.x, 512 - shape.getRadius());
}
if (position.x <= shape.getRadius())
{
xVelocity *= -1;
shape.setPosition(shape.getRadius(), position.y);
}
if (shape.getRadius() + position.x >= 1024)
{
xVelocity *= -1;
shape.setPosition(1024 - shape.getRadius(), position.y);
}
if (shape.getRadius() + position.y >= 512)
{
yVelocity = yVelocity / 2.0f;
}
//std::cout << "Ball position is " << position.x <<","<<position.y << std::endl;
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
void applyGravity(float gravity, float time)
{
yVelocity = yVelocity + (gravity * time);
if (yVelocity < .01)
{
yVelocity = 0;
}
}