Results 1 to 3 of 3

Thread: Collision

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.