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

Author Topic: Collision detection with an image.  (Read 2230 times)

0 Members and 1 Guest are viewing this topic.

Yaxlat

  • Newbie
  • *
  • Posts: 14
    • View Profile
Collision detection with an image.
« on: April 01, 2015, 06:13:29 pm »
I have a "spike" object which I want to implement into my game, which has:
  • A particular image, which looks something like this:
  • A position
  • And a rotation
The position and rotation can change dynamically.

I can get the thing to display, but I don't know how to do collision detection. I was thinking of either creating some sort of shape around the object upon initialization and somehow collision detect with that, or use pixel perfect collision.

However I don't know which one is better, or if there is another solution, and in any case I dont know how to achieve those things. I have only dealth with rectangles and circles when it comes to collision detection in the past.

Can someone give me a hand?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Collision detection with an image.
« Reply #1 on: April 01, 2015, 06:56:56 pm »
If you don't care AABB then the following should work for you. Otherwise just google for 'AABB collision detection' and you should find plenty of articles. By the way, I really doubt you will need pixel perfect collision.  ;)

if (player.getGlobalBounds().intersects(object.getGlobalBounds())
{
   // collision
}

http://www.sfml-dev.org/documentation/2.2/classsf_1_1Rect.php
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Yaxlat

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Collision detection with an image.
« Reply #2 on: April 01, 2015, 07:36:24 pm »
If you don't care AABB then the following should work for you. Otherwise just google for 'AABB collision detection' and you should find plenty of articles. By the way, I really doubt you will need pixel perfect collision.  ;)

if (player.getGlobalBounds().intersects(object.getGlobalBounds())
{
   // collision
}

http://www.sfml-dev.org/documentation/2.2/classsf_1_1Rect.php

I dont think you quite understand. I'm wondering how I can detect whether or not the player is within the white space of the picture. getGlobalBounds() simply deals with rectangles, which is not what I'm dealing with for my "spike" object.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10990
    • View Profile
    • development blog
    • Email
Re: Collision detection with an image.
« Reply #3 on: April 01, 2015, 09:45:35 pm »
Pixel perfect detection can be slow but doesn't have to be. To figure out how it performs for your game you'll have to actually try it out. On the plus side, pixel perfect collision is in my opinion a lot easier since you'll basically just have to compare the alpha value of some pixels.

Modeling an outline object for the image can be a lot more tricky and will require some math knowledge.

We can't answer you "what is better" because it's never just black and white but instead fully depends on your game architecture and what you actually want to do with it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Collision detection with an image.
« Reply #4 on: April 01, 2015, 10:10:03 pm »
You could build something like a collision map for each obstacle texture, just try to rebuild the visible parts of the picture with triangles (Try to use the least amount as possible) and save the coordinates of the vertices in a extra file and load them into your game. Then you would only have to do a few rect triangle intersections (google will be your friend  :D)

But if you have a lot of different and complex textures I would prefer the pixel perfect test.


AlexAUT

Yaxlat

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Collision detection with an image.
« Reply #5 on: April 02, 2015, 01:01:52 pm »
Ok I think i will try pixel perfect collision, and I'll change it up later down the line if I have performance issues. Thanks for your help! How do I do it using SFML?