-
I nearly made a racing "game" and I don't know how to implement a explosion if two cars crash. I tried to make a sprite explosion and I tried to move it in the middle of the crash with SetPosition() but it didn't worked,I also tried it and with pos,but that didn't work either, it always goes to the origin(0,0).Sorry If I sound like a noob :P
-
And your question is?
-
How to place the explosion in the middle of the car crash
-
For easy placement you can set the sprite origin to the center of the explosion texture and the set the position to the place between the two cars.
-
I tried this
Sprite SExplosion;
SExplosion.setTexture(TExplosion);
SExplosion.setOrigin(pos.x,pos.y + 15);
Vector2f posE = SExplosion.getPosition();
SExplosion.setPosition(posE.x,posE.y);
if(SPolice.getGlobalBounds().intersects(SDodgeViper.getGlobalBounds()))
{
posE.x = posP.x;
posE.y = posP.y - 150;
}
When I made the origin the pos. of the player the Explosion didn't load at all, why ?
-
Maybe take out a pen and some paper and draw all the points you're dealing with here.
Getting the position of the explosion and setting the position to that retrieve value does nothing at all. You're just resetting it to the same position as it already was.
What is "pos" exactly? You should set the origin to the middle of the texture or the "middle" of the explosion on the texture. So you should at some point take the texture's size and divide it by 2.f or similar.
Also keep in mind, that changing the variable posE doesn't change the position of the sprite, you have to pass the posE back to the sprite again, before the sprite itself changes position.
-
I did as you said but it doesn't work, can't you show me an example ?
Texture TExplosion;
if(!TExplosion.loadFromFile("Explosion.png"))
{
cout<<"Error while loading Explosion."<<endl;
}
TExplosion.setSmooth(1);
Sprite SExplosion;
SExplosion.setTexture(TExplosion);
SExplosion.setOrigin(105.5 , 110);
Vector2f posE = SExplosion.getPosition();
if(SPolice.getGlobalBounds().intersects(SDodgeViper.getGlobalBounds()))
{
posE.x = posP.x;
posE.y = posP.y - 150;
}
-
And how do you define "it doesn't work", because that's not a problem description.
-
The "explosion" appers at 105.5,110 not in the middle of the car crash :o
-
You should be using both origin and position.
The origin should be set to the point in SExplosion where you consider to be the main point of the explosion (where it would match the collision or whatever).
The position should then be set to point of collision or wherever you want that explosion to be.
Remember that the point specified by the origin ends up at the point specified by the position.
Also, this:
Vector2f posE = SExplosion.getPosition();
SExplosion.setPosition(posE.x,posE.y);
doesn't do anything.
p.s. In response to just your title - "How to make a car crash ?", press the accelerator and let go of the the steering wheel. ;D
-
Hapax, can you give me an example, because I tried to make this explosion a lot of times, but the explosion goes to 0,0 or to 105.5 , 110 or elsewere.
PS:Haha, I know the title is a little bit confusing but I didn't had any idea for it :P
-
Without resorting to writing the code for you, I could just describe it; that'll give you something to do :P
Try setting the origin to the centre of the local bounds of the explosion and then set the position to place where you want that explosion to occur.
-
I want my explosion to be like pos.y + 15 I tried to do this with SetPosition(), but it didn't move it, and how to set the origin to the "centre of the local bounds of the explosion" ? :-[
-
Okay.
The question that you have answer (for yourself) is which part of the explosion do you want to appear at that point? The top-left corner, the centre, the bottom-centre, half-way between the top-right and the centre?
The top-left is at (0, 0). You should set the origin to the point from which you want to control it. If you don't know that size of the explosion image, you can use its local bounding rectangle (https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Sprite.php#a69557a8369bc2e26dd2e2eb2c50f5c90). I'm assuming this is an sf::Sprite but works the same with an sf::RectangleShape too. Once you know its size, the centre would simply be its size halved.
Once you have the origin in the correct place, setPosition will place that point in the position that you set it...
-
Ok, I figured it out (I know someone said that the posE doesn't move the sprite,that was my mistake,now I will paste the code with some explanations for those who had the same problem as me),thanks for the awesome help and explanations.
Texture TExplosion;
if(!TExplosion.loadFromFile("Explosion.png"))
{
cout<<"Error while loading Explosion."<<endl; //Setting the texture
}
TExplosion.setSmooth(1); //Smoothing it to be more appeling
Sprite SExplosion; //Setting the sprite
SExplosion.setTexture(TExplosion); //Setting the texture to the sprite
SExplosion.setOrigin(105.5 , 110); //Setting the origin to the half of the texture
//in the game loop right before the clear();
if(SPolice.getGlobalBounds().intersects(SDodgeViper.getGlobalBounds())) //checking if the Police car intersects the Player car
{
SExplosion.setPosition(pos.x + 50 , pos.y - 40); //if so, the explosion will create
}
I know I didn't had the best explanations, but this will be a good start, correct me if I was wrong.