Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]How to make a car crash ?  (Read 2945 times)

0 Members and 1 Guest are viewing this topic.

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
[SOLVED]How to make a car crash ?
« on: May 01, 2017, 10:01:33 am »
 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
« Last Edit: June 06, 2017, 08:07:28 pm by Boanc »
Guess Who's Back ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Re: How to make a car crash ?
« Reply #1 on: May 01, 2017, 11:03:05 am »
And your question is?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: How to make a car crash ?
« Reply #2 on: May 01, 2017, 11:13:21 am »
How to place the explosion in the middle of the car crash
Guess Who's Back ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Re: How to make a car crash ?
« Reply #3 on: May 01, 2017, 12:51:55 pm »
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: How to make a car crash ?
« Reply #4 on: May 01, 2017, 01:05:33 pm »
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 ?
Guess Who's Back ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Re: How to make a car crash ?
« Reply #5 on: May 01, 2017, 01:23:34 pm »
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: How to make a car crash ?
« Reply #6 on: May 01, 2017, 01:50:50 pm »
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;
    }
Guess Who's Back ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Re: How to make a car crash ?
« Reply #7 on: May 01, 2017, 02:36:38 pm »
And how do you define "it doesn't work", because that's not a problem description.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: How to make a car crash ?
« Reply #8 on: May 01, 2017, 03:03:51 pm »
The "explosion" appers at 105.5,110 not in the middle of the car crash :o
Guess Who's Back ?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to make a car crash ?
« Reply #9 on: May 01, 2017, 04:50:56 pm »


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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: How to make a car crash ?
« Reply #10 on: May 01, 2017, 05:11:24 pm »
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
Guess Who's Back ?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to make a car crash ?
« Reply #11 on: May 01, 2017, 07:11:13 pm »
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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: How to make a car crash ?
« Reply #12 on: May 01, 2017, 07:14:49 pm »
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" ? :-[
Guess Who's Back ?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to make a car crash ?
« Reply #13 on: May 01, 2017, 10:03:01 pm »
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. 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...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ionut

  • Jr. Member
  • **
  • Posts: 59
  • Guess Who's Back ?
    • View Profile
    • Email
Re: How to make a car crash ?
« Reply #14 on: May 02, 2017, 05:05:20 pm »
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.
Guess Who's Back ?