Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Football Ball  (Read 3199 times)

0 Members and 1 Guest are viewing this topic.

Njifra

  • Guest
Football Ball
« on: December 11, 2013, 10:18:48 pm »
Well I'm wondering how to make football ball psychic.
Any kind of help would be awesome.

Greetings,

Njifra.

[EDIT] It is side view game.
« Last Edit: December 11, 2013, 10:30:39 pm by Njifra »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Football Ball
« Reply #1 on: December 11, 2013, 10:32:26 pm »
I think there you will find everything you want: http://bit.ly/18DCfD1

If not come here again with more specific questions.


AlexAUT

Njifra

  • Guest
Re: Football Ball
« Reply #2 on: December 11, 2013, 10:48:17 pm »
Meh...

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Football Ball
« Reply #3 on: December 11, 2013, 10:54:59 pm »
Did you mean psychic  (ai or something like that) or physics?

Sorry if I missread that.


AlexAUT

Njifra

  • Guest
Re: Football Ball
« Reply #4 on: December 15, 2013, 07:44:48 pm »

Something like this, but more bouncy...

Njifra

  • Guest
Re: Football Ball
« Reply #5 on: December 15, 2013, 09:46:30 pm »
I tried everything to make this more bouncy... but imposs :(
Please, help, and sorry for spam :(

// Football Head Champions.cpp : main project file.

#include "stdafx.h"
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <cmath>

using namespace System;

int main(array<System::String ^> ^args)
{
        bool isPlaying;
        sf::RenderWindow window;
        window.create(sf::VideoMode(800, 600), L"Building Blocks");

    float gravity = 0.6;
    float tempV;
    int ypos;
    int i = 2;
    bool collision = false;    
    float v;
    float time;
        sf::Clock clock;

        sf::Texture ballTXT;
    ballTXT.loadFromFile("data/ball.png");
    sf::Sprite ball;
    ball.setTexture(ballTXT);
    ball.setOrigin(ball.getGlobalBounds().width / 2, ball.getGlobalBounds().height / 2);
    ball.setPosition(400, 400);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        ball.move(0, 1.0f);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        ball.move(0, -1.0f);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        ball.move(1.0f, 0);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        ball.move(-1.0f, 0);
                }

                time = clock.getElapsedTime().asSeconds();
                if(collision == false)
                {
                        v = gravity * time;
                }
                ball.move(0,v);
                ypos = ball.getPosition().y;
                if(ball.getPosition().y + ball.getGlobalBounds().height >= 580)
                {
                        collision = true;
                        tempV = v;
                        clock.restart();
                        ball.setPosition(ball.getPosition().x, 579.9999 - ball.getGlobalBounds().height);
                        i++;
                }
                time = clock.getElapsedTime().asSeconds();
                if(collision == true)
                {
                        v = -((1 * tempV) / 2 - gravity * time);
                        if(-v <= 0)
                        {
                                collision = false;
                        }
                }
                window.clear(sf::Color(0, 50, 180));
                window.draw(ball);
                window.display();
        }
    return 0;
}
 

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Football Ball
« Reply #6 on: December 15, 2013, 11:48:59 pm »
A good tip: use the debugger. It makes it easy to see what happens for each line. Also, try to organize your code. Do one thing at the time. You start the movement of the ball by checking the bool collision, and only afterwards you evaluate what collision actually needs to be. And then you again move your ball.

You'll need the laws of physics to make your ball behave correctly. Here is the correct formula you need to use:

where x is the new position, a is the acceleration, v0 is the starting speed, x0 is the starting position and t is the total time elapsed. This is a 1D function, only focusing on 1 axis. Mind that if you want to use sf::Sprite::move you'll need to adapt this function.
You can also make use of the derivative of the function giving you the current speed:


If you need further guidance in physics, just ask. Please do give a minimal working example instead of your complete code: remove every line that is not part of the problem: window creation, event handlers, etc. And ask specific and clear questions. The more specific and clear your questions, the more likely someone will answer them and give you what you need.