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

Pages: [1]
1
Graphics / Collision detection problem
« on: July 15, 2011, 12:36:55 pm »
My frame rate is at 60fps

I wrote this code and sometimes it detects a collision and sometimes it does not.

My ship is meant to collide with mines and crates, but sometimes it will just pass through them, or the mine or crate will just explode before it should.

Any help would be appreciated.


Code: [Select]
bool Collision::HasCollided(sf::Sprite& sprite1, sf::Sprite& sprite2)
{
// this function checks if the two inputted sprites have
// collided using circle collision detection

if(sprite1.GetSize().x > sprite1.GetSize().y)
{
r1 = sprite1.GetSize().x/2;
}
else
{
r1 = sprite1.GetSize().y/2;
}

if(sprite2.GetSize().x > sprite2.GetSize().y)
{
r2 = sprite2.GetSize().x/2;
}
else
{
r2 = sprite2.GetSize().y/2;
}

circle1 = sf::Shape::Circle(0, 0, r1, sf::Color(255,255,255,200));
circle1.SetPosition(sprite1.GetPosition().x, sprite1.GetPosition().y);

circle2 = sf::Shape::Circle(0,0,r2,sf::Color(255,255,255,200));
circle2.SetPosition(sprite2.GetPosition().x, sprite2.GetPosition().y);

distX = circle2.GetPosition().x - circle1.GetPosition().x;
distY = circle2.GetPosition().y - circle1.GetPosition().y;

distance = sqrt((distX*distX)+(distY*distY));
sumOfRadii = r1 + r2;

if(distance <= sumOfRadii)
{
return true;
}
else
{
return false;
}
}

2
Graphics / Can I improve the efficiency of my vector for sprites?
« on: June 10, 2011, 04:09:45 pm »
Maybe inefficiency is the wrong word. I just wanted to know because I always like to learn better ways if there are any

But the vector in question is constantly moving and when it collides with bullets the sprite in vector is removed.

I haven't used a profile, do you have any recommendations of one to use?

3
Graphics / Can I improve the efficiency of my vector for sprites?
« on: June 10, 2011, 03:30:23 pm »
So I have an vector that stores multiple asteroids sprites, in its current form it copy's the original asteroid sprite into the vector, could I just point to the original sprite?

this how it is at the moment

imgAsteroid.LoadFromFile("Images/asteroid.png");
sprAsteroid.SetImage(imgAsteroid);
sprAsteroid.SetCenter((imgAsteroid.GetWidth())/2,(imgAsteroid.GetHeight())/2);

std::vector<sf::Sprite> roidStore; //vector to store asteroids

so anytime I add an asteroid to the vector I do

roidStore.push_back(sprAsteroid);

is there anyway I can improve the efficiency of this?

4
Graphics / random setPosition generater, using rand()
« on: June 06, 2011, 03:58:18 pm »
That's great, didn't though that rand() % range + minimum was the formula I thought it was rand() % minimum + maximum.

Thanks for the quick reply

5
Graphics / random setPosition generater, using rand()
« on: June 06, 2011, 03:42:24 pm »
hi,

I was wondering how would you generate random positions between -100 to 0 as well as 800 to 900

for example this is the code I have
(*currentIter).SetPosition(rand()% -100+0, rand()% 600);

and it sets the sprites position between 100 and 0
and the code below sets the sprite position between 800 and 900

(*currentIter).SetPosition(rand()%800 + 900, rand()% 600);

The question is how can I randomly choose between the both of these lines of code?

i tried:

(*currentIter).SetPosition(rand()%(-100 + 0)||(800 + 900), rand()% 600);

but that didn't work.

6
Graphics / Moving forwards/backwards based on angle
« on: June 02, 2011, 11:45:50 pm »
Ok thats great, thank you

7
Graphics / Moving forwards/backwards based on angle
« on: June 02, 2011, 10:51:11 pm »
Everything works fine when I changed it to -sin

but how come it needs to be -sin I always though that the velocity is calculated for y using: velocity_y = sin(angle) * speed?

is there something I have done wrong for it to have to be -sin?

8
Graphics / Moving forwards/backwards based on angle
« on: June 01, 2011, 01:36:41 pm »
That did it, I feel like an idiot..Thanks

9
Graphics / Moving forwards/backwards based on angle
« on: June 01, 2011, 11:26:20 am »
So the rotation works perfectly using the code above

but when I rotate the sprite and press the "UP" key it doesn't move forwards in the way it is meant to. I'm trying to create an asteroids game, and this is for the ship.

10
Graphics / Moving forwards/backwards based on angle
« on: May 31, 2011, 08:45:03 pm »
This is the code I used

Code: [Select]
//Convert angle to radians
angleRADS = (3.1415926536/180)*(sprPlayer.GetRotation());

//Set x and y
forx = 0.01f*cos(angleRADS);
fory = 0.01f*sin(angleRADS);

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
{
sprPlayer.Rotate(-0.03f);
}

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
{
sprPlayer.Rotate(0.03f);
}

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code ==  sf::Key::Up))
{
sprPlayer.Move(forx, fory);
}


The problem is the code above doesn't work, when i rotate the sprite, it doesn't in the direction its meant to.

I'm new to this so any guidance, on what is wrong with the above code will be helpful.

11
Graphics / Moving forwards/backwards based on angle
« on: May 31, 2011, 08:12:50 pm »
Hi

I am creating an asteroids game and I need to move the ships forwards and backwards based on its current angle. If someone could help me it would be greatly appreciated.

I have read that you can calculate the below, but can't seem to get it to work in c++

x = speed * cos(current_angle);
y = speed * sin(current_angle);

Pages: [1]
anything