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

Author Topic: Non-drawable Shape  (Read 2742 times)

0 Members and 1 Guest are viewing this topic.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Non-drawable Shape
« on: January 18, 2011, 11:21:50 am »
Yo!

We're going trough 3D math(don't know how to translate it from Swedish so let's just call it that) at school right now and I realized that you are missing a Shape class that are not for being drawn but for checking intersections.

Kinda like the Rect class but by using a normal vector(Again, I think that's the correct translation) on each line we can check if a point is inside the shape and so on.

This is more or less a simple utility that for most of the time will be used for collision detection for more complex shapes. If you feel that you don't got time for that I could just make it myself and add it to the Wiki and then you can add it to SFML2 if you feel it fits ^^
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Non-drawable Shape
« Reply #1 on: January 18, 2011, 11:26:46 am »
I was thinking about a polygon classe maybe, not sure.

Anyway you'd better write what you need and share it on the wiki, it will definitely be better than waiting for whatever I may write one day :)
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Non-drawable Shape
« Reply #2 on: January 22, 2011, 03:01:20 pm »
Just now quickly I made a LineVolume class that's for an assignment at school but with some modifications(like making the line calculation internal) it would be possible to use it like I intended and place it at the wiki.

Code: [Select]
#ifndef VOLUME_HEADER_
#define VOLUME_HEADER_

#include <vector>

// LineVolume inherits from this class. I made it like this as in the
// assignment we also had to create a PlaneVolume with the same
// functionallity but using planes as the primitive instead.
template<class Primitive, class VectorType>
class Volume
{
public:
Volume()
{
}

Volume(const std::vector<Primitive> &aList)
{
myList = aList;
}

~Volume()
{
}

void Add(const Primitive &aPrimitive)
{
myList.push_back(aPrimitive);
}

void Remove(const Primitive &aPrimitive)
{
for(std::vector<Primitive>::iterator it = myList.begin(), end = myList.end(); it != end; it++)
{
if(*it == aPrimitive)
{
myList.erase(it);
break;
}
}
}

bool Inside(const VectorType &aPoint) const
{
for(std::vector<Primitive>::const_iterator it = myList.begin(), end = myList.end(); it != end; it++)
{
if(it->Inside(aPoint) == false)
{
return false;
}
}
return true;
}
private:
std::vector<Primitive> myList;
};

#endif VOLUME_HEADER_


Code: [Select]
#ifndef LINE_HEADER_
#define LINE_HEADER_

#include "Vector2.h"

template<class Type>
class Line
{
public:
Line() {}
Line(const Vector2<Type> &aPoint, const Vector2<Type> &aSecondPoint)
{
InitWith2Points(aPoint, aSecondPoint);
}

void InitWith2Points(const Vector2<Type> &aPoint, const Vector2<Type> &aSecondPoint)
{
myPoint = aPoint;
myDirection = aSecondPoint - aPoint;
myDirection /= myDirection.Length();
}

void InitWithPointAndDirection(const Vector2<Type> &aPoint, const Vector2<Type> &aDirection)
{
myPoint = aPoint;
myDirection = aDirection;
}

bool Inside(const Vector2<Type> &aPoint) const
{
Vector2<Type> normal;
normal.x = -myDirection.y;
normal.y = myDirection.x;

Vector2<Type> testPosition = aPoint - myPoint;
testPosition /= testPosition.Length();
return normal.Dot(testPosition) > 0;
}

bool operator==(const Line<Type> aLine)
{
return myPoint == aLine.myPoint && myDirection == aLine.myDirection;
}

private:
Vector2<Type> myPoint;
Vector2<Type> myDirection;
};

#endif LINE_HEADER_
Developer and Maker of rbSFML and Programmer at Paradox Development Studio