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

Author Topic: Adding collision rectangles to tiles (Help)  (Read 1816 times)

0 Members and 1 Guest are viewing this topic.

Dreken

  • Newbie
  • *
  • Posts: 9
    • View Profile
Adding collision rectangles to tiles (Help)
« on: September 09, 2012, 08:27:12 pm »
I'm using sfml 2.0-rc, Windows 7, and Visual studio 2010.

Here is my snippet of code:

for(int nrow = 0; nrow < 24; nrow++)
                {
                        for (int ncol = 0; ncol < 33; ncol++)
                        {
                        sprite2.setPosition(ncol * 32, nrow * 32);
                        sprite1.setPosition(ncol * 32, nrow * 32);
                        if (dDungeon[nrow][ncol] == '#')
                                {

                                CollisionRectangles[nrow][ncol] = sf::FloatRect (ncol*32, nrow*32,32,32);
                                window.draw(sprite1);
                                }
                        if (dDungeon[nrow][ncol] == ' ')
                                {
                                window.draw(sprite2);
                                }      
                        }
                }

I'm getting some kind of error when I hover over the sf::FloatRect, which reads:

"Error: No suitable Conversion function from "sf::FloatRect" to "float" exists.

I don't really understand this at all, can anyone help?

With this code, I'm creating a tilemap from sprites, and putting one collision rectangle over every single sprite so that I can define collision detection later with a moving character sprite.

So how do I load 792 different (24*33) collision rectangles into an array made to store them?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Adding collision rectangles to tiles (Help)
« Reply #1 on: September 09, 2012, 08:41:30 pm »
Please provide more information: the line of the error, the type of the variables involved, etc.

My guess is that CollisionRectangles is an array of floats instead of FloatRects. That's the only line that can trigger this error in the code that you show.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10235
    • View Profile
    • development blog
    • Email
Re: Adding collision rectangles to tiles (Help)
« Reply #2 on: September 09, 2012, 08:44:00 pm »
One important part of the code is missing, where the CollisionRectangles gets declared, because now we can only guess what it's type is.
If I'm right the array is declared as float instead of sf::FloatRect, because that's what the error says. It's trying to convert sf::FloatRect to a float, which obviously fails. ;)

Additionally, since you're defining a sf::FloatRect you should also use floats in its constructor, e.g.
sf::FloatRect (ncol*32.f, nrow*32.f, 32.f, 32.f)
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

Dreken

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Adding collision rectangles to tiles (Help)
« Reply #3 on: September 09, 2012, 09:49:53 pm »
I see what you guys are saying, but I just can't get it to work -_-


This is one part of what you asked for:

float CollisionRectangles[24][33];

and  this line was giving me that error:

CollisionRectangles[nrow][ncol] = sf::FloatRect (ncol*32.f, nrow*32.f, 32.f, 32.f);

I'll try to explain again,

Basically I'm trying to put a rectangle the same size and position over every tile on the tile map, which lets me use the check intersect function with the player sprite's rectangle to establish collision detection.

I don't know how to put a different rectangle onto every tile of the tilemap. (I am going to use a for loop to cycle through them later and check each tile for collision).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10235
    • View Profile
    • development blog
    • Email
Re: Adding collision rectangles to tiles (Help)
« Reply #4 on: September 09, 2012, 09:52:53 pm »
I see what you guys are saying, but I just can't get it to work -_-

This is one part of what you asked for:

float CollisionRectangles[24][33];

and  this line was giving me that error:

CollisionRectangles[nrow][ncol] = sf::FloatRect (ncol*32.f, nrow*32.f, 32.f, 32.f);

The just do this change to your code:
sf::FloatRect CollisionRectangles[24][33];

:)
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

Dreken

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Adding collision rectangles to tiles (Help)
« Reply #5 on: September 09, 2012, 10:29:43 pm »
Worked, Thanks!