1
General / How to add scoring?
« on: June 06, 2011, 08:01:41 pm »
Thanks. Going to test now.
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.
An image is always a square. To do collission detection you could check the color of different parts of your maze and if it's white/transparent you would have no collision.
Not 100% sure what you specifically meant with bounding box. Fireworks have a feature where you can export certain tiles as slices into separate images with specific output format; also note the editor is more suited for web development, but you can do small game graphics as well. Its also lot easier to prototype game scene with many small tiles and throw in some advanced features with effects, lightning, blending, ... etc.
Can I ask how you would do collision with the bat if it is drawn from the centre?
Also whenever I use your method for bouncing the ball off the bat it always goes towards the bottom left corner, what am I doing wrong?
- Simon
That moves it only in one direction because you are telling it to move only to one direction. What you want to do is to figure out which side of the rectangle is actually hit and move the object accordingly. For example, if the object is coming straight down when it hits the object, you want to move the object up to keep it above the rectangle. You should be able to figure it out by comparing the coordinates, a bit like the collision detection is done. For example to find out if the object is coming from the top, you could do something like thisCode: [Select]
if(object.x() + object.size().x > object2.x() && object.x() < object2.x() + object2.size().x && object.y() > object2.y())
Btw. sprite.move(x, y) moves the sprite horizontally the amount of x and vertically the amount of y.
sprite1.move(speed * ElapsedTime, 0);
but it only goes left(I think) but I want the whole box to be protected.
Here's one that I wrote in maybe two minutes. I'll try to explain it to you later (gotta go right now) but the idea is exactly the same that I tried to explain to you in the other thread.
Add this before your main-functionCode: [Select]
static bool checkCollision(sf::Sprite sprite1, sf::Sprite sprite2) {
if(sprite1.GetPosition().x + sprite1.GetSize().x > sprite2.GetPosition().x && sprite1.GetPosition().x < sprite2.GetPosition().x + sprite2.GetSize().x && sprite1.GetPosition().y + sprite1.GetSize().y > sprite2.GetPosition().y && sprite1.GetPosition().y < sprite2.GetPosition().y + sprite2.GetSize().y)
return true;
return false;
}
It's used like thisCode: [Select]if(checkCollision(Object1, Object2))
std::cout << "Collision!";
You can't manually set the size of the bounding box without modifying the BoundingBoxTest-function (unless there's something you can do with the sprites themselves that I'm not aware of). What are the dimensions of your images?
Do you need to rotate the sprites btw? If not then writing your own detection function is really easy.
It doesn't matter whether if the objects are on the screen or not, the detection works exactly the same. I wanted you to put them far from each other so that you could see if the program closes no matter where the objects are (ie. the collision detection function doesn't work).
If adding that causes the program exit, it means that the function returns true so there should be a collision. Try to put the sprites further away from each other. Put them on opposite edges of the screen.