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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - kintantee

Pages: [1]
1
General / Collision detection(Pong game)
« on: February 09, 2012, 08:05:56 pm »
Okay guys I found the problem and thank you for your help!

The problem occured because of the shape of my ball which is square. I was comparing the top left y-coordinate of the ball(for both vy > 0 and vy <0), however, I should have compared like fallowing:

if(vy > 0)
    compare lower-left corner(bouncerPosition.y + ballHeight_)

else if(vy < 0)
    compare top-left corner(bouncerPosition.y)

here is my code for solution:

Code: [Select]

if(velocity_.x < 0.0f)
{
if(bouncerPosition.x <= leftRacketPosition.x + racketWidth)
{
if(velocity_.y < 0.0f)
{
if(bouncerPosition.y <= leftRacketPosition.y + racketHeight &&
bouncerPosition.y >= leftRacketPosition.y)
{
return COLLIDING_WITH_LEFT_PADDLE;
}
}

else
{
if(bouncerPosition.y + ballHeight_ >= leftRacketPosition.y &&
  bouncerPosition.y + ballHeight_ <= leftRacketPosition.y + racketHeight)
{
return COLLIDING_WITH_LEFT_PADDLE;
}
}
}
}

else if(velocity_.x > 0.0f)
{
if(bouncerPosition.x + ballWidth_ >= rightRacketPosition.x)
{
if(velocity_.y < 0.0f)
{
if(bouncerPosition.y <= rightRacketPosition.y + racketHeight &&
  bouncerPosition.y >= rightRacketPosition.y)
{
return COLLIDING_WITH_RIGHT_PADDLE;
}
}

else
{
if(bouncerPosition.y + ballHeight_ >= rightRacketPosition.y &&
  bouncerPosition.y + ballHeight_ <= rightRacketPosition.y + racketHeight)
{
return COLLIDING_WITH_RIGHT_PADDLE;
}
}
}
}


Thank you again!

2
General / Collision detection(Pong game)
« on: February 08, 2012, 10:29:25 pm »
Quote from: "texus"
Quote
It's not detecting the paddles

Sorry, I misunderstood your sentence. I thought you meant that the problem did not lie with detecting the paddles.


Quote
the major problem is I can not detect collision.

You should be able to locate the problem a little better.
Debug your program: Is the detectCollision function returning NO_COLLISION? If so then check the values of the variables while performing the check.


It's only detecting the collision with right racket if vy < 0 and if it hits the upper side of the racket and vice versa for left racket : detects the collision if vy > 0 and if it hits the upper sides of the racket.

3
General / Collision detection(Pong game)
« on: February 08, 2012, 09:34:58 pm »
Quote from: "texus"
I still think that your formula is wrong. Lets just use numbers as an example.
leftRacketPosition.x = 10
bouncerPosition.x = 12
racketWidth = 5

If I understand it correctly then the fraction would have to be 3.
You are doing 12 - 10 + 5 = 7   (bouncerPosition.x - leftRacketPosition.x + racketWidth)
My code does 10 + 5 - 12 = 3   (leftRacketPosition.x + racketWidth - bouncerPosition.x)



I think I should put paranteses:

like fallowing:

-(12 - (10 + 5)) = 3

however, the major problem is I can not detect collision.

4
General / Collision detection(Pong game)
« on: February 08, 2012, 08:00:33 pm »
Quote from: "texus"
What exactly is going wrong? Does the ball go too far?


It's not detecting the paddles

Quote from: "texus"

I think your fraction is wrong with the left panel. I think you have to add racketWidth instead of subtracting it.
Code: [Select]
fraction = leftRacketPosition.x + racketWidth - bouncerPosition.x;



