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

Pages: [1]
1
Graphics / set coordinates depending on rotation
« on: July 02, 2012, 11:48:30 am »
I am currently working on collision detection/effect with triangles and I've got it to work if they are not rotating..

I have made 2 variables for each corner, one for x and y. The problem I'm having is that when you rotate the triangle the values of the corners stays the same (they are not targeting the corners anymore therefor the collision detection function I have made does not work). So I tried to make it so that those values depend on what the rotation is. The radius of the triangle is 64 and the top corner is right in the center so I thought that if the rotation was 90 degrees the x values should be - 64. With that in mind I came up with this: x = center of triangle - (rotation/1.40625) the 1.40625 is 90/64. This indeed made it so that when the rotation was 90 the value of x was - 64, I later realized that I had made a line instead of a curve.

So how do I make x and y coordinates make the same curve as the rotation?

2
General / Re: Jumping Sprite triggers after few seconds
« on: April 23, 2012, 03:12:18 pm »
I wrote a program from scratch with a sprite jumping.

Here you go.

Code: [Select]
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
using namespace std;

int main ()
{
float gravity = 0.2; // gravity... duh
float speed_y = 0; // vertical speed
bool ground = false; // if it's on the ground or in the air
sf::RenderWindow App(sf::VideoMode::GetMode(0), "Jumping Mario!", sf::Style::Fullscreen);
App.SetFramerateLimit(90);

sf::Image Mario;
Mario.LoadFromFile("mario2.png");

sf::Sprite mario;
mario.SetImage(Mario);

while(App.IsOpened())
    {
sf::Event Event;

while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}

// the movement

if (App.GetInput().IsKeyDown(sf::Key::Left))mario.Move(-300 * App.GetFrameTime(), 0);
if (App.GetInput().IsKeyDown(sf::Key::Right))mario.Move(300 * App.GetFrameTime(), 0);

// if it's on the ground

if (ground == true)
{
    if (App.GetInput().IsKeyDown(sf::Key::Space))
{
speed_y = -10;
ground = false;
}
}

// if it's in the air

if (ground == false)
{
speed_y = speed_y + gravity; // gravity takes effect
}
mario.Move(0, speed_y);

if (mario.GetPosition().y > App.GetHeight() - mario.GetSize().y) // tiny tiny tiny collision for the "floor"
{
ground = true;
mario.SetPosition(mario.GetPosition().x, App.GetHeight() - mario.GetSize().y);
}

App.Clear(sf::Color(255,255,255,255));
App.Draw(mario);
App.Display();
}
}

Ps. I hate the way the site messes up the spaces in your code.

3
General / Help needed:Delete element of vecotor (erase)
« on: April 23, 2012, 01:50:03 pm »
I am creating bullets that move to the left, when first  bullet have passed a certain point I want to delete it to make room for a new bullet to take it's place. I've put comments where the problem lies.

Declarations.h

Code: [Select]
#ifndef DECLARATIONS_H
#define DECLARATIONS_H

class bullet
{
public:
bullet(){shoot = true;}
sf::Clock clock;
sf::Sprite sprite;
void setShoot(bool setShoot){shoot = setShoot;}
bool getShoot(){return shoot;}
private:
bool shoot;
};

#endif

main.cpp

Code: [Select]
#include <iostream>
#include <vector>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"

using namespace std;

int main ()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Bullet");
App.SetFramerateLimit(90);

sf::Image Bullet_Bill;
Bullet_Bill.LoadFromFile("Bullet_Bill.png");

int count = 0;
bullet bulletMember;

vector <bullet> bulletlist;
vector <bullet> myBullet(10);
vector <bullet>::iterator start = bulletlist.begin();
vector <bullet>::iterator start2 = myBullet.begin();

while(App.IsOpened())
    {
sf::Event Event;

while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
if (bulletMember.clock.GetElapsedTime() > 1.0 && bulletMember.getShoot() == true && count < 9)
    {
count ++;
cout << "Bang! " << count << endl;
bulletlist.push_back(myBullet[count]);
myBullet[count].sprite.SetImage(Bullet_Bill);
myBullet[count].sprite.SetPosition(App.GetWidth()- myBullet[count].sprite.GetSize().x, 0);
    bulletMember.setShoot(false);
    }
    if (bulletMember.getShoot() == false)
    {
    bulletMember.clock.Reset();
    bulletMember.setShoot(true);
    }

//-----------need help with this bit----------------

if ( count > 0 && myBullet[1].sprite.GetPosition().x < -1)
{
count--;
bulletlist.erase(bulletlist.begin());
//when the first bullet is touching the left edge all bullets gets deleted but I only want 1 bullet to get deleted and
// when the next bullet is touching it I want that one to get deleted and so on.
//cout<<myBullet[1].sprite.GetPosition().x << endl;
}

//--------------------------------------------------

    App.Clear(sf::Color(255,255,255,255));
for(int i = 0; i <= count; i ++)
{
myBullet[i].sprite.Move(-150 * App.GetFrameTime(), 0);
App.Draw(myBullet[i].sprite);
}
App.Display();
}
}


4
General / Re: Help needed: Target an entire Array
« on: April 23, 2012, 01:13:15 am »
It's not working for me, still the same problem. Can you please show me how you got it to work?

5
General / Help needed: Target an entire Array
« on: April 22, 2012, 08:22:56 pm »
So I am trying to make a canon that shoots bullets, the only problem that I'm having is that I don't know how to move/draw the objects sprite efficently. The commented parts are where the problem lies

Here is the code

Declarations.h
Code: [Select]
#ifndef DECLARATIONS_H
#define DECLARATIONS_H

