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 - collechess

Pages: [1]
1
General / Re: Rotated Rectangle Collision Implementation
« on: September 07, 2013, 10:09:22 pm »
I had believed that the problem was with the values that I had been inputting, as after I rotated the rectangles, I did not reassign them as top-left, top-right, etc.  However, apparently that was not the case, as it still doesn't work, and registers a collision with basically any values.

For example, A (219, 128) (232, 119) (241, 132) (228, 141) and B (246, 246) (254, 246) (246, 254) (254, 254) registers a collision.

So I once again have no idea what's wrong with the code.

2
General / Re: Rotated Rectangle Collision Implementation
« on: September 03, 2013, 01:32:33 am »
For example, when Rect A is equal to [(242, 133), (228, 142), (233, 120), (219, 129)] and Rect B is equal to [(246, 246), (254, 246), (246, 254), (254, 254)] it registers a collision.  It computes the axes to be (-13, 9), (9, 13), (0, -8), (-8, 0), which I believe is correct.

3
General / Rotated Rectangle Collision Implementation
« on: September 03, 2013, 12:47:48 am »
Recently I've had to implement collision detection between rotated rectangle.  Following this algorithm, http://www.gamedev.net/page/resources/_/technical/game-programming/2d-rotated-rectangle-collision-r2604, I created a function to take the four corners of two rectangles and return a bool as to whether or not they collide.  However, for some reason it registers collisions even when no collisions occur.


bool Game_State::RotatedRectCollision(sf::Vector2f UL1, sf::Vector2f UR1, sf::Vector2f LL1, sf::Vector2f LR1, sf::Vector2f UL2, sf::Vector2f UR2, sf::Vector2f LL2, sf::Vector2f LR2)
{
        sf::Vector2f RectA[4];
        sf::Vector2f RectB[4];
       
        RectA[0] = UL1;
        RectA[1] = UR1;
        RectA[2] = LL1;
        RectA[3] = LR1;
       
        RectB[0] = UL2;
        RectB[1] = UR2;
        RectB[2] = LL2;
        RectB[3] = LR2;
       
        sf::Vector2f Axes[4];
        Axes[0] = sf::Vector2f(UR1.x - UL1.x, UR1.y - UL1.y);
        Axes[1] = sf::Vector2f(UR1.x - LR1.x, UR1.y - LR1.y);
        Axes[2] = sf::Vector2f(UL2.x - LL2.x, UL2.y - LL2.y);
        Axes[3] = sf::Vector2f(UL2.x - UR2.x, UL2.y - UR2.y);

        float ScalarVal[8];

        for(int i = 0; i < 4; i++)
        {
                sf::Vector2f Proj[8];
                int curProj = 0;

                for(int j = 0; j < 4; j++)
                {
                        Proj[curProj].x = Axes[i].x * (RectA[j].x * Axes[i].x + RectA[j].y * Axes[i].y)/(Axes[i].x * Axes[i].x + Axes[i].y * Axes[i].y);
                        Proj[curProj].y = Axes[i].y * Proj[curProj].x / Axes[i].x;

                        curProj++;

                        Proj[curProj].x = Axes[i].x * (RectB[j].x * Axes[i].x + RectB[j].y * Axes[i].y)/(Axes[i].x * Axes[i].x + Axes[i].y * Axes[i].y);
                        Proj[curProj].y = Axes[i].y * Proj[curProj].x / Axes[i].x;

                        curProj++;
       
                }

                for(int j = 0; j < 8; j++)
                {
                        ScalarVal[j] = Proj[j].x * Axes[i].x + Proj[j].y * Axes[i].y;
                }
               
                float min_a = ScalarVal[0];
                float min_b = ScalarVal[4];
                float max_a = ScalarVal[0];
                float max_b = ScalarVal[4];

                for(int j = 1; j < 4; j++)
                {
                        min_a = std::min(min_a, ScalarVal[j]);
                        min_b = std::min(min_b, ScalarVal[j + 4]);
                        max_a = std::max(max_a, ScalarVal[j]);
                        max_b = std::max(max_b, ScalarVal[j + 4]);

                }

                if((min_b > min_a) && (max_b < min_a))
                {
                        return false;
                }
        }

        return true;

}

4
General / Re: Rotating Help
« on: September 01, 2013, 01:58:13 am »
Ah, I was accidentally applying the Transform before rotating it.  However, I changed it and it still has the same problem.  Even if I change the rotation to some other value, such as 45, it still does not rotate.

float offset_x = collide_rect.width / 2 + collide_rect.left;
float offset_y = collide_rect.height / 2 + collide_rect.top;
       
sf::Transform t;
t.rotate(sprite.getRotation(), offset_x, offset_y);
       
for(int i = 0; i < 4; i++)
{
        t.transformPoint(collide_box[i]);
                       
}

5
General / Re: Rotating Help
« on: September 01, 2013, 01:12:56 am »
Quote
float rad = sprite.getRotation() / 180 * PI;