What I'm trying to acheieve there is that getting the fraction that is inside the racket. Since my ball is a square I'm extracting the left side of ball(assuming it's inside or atleast at the same coordinate) from the right side of the racket which is leftRacketPosition.x + racketWidth

something like this :


Quote from: "texus"

Why are you adding 1 when colliding with the left paddle? You don't substract 1 when colliding with the right paddle.


that was an experiment and didn't work. I'll fix it

5
General / Collision detection(Pong game)
« on: February 08, 2012, 05:35:47 pm »
Hi folks, I need your help with collision detection with my little sfml pong game.

First I can detect the collision with walls(window boundaries) however I can't do the same with rackets.  I just don't get where the mistake is...

as a background information, my ball is a square.

fallowing lines belong to collision detection function. Firstly I'm trying to understand if ball is going left and did it hit the racket horizontally. Then I control if it's indeed hit the racket by checking the coordinate vertically.

Code: [Select]

int Ball::detectCollision(Racket &leftRacket, Racket &rightRacket)
{
sf::Vector2f bouncerPosition = geometry_.GetPosition();
sf::Vector2f leftRacketPosition = leftRacket.getRacketPosition();
sf::Vector2f rightRacketPosition = rightRacket.getRacketPosition();

float racketWidth = leftRacket.getRacketWidth();
float racketHeight = leftRacket.getRacketHeight();



if(bouncerPosition.x <= leftRacketPosition.x + racketWidth && velocity_.x < 0.0f)
{
if(bouncerPosition.y + ballHeight_ >= leftRacketPosition.y && bouncerPosition.y <= leftRacketPosition.y + racketHeight)
{
return COLLIDING_WITH_LEFT_PADDLE;
}
}

else if(bouncerPosition.x + ballWidth_ >= rightRacketPosition.x && velocity_.x > 0.0f)
{
if(bouncerPosition.y + ballHeight_ >= rightRacketPosition.y && bouncerPosition.y <= rightRacketPosition.y + racketHeight)
{
return COLLIDING_WITH_RIGHT_PADDLE;
}
}

else
{
return NO_COLLISION;
}
}


and here is my process function for collision. In essence, what I'm trying to do is firstly changing the x velocity and then altering the ball position for possible infinite strucks in paddle.

Code: [Select]

void Ball::processCollision(const int collisionInformation, Racket &leftRacket, Racket &rightRacket)
{
sf::Vector2f bouncerPosition = geometry_.GetPosition();
sf::Vector2f leftRacketPosition = leftRacket.getRacketPosition();
sf::Vector2f rightRacketPosition = rightRacket.getRacketPosition();

float racketWidth = leftRacket.getRacketWidth();

float fraction = 0.0f;

if(collisionInformation == COLLIDING_WITH_LEFT_PADDLE)
{
velocity_.x = -velocity_.x;


fraction = -(bouncerPosition.x - leftRacketPosition.x + racketWidth);
bouncerPosition.x += fraction;
geometry_.SetPosition(bouncerPosition);
}

else if(collisionInformation == COLLIDING_WITH_RIGHT_PADDLE)
{
velocity_.x = -velocity_.x;

fraction = -(bouncerPosition.x + ballWidth_ - leftRacketPosition.x);
bouncerPosition.x += fraction;
geometry_.SetPosition(bouncerPosition);
}
}


and enumartion for collision types:
Code: [Select]

enum COLLISION_INFORMATION
{
NO_COLLISION,

COLLIDING_WITH_UPPER_WALL,
COLLIDING_WITH_LOWER_WALL,
COLLIDING_WITH_LEFT_WALL,
COLLIDING_WITH_RIGHT_WALL,

COLLIDING_WITH_LEFT_PADDLE,
COLLIDING_WITH_RIGHT_PADDLE
};


Finally my driving code in the main game loop:

Code: [Select]

bouncer.calculateDisplacement(deltaTime);


if(collidingEdge = bouncer.detectCollision(leftRacket, rightRacket))
{
bouncer.processCollision(collidingEdge, leftRacket, rightRacket);
bouncer.accelerate();
}

else
{
bouncer.bounce();
bouncer.tailAnimation();
}
}




Thank you in advance!

6
General / Cannot move shapes
« on: January 01, 2012, 12:59:57 pm »
Thank you for your tips and help, It really helped a lot. I got two more questions.

