Hello, I made a function which take Sprite and fill dynamic allocated 2D bool array with 1 or 0 depends on if Alpha channel is bigger than 0.f or equals 0.f.
1 = bigger than 0.f, 0 = equals 0.f. It works great.
Now I still trying to figure way to check collision depend between two rectangles two sprites. First I need to change two sprites into rectangles and check for intersects, if intersects exist then I fill overlaping rectangle with new sfIntRect and check there for pixel collision? But how do I check it? I change it's global coordinates into local and run throught 2D array and if it gets to 1*1 then return collision happend?
Plus I got problem with views:
When I don't use view sfIntRect_Intersects checks collision perfectly but if I use view then it's showing that two rectangles collide no metter where I am except being before the rectangle, minimal code:
extern "C"
{
//#include <SFML/System.h>
#include <SFML/Graphics.h>
#include <SFML/Window.h>
#include "AniSprite.h"
#include "Kolizje.h"
}
#include <iostream>
#include <stdlib.h>
void Instaluj(sfSprite* duszek, sfImage* zdjecie, char* sciezka);
char* tabela[4] = {"natesty.jpg", "natesty2.jpg", "natesty3.jpg"};
char* tabela2[2] = {"wykrzyknik.png", NULL};
int main()
{
sfIntRect jajebie; //overlapped rectangle
sfBool zobaczymy;
sfIntRect uj;
sfIntRect uj2;
bool** Macha;
sfImage* Kamien = NULL;
sfSprite* sKamien = NULL;
sKamien = sfSprite_Create();
Kamien = sfImage_CreateFromFile("kamol.png");
sfSprite_SetImage(sKamien, Kamien);
sfView* Widok = NULL; //Creating view from rectangle
sfFloatRect prostychuj; //
prostychuj.Bottom = 600; //
prostychuj.Left = 0; //
prostychuj.Right = 800; //
prostychuj.Top = 0; //
Widok = sfView_CreateFromRect(prostychuj); //
sfClock* zegar; //Used for animation time
zegar = sfClock_Create(); //Used for animation time
float czas = 0; //Used for animation time
sfInput* Wejscie; //Input of window
sfColor Kolor; //Mask for Character picture
Kolor = sfColor_FromRGB(0,0,0); //Mask for Character picture
AniSprite test; //Structure of ainmated char
AniSprite* wsk= &test; //Structure of ainmated char
ZerujDane(wsk); //Structure of ainmated char
wsk->clock = sfClock_Create(); //Structure of ainmated char
wsk->duszek = sfSprite_Create(); //Structure of ainmated char
wsk->zdjecie = sfImage_CreateFromFile("Kubac.png"); //Structure of ainmated char
//sfImage_CreateMaskFromColor(wsk->zdjecie, Kolor, 0); //Mask
sfSprite_SetImage(wsk->duszek, wsk->zdjecie); //Structure of ainmated char
SetFrameSize(47,64,wsk); //Structure of ainmated char
SetLoopSpeed(10, wsk); //Structure of ainmated char
sfWindowSettings Settings = {24, 8, 0}; //Settings of window
sfVideoMode Mode = {800, 600, 32}; //Settings of window
sfImage* Image = NULL; //Background image
sfSprite* Sprite = NULL; //Background image
sfEvent Event;
Sprite = sfSprite_Create(); //Background image
/* Create the main window */
sfRenderWindow* App;
App = sfRenderWindow_Create(Mode, "SFML Testy", sfClose, Settings);
Instaluj(Sprite, Image, tabela[0]); //Assign background image to sprite
sfRenderWindow_SetFramerateLimit(App, 60);
sfSprite_SetPosition(sKamien,100,0); //Sets position of obsatcle rectangle
/* Start the game loop */
while (sfRenderWindow_IsOpened(App))
{
uj = SpriteNaRect(sKamien); //Creates rectangles from sprite
uj2 = SpriteNaRect(wsk->duszek); Creates rectangles from sprite (function which does it below
zobaczymy = sfIntRect_Intersects(&uj, &uj2, &jajebie); //Checks for intersects
if (zobaczymy) //If exists
{
std::cout << "Nachodza" << std::endl;
std::cout << jajebie.Left << std::endl;
std::cout << jajebie.Top << std::endl;
std::cout << jajebie.Right << std::endl;
std::cout << jajebie.Bottom << std::endl;
}
else std::cout << "Nienachodza" << std::endl; //if not
Wejscie = sfRenderWindow_GetInput(App); //Input
sfBool Prawo = sfInput_IsKeyDown(Wejscie, sfKeyD);
sfBool Lewo = sfInput_IsKeyDown(Wejscie, sfKeyA);
sfBool Gora = sfInput_IsKeyDown(Wejscie, sfKeyW); /
sfBool Dol = sfInput_IsKeyDown(Wejscie, sfKeyS); /
float Offset = sfRenderWindow_GetFrameTime(App) * 70.f; //Speed of moving character
float OffsetX = 0.f; //
float OffsetY = 0.f; //
float OffsetXWidoku = 0.f; //Offset of view
float OffsetYWidoku = 0.f; //Offset of view
/* Process events */
while (sfRenderWindow_GetEvent(App, &Event))
{
/* Close window : exit */
if (Event.Type == sfEvtClosed)
sfRenderWindow_Close(App);
}
czas = sfClock_GetTime(zegar); //DO ANIMACJI
if (Prawo == sfTrue && Lewo == sfFalse && Gora == sfFalse && Dol == sfFalse) //If You pressed D
{
OffsetXWidoku +=Offset;
OffsetX += Offset;
sfView_Move(Widok, OffsetXWidoku, 0);
sfSprite_Move(wsk->duszek, OffsetX, 0);
if (czas > 0.4f)
{
PlayUstalone(4,8,wsk);
sfClock_Reset(zegar);
}
}
if (Gora == sfTrue && Lewo == sfFalse && Prawo == sfFalse && Dol == sfFalse) //If You pressed W
{
OffsetYWidoku = -Offset;
OffsetY = -Offset;
sfView_Move(Widok,0,OffsetYWidoku);
sfSprite_Move(wsk->duszek, 0, OffsetY);
if (czas > 0.4f)
{
PlayUstalone(0,4,wsk);
sfClock_Reset(zegar);
}
}
if (Lewo == sfTrue && Prawo == sfFalse && Gora == sfFalse && Dol == sfFalse) //If You pressed A
{
OffsetXWidoku = -Offset;
OffsetX = -Offset;
sfView_Move(Widok, OffsetXWidoku,0);
sfSprite_Move(wsk->duszek, OffsetX, 0);
if (czas > 0.4f)
{
PlayUstalone(12,16,wsk);
sfClock_Reset(zegar);
}
}
if (Dol == sfTrue && Gora == sfFalse && Prawo == sfFalse && Lewo == sfFalse) If You pressed S
{
OffsetYWidoku = +Offset;
OffsetY = +Offset;
sfView_Move(Widok, 0, Offset);
sfSprite_Move(wsk->duszek,0, OffsetY);
if (czas > 0.4f)
{
PlayUstalone(8,12,wsk);
sfClock_Reset(zegar);
}
}
if ((Lewo == sfTrue) && (Prawo == sfTrue)) //All way down it makes sure that character moves when only one key is pressed
{
Stop(wsk);
}
if ((Dol == sfTrue) && (Gora == sfTrue))
{
Stop(wsk);
}
if ((Prawo == sfTrue) && (Gora == sfTrue))
{
Stop(wsk);
}
if ((Prawo == sfTrue) && (Dol == sfTrue))
{
Stop(wsk);
}
if ((Lewo == sfTrue) && (Gora == sfTrue))
{
Stop(wsk);
}
if ((Lewo == sfTrue) && (Dol == sfTrue))
{
Stop(wsk);
}
if ((Prawo == sfFalse) && (Lewo == sfFalse) && (Gora == sfFalse) && (Dol == sfFalse))
{
Stop(wsk);
}
/* Clear the screen */
sfRenderWindow_SetView(App, Widok); //Sets view
sfRenderWindow_Clear(App, sfBlack); /
/* Draw the sprite */
sfRenderWindow_DrawSprite(App, Sprite); //Draw bkacground
sfRenderWindow_DrawSprite(App, sKamien); //Draw obstacle
sfRenderWindow_SetView(App,
sfRenderWindow_GetDefaultView(App)); //Change view to default
Update(wsk); //Updates animation
sfRenderWindow_DrawSprite(App, wsk->duszek); //Draw animation
/* Update the window */
sfRenderWindow_Display(App);
}
/* Cleanup resources */
sfSprite_Destroy(wsk->duszek);
sfSprite_Destroy(sKamien);
sfSprite_Destroy(sHUD);
sfImage_Destroy(HUD);
sfImage_Destroy(Kamien);
sfImage_Destroy(wsk->zdjecie);
sfClock_Destroy(wsk->clock);
sfClock_Destroy(zegar);
sfClock_Destroy(zegar2);
sfSprite_Destroy(Sprite);
sfImage_Destroy(Image);
sfView_Destroy(Widok);
sfRenderWindow_Destroy(App);
return EXIT_SUCCESS;
}
Function to change sprite to rectangle:
sfIntRect SpriteNaRect(sfSprite* &duch)
{
sfIntRect Prostokat;
float x,y;
sfSprite_TransformToGlobal(duch, 0,0,&x, &y);
Prostokat.Top = (int) y;
Prostokat.Left = (int) x;
Prostokat.Bottom = (int)(y + sfSprite_GetHeight(duch));
Prostokat.Right = (int)(x + sfSprite_GetWidth(duch));
return Prostokat;
}
So far I know that doesn't work because when I use view there is like another ratio fo pixel because sKamien instead of 305 widht it has like 50 when I move my view and when I finally reach 405 pixel by moving my character then it says that rectangles doesn't collide.