this looks wrong.
Unless your trying to turn from rad to deg, but sprite.getRoation() doesn't return radians.
Isn't the equation for transforming degrees to radians * PI/180?
Quote
However I still suggest to use sf::Transform (nice interface to a 3D matrix) or even create a class and derive from sf::Transformable, so you can apply the rotation in an easy way and reuse the transformation for all the different points. Just look it up in the documentation/tutorial, it's really simple to use. :)

I'm trying to use sf::Transform, but I don't think I quite understand how it works.
        collide_box[0] = sf::Vector2f(collide_rect.left, collide_rect.top);
collide_box[1] = sf::Vector2f(collide_rect.left + collide_rect.width, collide_rect.top);
collide_box[3] = sf::Vector2f(collide_rect.left, collide_rect.top + collide_rect.height);
collide_box[2] = sf::Vector2f(collide_rect.left + collide_rect.width, collide_rect.top + collide_rect.height);

float offset_x = collide_rect.width / 2 + collide_rect.left;
float offset_y = collide_rect.height / 2 + collide_rect.top;
float rad = sprite.getRotation() * PI / 180;
       
for(int i = 0; i < 4; i++)
{
        sf::Transform t;
        t.transformPoint(collide_box[i]);
        t.rotate(rad, offset_x, offset_y);
}
This is just giving me a point outside of the rectangle.

6
General / Re: Rotating Help
« on: August 31, 2013, 10:34:43 pm »
I am using this rotated array in order to check for collisions between sprites.  I don't know how else I would be able to get the rotated rectangle's coordinates.

As for why it doesn't work, when I draw lines between the points, they stretch to parallelograms.   

7
General / Rotating Help
« on: August 31, 2013, 10:04:28 pm »
I have been trying to rotate an array of four points that represent a rectangle to match a rotated rectangle sprite.  This is the function I've been using, but it doesn't seem to be working. 

collide_box[0] = sf::Vector2f(collide_rect.left, collide_rect.top);
collide_box[1] = sf::Vector2f(collide_rect.left + collide_rect.width, collide_rect.top);
collide_box[2] = sf::Vector2f(collide_rect.left, collide_rect.top + collide_rect.height);
collide_box[3] = sf::Vector2f(collide_rect.left + collide_rect.width, collide_rect.top + collide_rect.height);
       
float offset_x = collide_rect.width / 2 + collide_rect.left;
float offset_y = collide_rect.height / 2 + collide_rect.top;

float rad = sprite.getRotation() / 180 * PI;

for(int i = 0; i < 4; i++)
{
        collide_box[i].x -= offset_x;
        collide_box[i].y -= offset_y;

        collide_box[i].x = collide_box[i].x * std::cos(rad) - collide_box[i].y * std::sin(rad);
        collide_box[i].y = collide_box[i].x * std::sin(rad) + collide_box[i].y * std::cos(rad);
               
        collide_box[i].x += offset_x;
        collide_box[i].y += offset_y;

}
 

Thank you.

8
General / Release Build Has No Graphics
« on: May 23, 2013, 10:49:22 pm »
Let me first say that I'm not sure if my problem is with SFML or Visual C++ 2010.  I'm hoping somebody can help me either way  :).  I have an SFML program that works fine in Debug, and even after building Release and running it from Visual C++ it runs fine.  However, when I run the Release .exe in its location, without the IDE, the graphics do not show up at all.    I have changed the SFML .libs to static, and have added the only image file to the Resource files (I'm not sure if this is needed).  Any solutions?

9
Graphics / Re: Problem with Returning Sprite
« on: May 23, 2013, 08:59:35 pm »
Thank you so much, I really feel like an idiot now.

10
Graphics / Problem with Returning Sprite
« on: May 23, 2013, 08:27:36 pm »
I just recently got back into coding c++ after about a year's hiatus, so this may well be a basic mistake.  I decided to drop SDL and I am starting to learn SFML.  I created a basic Pong game, but when I tried to implement classes it didn't work.  I tried to simplify the problem by just getting a class to represent an image, however I keep raising an error.  I've torn my hair out trying to fix this, but I think I've reached a point where even if the solution was at first obvious, I will not find it.  The program only encompasses 3 files.

main.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <windows.h>
#include "ball.h"



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)
{
    sf::RenderWindow window(sf::VideoMode(200, 300), "Test");
   Ball ball;
       
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(ball.get_Sprite());
        window.display();
    }

    return 0;
}

ball.h

#ifndef BALL_H
#define BALL_H
#include "SFML\Graphics.hpp"

class Ball
{
private:
        sf::Texture texture;
        sf::Sprite sprite;

public:
        Ball();
        sf::Sprite get_Sprite();
       

};


#endif

ball.cpp

#include "ball.h"
#include "SFML\Graphics.hpp"

Ball::Ball()
{
        texture.loadFromFile("ball_img.png");
        sprite.setTexture(texture);
}

sf::Sprite get_Sprite()
{
        return sprite;
}

The error returned is ball.cpp(12): error C2065: 'sprite' : undeclared identifier.  Thank you.

Pages: [1]