first one: I didn't get what do you mean with initializer list.

I made some research and if you mean something like this:

Code: [Select]

class SomeClass
{
public:
    SomeClass(): shape_(sf::Shape(required parameters)) {}
private:
    sf::Shape shape_
};


It didn't seem much different than what I do and I don't know if there is a difference. I would appreciate if you explain me difference.


second: Why I can't create objects with


Code: [Select]

shape_.Circle(required parameters);


it works when I do like fallowing:
Code: [Select]

shape_ = sf::Shape::Circle(req. params.)

7
General / Cannot move shapes
« on: December 31, 2011, 02:03:09 am »
I edited my post, made my points as clear as possible without showing bunch of codes. I really need help on this, I stuck on same point for 1,5 weeks. Thank you!

8
General / Re: Cannot move shapes
« on: December 28, 2011, 02:54:46 pm »
Quote from: "Nexus"
Quote from: "kintantee"
I will upload full soruce code as soon as possible.
Very bad idea, already the posted source code is far too big. You can't expect us to skim through it and to search for mistakes which you haven't even described. "I have some problems at fundamental parts such as moving an object" is no meaningful problem description.

I suggest to read this thread carefully, and then to post again, considering the points mentioned in the link.


thank you for tip. I was in kinda rush so I couldn't properly describe the problem. I will edit the topic as soon as possible.

9
General / Cannot move shapes
« on: December 28, 2011, 01:37:42 pm »
Hello,
I'm trying to make an Pong clone however I have some problems with constructing and moving static shapes like circle and rectangle.

Let's start with circle,

First of all I have an primitive class called Circle.

Circle class:

Code: [Select]

class Circle
{
public:
    Circle(const sf::Vector2f &position,
      const float         &radius,
           const sf::Color    &fillerColor,
           const float        &outlineThickness,
           const sf::Color    &outlineColor);

sf::Vector2f    getPosition() const;
float           getRadius() const;
const sf::Shape &getDrawableObject() const;

void draw(sf::RenderTarget &renderTarget);
void bounce(const sf::Vector2f &displacement);

private:
    sf::Vector2f position_;
    sf::Shape    shape_;
    float        radius_;
    sf::Color    fillerColor_;
    float        outlineThickness_;
    sf::Color    outlineColor_;
};



I'm constructing my Circle object like fallowing:

Code: [Select]

Circle::Circle(const sf::Vector2f &position,
               const float        &radius,
               const sf::Color    &fillerColor,
               const float        &outlineThickness,
               const sf::Color    &outlineColor)
{
    position_         = position;
    radius_           = radius;
    fillerColor_   = fillerColor;
    outlineThickness_ = outlineThickness;
    outlineColor_     = outlineColor;


    shape_ = sf::Shape::Circle(position_,
                               radius_,  
                               fillerColor_,  
                               outlineThickness_,
                               outlineColor_);
}


and here is my move function:

Code: [Select]

void Circle::bounce(const sf::Vector2f &displacement)
{
position_ += displacement;
shape_.SetPosition(position_);
}


now my question is when I use

Code: [Select]

shape_.SetCenter(position_);


instead of current one it doesn't work. Do you know why?

Also fallowing part fallowing part from constructor is not working as well

Code: [Select]

shape_ = sf::Shape::Circle(position_,
  radius_,
  fillerColor_,
  outlineThickness_,
  outlineColor_);


however if I change position_ with fallowing parameter

Code: [Select]

sf::Vector2f(radius_ / 2, radius_ / 2)

then it's working. I dunno why it's not working, on console the numbers seem right. I mean it's just wierd. By working, I mean it's drawing beucase with position_
parameter it's not showing on screen(or it's on some coordinate out of screen)

now my problem with Rectangle is something smilar

here is my Rectangle class:

Code: [Select]

class Rectangle
{
public:
    Rectangle(const sf::Vector2f &position,
              const float        &width,
              const float        &height,
              const sf::Color    &fillerColor,
              const float        &outlineThickness,
              const sf::Color    &outlineColor);