class bullet
{
public:
bullet(){shoot = true;}
sf::Clock clock;
sf::Sprite sprite;
void setShoot(bool setShoot){shoot = setShoot;}
bool getShoot(){return shoot;}
private:
bool shoot;
};

#endif

Main.cpp
Code: [Select]
#include <iostream>
#include <vector>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"

using namespace std;

int main ()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Bullet");
App.SetFramerateLimit(90);

sf::Image Bullet_Bill;
Bullet_Bill.LoadFromFile("Bullet_Bill.png");

int i = 0;
bullet bulletMember;
bullet myBullet[10];

vector <bullet> bulletlist;

while(App.IsOpened())
    {
sf::Event Event;

while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
if (bulletMember.clock.GetElapsedTime() > 1.0 && bulletMember.getShoot() == true && i <= 8)
    {
    cout << "Bang! " << i << endl;
i ++;
    bulletlist.push_back(myBullet[i]);
myBullet[i].sprite.SetImage(Bullet_Bill);
    bulletMember.setShoot(false);
    }
    if (bulletMember.getShoot() == false)
    {
    bulletMember.clock.Reset();
    bulletMember.setShoot(true);
    }
    myBullet[i].sprite.Move(150 * App.GetFrameTime(), 0); // only applies to 1 object at a time :(

    App.Clear(sf::Color(255,255,255,255));
App.Draw(myBullet[i].sprite); // only applies to 1 object at a time :(
App.Display();
}
}

6
I have watched a few videos of the stl containers, vector, map, list, set but I nevered used any of them before so I would be very happy if you could give me an example,

7
General / Help needed: Creating "pointer array objects" (*object[])
« on: April 21, 2012, 06:01:24 pm »
In my game I want a canon to fire bullets once every second, im half way there but it's getting too complicated for me so I need some help. I made a small example of the bullet code. The code that has been commented causes the program to crash.

Here is the code

Declarations.h
Code: [Select]
#ifndef DECLARATIONS_H
#define DECLARATIONS_H

unsigned char level_1
(sf::RenderWindow &App,
sf::Image &Bullet_Bill,
class bullet *bulletlist[],
class bullet &bulletMember,
int &counter,
unsigned char &level);

class bullet
{
public:
    bullet(){shoot = true;}
    sf::Clock clock;
    sf::Sprite sprite;
    void setShoot(bool setShoot){shoot = setShoot;}
    bool getShoot(){return shoot;}
private:
    bool shoot;
};

#endif

Main.cpp

Code: [Select]
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"

using namespace std;

int main ()
{
    unsigned char level = 1;

    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Bullet");
    App.SetFramerateLimit(90);

    sf::Image Bullet_Bill;
    Bullet_Bill.LoadFromFile("Bullet_Bill.png");

    int counter = 0;
    bullet *bulletlist[100];

    bullet bulletMember;

    while(App.IsOpened())
    {
        sf::Event Event;

        while(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed)
            App.Close();
            if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
            App.Close();
        }
        switch (level)
        {
            case 1:level_1 (App, Bullet_Bill, &bulletlist[counter], bulletMember, counter, level);
        }
    }
}

level_1.cpp

Code: [Select]
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"

using namespace std;

unsigned char level_1(sf::RenderWindow &App,
sf::Image &Bullet_Bill,
class bullet *bulletlist[],
class bullet &bulletMember,
int &counter,
unsigned char &level)
{
    if (bulletMember.clock.GetElapsedTime() > 1.0 && bulletMember.getShoot() == true)
    {
        cout << "Bang!" << counter << endl;
        bulletMember.setShoot(false);
        bulletlist[counter] = new bullet;
        bulletlist[counter]->sprite.SetImage(Bullet_Bill);
        bulletlist[counter]->sprite.SetPosition(App.GetWidth()/2, App.GetHeight()/2);
        counter ++;
    }
    if (bulletMember.getShoot() == false)
    {
        bulletMember.clock.Reset();
        bulletMember.setShoot(true);
    }
    /*if (bulletlist[counter]->sprite.GetPosition().x < 0 - bulletlist[counter]->sprite.GetSize().x)
    {
        delete bulletlist[counter];
        counter --;
    }*/
    //bulletlist[counter]->sprite.Move(-150 * App.GetFrameTime(), 0);

    App.Clear(sf::Color(255,255,255,255));
    //App.Draw(bulletlist[counter]->sprite);
    App.Display();
    return level;
}

I appreciate all the help I get becuase right now this is frying my brain.

8
Graphics / Re: Help needed: Display variable(int) in sf::string
« on: April 08, 2012, 07:12:11 pm »
Yes it works wonderfully thanks a LOT!

9
Graphics / Help needed: Display variable(int) in sf::string
« on: April 08, 2012, 06:42:46 pm »
I am making a game in c++ sfml 1.6 and I have encountered a problem.

What I am trying to achieve

Add a life indicator in the top right corner by drawing a string that has the value of the life variable.

What I have done so far

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    int life = 3; // decides how much health you got left (the life variable)
   
    std::ostringstream oss;
    oss << life;
   
    sf::String life_string;
    life_string.SetText(oss.str());
   
    // in the game loop
   
    App.Draw(life_string);

    // in the game loop if my character is dead

    life--;
    oss << life;
    life_string.SetText(oss.str());
}

The problem is that everytime I die a new value gets printed but the old one stays so first it's 3 then its 32 and then its 321 etc

Here is a picture with 0 deaths
Here is a pricture with 2 deaths

so how do I remove the onld value so that there is only 1 value showing

Pages: [1]
anything