Results 1 to 3 of 3

Thread: Collision

  1. #1
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Question Collision

    Hello !!!

    So I'm making a game in which i have a working player (QGraphicsPixmapItem ) that can move. I implemented rocks and trees into the game that are of QGraphicsPixmapItem type.
    What i want to know is if someone knows a better way for me to check for collision when destroying rocks and trees ?

    The way i have it right now is like this :


    When the player attacks i check if the item (slash which is also a QGraphicsPixmapItem ) has collided with the Tree or the Rock, but the problem appears when i'm trying to access a function inside the collided Tree or Rock
    And the only solution i found out on how to that is

    - first by checking if the slash item has hit the Tree
    Qt Code:
    1. QList<QGraphicsItem *> colliding_items = slash->collidingItems();
    2.  
    3. for(int i=0;i<colliding_items.size();++i){
    4. if(holding == "axe"){
    5. if(typeid (*(colliding_items[i])) == typeid (Tree))
    6. for(int k=0;k<3;k++)
    7. game->tree[k]->checkCollision();
    8. }else if(holding == "pickaxe"){
    9. if(typeid (*(colliding_items[i])) == typeid (Rock))
    10. for(int k=0;k<3;k++)
    11. game->rock[k]->checkCollision();
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    - second going through all the created Trees and Rocks and check for collision again inside the Tree or Rock function ( checkCollision() ) in which i go again through another loop to check if it has collided with the slash item
    Qt Code:
    1. void Rock::checkCollision()
    2. {
    3. QList<QGraphicsItem *> colliding_items = collidingItems();
    4. for(int i=0;i<colliding_items.size();++i){
    5. if(colliding_items[i] == game->player->slash){
    6. hp--;
    7. }
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    I made a dynamic table of trees and rocks but i hate doing it like that especially since i plan to have a lot more trees and rocks ( around 60 minimum ), if any1 knows a better more optimized way for me to do it, please share...
    Last edited by MongKong; 8th March 2020 at 12:59.

  2. #2
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Collision

    I figured it out, if anyone else wants to know how i did it, here it is:
    Qt Code:
    1. QList<QGraphicsItem *> colliding_items = slash->collidingItems();
    2.  
    3. for(int i=0;i<colliding_items.size();++i){
    4. if(holding == "axe"){
    5. if(Tree* asTree = dynamic_cast<Tree*>(colliding_items[i])){
    6. asTree->collide();
    7. break;
    8. }
    9. }else if(holding == "pickaxe"){
    10. if(Rock* asRock = dynamic_cast<Rock*>(colliding_items[i])){
    11. asRock->collide();
    12. break;
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to MongKong for this useful post:

    d_stranz (9th March 2020)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,479
    Thanks
    252
    Thanked 709 Times in 699 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Collision

    Thanks. Next time I go into the woods with my axe and pickaxe, I'll know which one to use if I come across a tree or a rock...
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    MongKong (9th March 2020)

Similar Threads

  1. Collision direction
    By Erkki2 in forum Qt Programming
    Replies: 6
    Last Post: 4th November 2013, 14:00
  2. Need Help Regarding Qt GraphicsItem Collision
    By RE in forum Qt Programming
    Replies: 4
    Last Post: 28th September 2012, 11:12
  3. Collision between QGraphicsItem -
    By been_1990 in forum General Discussion
    Replies: 6
    Last Post: 19th November 2010, 12:32
  4. collision detection...
    By Muffin in forum Newbie
    Replies: 1
    Last Post: 8th January 2010, 10:28
  5. Collision handling 101
    By Morea in forum Qt Programming
    Replies: 9
    Last Post: 25th November 2006, 20:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.