    sf::Vector2f    getPosition() const;
    float           getWidth() const;
    float           getHeight() const;
    const sf::Shape &getDrawableObject() const;

    void draw(sf::RenderTarget &renderTarget);
    void moveUp(const float &displacement);
    void moveDown(const float &displacement);

private:
    sf::Vector2f position_;
    float        width_;
    float        height_;
    sf::Color    fillerColor_;
    float        outlineThickness_;
    sf::Color    outlineColor_;
    sf::Shape    shape_;
};


I'm constructing my Rectangle object like fallowing:

Code: [Select]

Rectangle::Rectangle(const sf::Vector2f &position,
                     const float        &width,
                     const float        &height,
                     const sf::Color    &fillerColor,
                     const float        &outlineThickness,
                     const sf::Color    &outlineColor)
{
    position_         = position;
    width_            = width;
    height_           = height;
    fillerColor_      = fillerColor;
    outlineThickness_ = outlineThickness;
    outlineColor_     = outlineColor;

    sf::Vector2f lowerRightPoint;
    lowerRightPoint.x = position_.x + width;
    lowerRightPoint.y = position_.y + height;

    shape_ = sf::Shape::Rectangle(position_,
                                  lowerRightPoint,
                                  fillerColor_,
                                  outlineThickness_,
                                  outlineColor_);

}


My Rectangle objectets does not even drawn(or drawn but somewhere else, not in screen) I think there is something wrong with fallowing lines

Code: [Select]

    shape_ = sf::Shape::Rectangle(position_,
                                  lowerRightPoint,
                                  fillerColor_,
                                  outlineThickness_,
                                  outlineColor_);


I think it's position_ again but this time I couldn't find a way to show it on screen

is there any better way to create a static shape?

finally I'm trying to move my rectangle objects like fallowing:

Code: [Select]

void Rectangle::moveUp(const float &displacement)
{
    position_.y -= displacement;
    shape_.s
}

void Rectangle::moveDown(const float &displacement)
{
    position_.y += displacement;
    shape_.SetPosition(position_);
}


even I can't move it right now I can see a problem incoming which in above lines I only move the upper left point of rectangle(I assume).
does SetPosition take care of lower right point as well or do I need to add something else.

I hope I made my points clear this time.

Thank you in advance.

10
Graphics / Cannot draw shape
« on: December 24, 2011, 11:52:20 pm »
Since I'm not very familiar with SFML and C++ I probably picked a bad tittle but I hope you forgive me. My problem is that I'm trying to make a bouncing circle but I couldn't find where is my problem. Also do you think this game structure is appropriate for small games(I'm sure it's not). I really need to understand if I'm going in right way or not in terms of design. Anyway here is the code.

Code: [Select]

#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;

const unsigned int SCREEN_WIDTH = 640;
const unsigned int SCREEN_HEIGHT = 480;
const unsigned int BIT_PER_PIXEL = 32;
const unsigned int DEPTH_BITS = 24;
const unsigned int STENCIL_BITS = 8;
const unsigned int ANTI_ALIASING = 16;


struct Point2D
{
float x;
float y;
};

struct Primitive
{
Point2D   position;
float  radius;
sf::Color fillerColor;
float     outlineThickness;
sf::Color outlineColor;
};

class Ball
{
private:
Primitive circle;
Point2D   velocity;
Point2D   displacement;
sf::Color color;
sf::Shape shape;
public:
Ball();
void initialize(const Primitive&, const Point2D&);  
void detectAndProcessCollision();
sf::Shape getDrawableObject() const { return shape; }
void move(const float&);
};

Ball::Ball()
{
circle.position.x = 0.0f;
circle.position.y = 0.0f;
circle.radius = 0.0f;
circle.fillerColor = sf::Color(0, 0, 0);
circle.outlineThickness = 0.0f;
circle.outlineColor = sf::Color(0, 0, 0);
shape = sf::Shape::Circle(circle.position.x, circle.position.y, circle.radius,
 circle.fillerColor, circle.outlineThickness, circle.outlineColor);
}

void Ball::initialize(const Primitive &givenCircle, const Point2D &givenVelocity)
{
circle.position.x = givenCircle.position.x;
circle.position.y = givenCircle.position.y;
circle.radius = givenCircle.radius;
circle.fillerColor = givenCircle.fillerColor;
circle.outlineThickness = givenCircle.outlineThickness;
circle.outlineColor = givenCircle.outlineColor;

velocity.x = givenVelocity.x;
velocity.y = givenVelocity.y;

}

void Ball::move(const float &deltaTime)
{
displacement.x = deltaTime * velocity.x;
displacement.y = deltaTime * velocity.y;

circle.position.x += displacement.x;
circle.position.y += displacement.y;

std::cout << "x pos = " << circle.position.x << std::endl;
std::cout << "y pos = " << circle.position.y << std::endl;
shape.SetPosition(circle.position.x, circle.position.y);
}

void Ball::detectAndProcessCollision()
{
if((circle.position.y - circle.radius <= 0) && velocity.y < 0)
{
velocity.y = -velocity.y;
float outedFraction = -(circle.position.y - circle.radius);
circle.position.y += outedFraction;
}

else if((circle.position.y + circle.radius >= SCREEN_HEIGHT) && velocity.y > 0)
{
velocity.y = -velocity.y;
float outedFraction = -((circle.position.y - circle.radius) - SCREEN_HEIGHT);
circle.position.y += outedFraction;
}


if((circle.position.x - circle.radius <= 0) && velocity.x < 0)
{
velocity.x = -velocity.x;
float outedFraction = -(circle.position.x - circle.radius);
circle.position.x += outedFraction;
}
else if((circle.position.x + circle.radius >= SCREEN_WIDTH) && velocity.x > 0)
{
velocity.x = -velocity.x;
float outedFraction = -((circle.position.x - circle.radius) - SCREEN_WIDTH);
circle.position.x += outedFraction;
}
}



class Game
{
private:
sf::RenderWindow gameArea;
sf::VideoMode videoMode;
sf::WindowSettings windowSettings;
sf::Clock timer;
sf::Event gameEvents;
Ball bouncer;
bool isGameFinished;
public:
Game();
~Game();
void processEvents();
void draw() { gameArea.Draw(bouncer.getDrawableObject()); }
void main();
};

Game::Game(): videoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BIT_PER_PIXEL),
 windowSettings(DEPTH_BITS, STENCIL_BITS, ANTI_ALIASING)
{
isGameFinished = false;

Point2D initialVelocity;
initialVelocity.x = 30.0f;
initialVelocity.y = 30.0f;

Primitive tempCircle;
tempCircle.position.x = SCREEN_WIDTH / 2;
tempCircle.position.y = SCREEN_HEIGHT / 2;
tempCircle.radius = 10.0f;
tempCircle.fillerColor = sf::Color(255, 0, 255);
tempCircle.outlineThickness = 1;
tempCircle.outlineColor = sf::Color(230, 0, 0);

bouncer.initialize(tempCircle, initialVelocity);
gameArea.Create(videoMode, "Pong++", sf::Style::Close, windowSettings);
}

Game::~Game()
{
gameArea.Close();
}




void Game::main()
{
while(!isGameFinished)
{
gameArea.Clear();
processEvents();
draw();
gameArea.Display();
}
}

void Game::processEvents()
{
const sf::Input& input = gameArea.GetInput();
while (gameArea.GetEvent(gameEvents))
{
// Window closed
if (gameEvents.Type == sf::Event::Closed)
isGameFinished = true;
}
float deltaTime = timer.GetElapsedTime();
timer.Reset();
bouncer.move(deltaTime);
}



int main()
{
Game pong;
pong.main();
return EXIT_SUCCESS;
}




Thank you!

--edit--

I found few bugs in my code and changed it but it still does not show ball on the screen.

Pages: [1]