Object coordinates - c++

I'm working at QT application that have a OSGWidget. How could i get coordinates of picked object in osg viewer and transfer it to QT textEdit in Dialog window. Could you give advice how to extract osg::Vec3d of selected object from viewer convert it to string and show it off? For example. I have these scene:
And when i click on add button it opens a dialog window where i have a textEdit object. Transfer coords in this editText. How could be this done? Forget to add that this cubes are imported from ads file. Probably it can help in any place.

In order to retrieve an object's coordinates from your scene, you'll need to add new event handler to your viewer. Let's call it a PickHandler.
Here's a basic code that will get you started. You'll need to add the "includes" and modify it to suit your needs. (Please note that I haven't tested it. I wrote it "by memory", but if there are any errors the should be very easy to fix).
PickHandler.h
class PickHandler: public QObject, public osgGA::GUIEventHandler {
Q_OBJECT
public:
PickHandler();
virtual bool handle( const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter& aa );
signals:
void query( osg::Vec3f );
protected:
virtual ~PickHandler()
{
}
bool pick( const double x, const double y, osgViewer::Viewer* viewer );
private:
bool getPickedPoint( const double x, const double y, float buffer,
osgViewer::Viewer* viewer, osg::Vec3f& point );
};
PickHandler.cpp
PickHandler::PickHandler() : osgGA::GUIEventHandler()
{
}
bool PickHandler::handle( const osgGA::GUIEventAdapter &ea,
osgGA::GUIActionAdapter &aa )
{
osgViewer::View* viewer = dynamic_cast<osgViewer::Viewer*>( &aa );
if( !viewer ) {
return false;
}
switch( ea.getEventType() ) {
default:
break;
case osgGA::GUIEventAdapter::RELEASE: {
if( pick(ea.getXnormalized(), ea.getYnormalized(),
viewer ) )
{
return true;
}
break;
}
}
return false;
}
bool PickHandler::pick( const double x, const double y,
osgViewer::Viewer* viewer )
{
if( !viewer->getSceneData() ) {
return false;
}
osg::Vec3f point;
float buffer = 0.005f;
if( getPickedPoint( x, y, buffer, viewer, point ) ) {
emit query( point );
}
return false;
}
bool PickHandler::getPickedPoint( const double x, const double y, float buffer,
osgViewer::Viewer* viewer,
osg::Vec3f& point )
{
osg::ref_ptr<osgUtil::PolytopeIntersector> intersector( 0 );
try {
intersector = new osgUtil::PolytopeIntersector(
osgUtil::Intersector::PROJECTION,
x - buffer, y - buffer, x + buffer,
y + buffer )
;
} catch( const std::bad_alloc& ) {
return false;
}
// DimZero = check only for points
intersector->setDimensionMask( osgUtil:: PolytopeIntersector::DimZero );
//
intersector->setIntersectionLimit( osgUtil::Intersector::LIMIT_NEAREST );
osgUtil::IntersectionVisitor iv( intersector );
viewer->getCamera()->accept( iv );
if( intersector->containsIntersections() ) {
osgUtil::PolytopeIntersector::Intersection intersection =
*( intersector->getIntersections().begin() )
;
const osg::Vec3f& P = intersection.intersectionPoints[ 0 ];
if( P.isNaN() ) {
return false;
}
point.set( P[ 0 ], P[ 1 ], P[ 2 ] );
return true;
}
return false;
}
I'm using a PolytopeIntersector, since I don't have any solid models, like the cessna in the OSG's example data; only a lot of points and using a LineIntersector (the fastest) is almost impossible to get a hit. The Polytope will build a frustrum volume intersects with anything in the area you have specified (with the parameters when constructing the Polytope).
Also, you might need to play with the parameters you send to the pick function, like the buffer size. I use ea.getXNormalized() and inside pick() an osgUtil::Intersector::PROJECTION value.
If you use, say an osgUtil::Intersector::WINDOW value, you don't need to normalize the mouse values. If you don't have any "strange" transformations in your view, most likely the PROJECTION value is what you need.
Last thing, this code is rather old. With newer osg versions, maybe some of it will be seen as deprecated. I'm not sure as I haven't updated yet my picker code.
Now, with this code, when an itersection is found, it retrieves the FIRST one and send the point values via an emit. You just have to connect this emit to your SLOT and receive the cliked point coords.
Lastly, for converting something to a string, you can use the Qt String function, QString::number(...).
For example:
const QString x = QString::number( point[0], 'd', 6 );
will stringify the x-coord of the point using fixed-point format with 6 decimal places.

Related

Write text input on the screen in SFML

So I'm creating a graphing calculator. I have an input string s. From the string, I can graph it using SFML. I start from the a MIN x-coordinate to a MAX x-coordinate, get the corresponding y from a EvaluateString() method, and all the coordinates to a VertexArray v. I wrote my method and the graphing method already and it all worked well.
However, I have a small issue. I want to input my string on the screen, such as "sin(cos(tan(x)))" like this. I'm struggling to find a way to do it. I kinda figured out it has to do with the event TextEntered, but still I can't find anything completely.
Please suggest me a way.
class Calculator{
public:
void main();
private:
WindowSize DefaultWindow;
sf::RenderWindow window;
Cartesian vertexX[2],vertexY[2];
sf::Vertex axis[4];
const double MAX = 10;
const double MIN = -10;
const double INCREMENT = 0.001;
};
int main(){
DefaultWindow.Max = Cartesian(10,10);
DefaultWindow.Min = Cartesian(-10,-10);
DefaultWindow.plane.width=1500;
DefaultWindow.plane.height=1500;
// Set up x and y-axis
vertexX[0] = Cartesian(-100,0);
vertexX[1] = Cartesian(100, 0);
vertexY[0] = Cartesian(0,-100);
vertexY[1] = Cartesian(0,100);
axis[0] = sf::Vertex(convertCartesiantoWindow(vertexX[0],DefaultWindow));
axis[1] = sf::Vertex(convertCartesiantoWindow(vertexX[1],DefaultWindow));
axis[2] = sf::Vertex(convertCartesiantoWindow(vertexY[0],DefaultWindow));
axis[3] = sf::Vertex(convertCartesiantoWindow(vertexY[1],DefaultWindow));
// Set up the window
window.create(sf::VideoMode(1500, 1500), "Graphing calculator");
// Input string
string s = "sin(cos(tan(x)))";
// Stack c contains all the Cartesian coordinate vertices
// Cartesian is a struct which contains x and y coordinates
Stack<Cartesian> c;
sf::VertexArray v;
// For a certain function in string s, I evaluate it
// and return the y_coordinate from the function EvaluateString (s, i)
// Push each (x,y) evaluated in the Stack c
for (double i = MIN; i <= MAX; i+= INCREMENT)
c.Push(Cartesian(i,EvaluateString(s,i)));
// v is VertexArray which contains all the vertices (x,y)
v = plot(DefaultWindow, c);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
}
}
}
// Draw the graph
window.clear(sf::Color::Black);
window.draw(axis,4,sf::Lines);
window.draw(v);
window.display();
}
As #super suggest, use a library would be a nice solution, and surely better than mine, but just in case this satisfies your needs, I implemented a super basic TextField class.
It may be plenty of errors, but it can gives you an idea on how to achieve that functionality.
A TextField is nothing more than a rectangle which contains a text. Since it will have a sf::Text, it must have a sf::Font. Additionally, I limit the number of characters that it will contain. In order for us to write inside the TextField, we have to know if it's selected, i.e. if it has the focus. So, a first approach could be:
class TextField : public sf::Transformable, public sf::Drawable{
private:
unsigned int m_size;
sf::Font m_font;
std::string m_text;
sf::RectangleShape m_rect;
bool m_hasfocus;
};
We need a constructor for this class:
class TextField : public sf::Transformable, public sf::Drawable{
public:
TextField(unsigned int maxChars) :
m_size(maxChars),
m_rect(sf::Vector2f(15 * m_size, 20)), // 15 pixels per char, 20 pixels height, you can tweak
m_hasfocus(false)
{
m_font.loadFromFile("C:/Windows/Fonts/Arial.ttf"); // I'm working on Windows, you can put your own font instead
m_rect.setOutlineThickness(2);
m_rect.setFillColor(sf::Color::White);
m_rect.setOutlineColor(sf::Color(127,127,127));
m_rect.setPosition(this->getPosition());
}
private:
unsigned int m_size;
sf::Font m_font;
std::string m_text;
sf::RectangleShape m_rect;
bool m_hasfocus;
};
We also need some basic methods, we want to get the text inside:
const std::string sf::TextField::getText() const{
return m_text;
}
and move it, placing it somewhere inside our window:
void sf::TextField::setPosition(float x, float y){
sf::Transformable::setPosition(x, y);
m_rect.setPosition(x, y);
}
this is a tricky one. We are overwritting setPosition method of sf::Transformable because we need to update our own m_rect.
Also, we need to know if a point is inside of the box:
bool sf::TextField::contains(sf::Vector2f point) const{
return m_rect.getGlobalBounds().contains(point);
}
pretty simple, we use cointains method of sf::RectangleShape, already in sfml.
Set (or unset) focus on the TextField:
void sf::TextField::setFocus(bool focus){
m_hasfocus = focus;
if (focus){
m_rect.setOutlineColor(sf::Color::Blue);
}
else{
m_rect.setOutlineColor(sf::Color(127, 127, 127)); // Gray color
}
}
easy one. For aesthetics, we also change the outline color of the box when focused.
And last, but not least, our TextField has to behave some way when input (aka an sf::Event) is received:
void sf::TextField::handleInput(sf::Event e){
if (!m_hasfocus || e.type != sf::Event::TextEntered)
return;
if (e.text.unicode == 8){ // Delete key
m_text = m_text.substr(0, m_text.size() - 1);
}
else if (m_text.size() < m_size){
m_text += e.text.unicode;
}
}
That delete key check is little dirty, I know. Maybe you can find better solution.
That's all! Now main looks like:
int main()
{
RenderWindow window({ 500, 500 }, "SFML", Style::Close);
sf::TextField tf(20);
tf.setPosition(30, 30);
while (window.isOpen())
{
for (Event event; window.pollEvent(event);)
if (event.type == Event::Closed)
window.close();
else if (event.type == Event::MouseButtonReleased){
auto pos = sf::Mouse::getPosition(window);
tf.setFocus(false);
if (tf.contains(sf::Vector2f(pos))){
tf.setFocus(true);
}
}
else{
tf.handleInput(event);
}
window.clear();
window.draw(tf);
window.display();
}
return 0;
}
Proof of concept:
std::string str;
sf::String text;
// In event loop...
if (event.Type == sf::Event::TextEntered)
{
// Handle ASCII characters only
if (event.Text.Unicode < 128)
{
str += static_cast<char>(event.Text.Unicode);
text.SetText(str);
}
}
// In main loop...
window.Draw(text);
This should create an sf::Event::TextEntered for input, and sf::String for output

QCustomPlot replot QCPLayer

I'm trying to figure out how to use QCPLayer to only replot certain items in the plot.
The qcustomplot documentation states this:
If you often need to call a full QCustomPlot::replot only because a non-complex object (e.g. an item) has changed while having relatively static but complex graphs in the plot, consider placing the regularly changing objects onto an own layer and setting its mode (QCPLayer::setMode) to QCPLayer::lmBuffered. This makes QCustomPlot allocate a dedicated paint buffer for this layer, and allows it to be replotted individually with QCPLayer::replot, independent of the other layers containing the potentially complex and slow graphs. See the documentation of the respective methods for details.
Which is what I'm trying to do in the example below:
I am creating a custom qcustomplot by inheriting from QCustomPlot:
QCustomPlot_custom.h
#pragma once
#include "qcustomplot.h"
#define USING_LAYER false
struct QCPCursor{
QCPItemLine *hLine;
QCPItemLine *vLine;
QCPItemText* cursorText;
};
class QCustomPlot_custom :
public QCustomPlot
{
Q_OBJECT
private slots:
void mouseMove(QMouseEvent*);
public:
QCustomPlot_custom(QWidget* parent = NULL);
~QCustomPlot_custom(){}
private:
QCPLayer* cursorLayer;
QCPCursor cursor;
void manageCursor(double x, double y, QPen pen);
public:
void init(QVector<double> xdata, QVector<double> ydata);
};
This class initializes with some data to plot. It also overloads the mouseMove event to control a custom cursor. USING_LAYER set to true means that the custom cursor is added to it's own layer (cursorLayer).
By setting USING_LAYER to false I get the desired effect as seen below:
The cursor is displayed by a horizontal and vertical line and the coordinates.
If I have many graphs in the plot and/or a lot of point in each graph, I will see a delay when moving the cursor. (Which is the reason I want to be able to replot only the cursor by setting it in a layer.)
QCustomPlot_custom.cpp
QCustomPlot_custom::QCustomPlot_custom(QWidget* parent)
{
connect(this, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
QCustomPlot::setInteraction(QCP::iRangeDrag, true);
QCustomPlot::setInteraction(QCP::iRangeZoom, true);
if (USING_LAYER){
this->addLayer("cursorLayer", 0, QCustomPlot::limAbove);
cursorLayer = new QCPLayer(this, "cursorLayer");
cursorLayer->setMode(QCPLayer::lmBuffered);
}
}
void QCustomPlot_custom::init(QVector<double> xdata, QVector<double> ydata)
{
this->addGraph();
this->graph(0)->setData(xdata, ydata);
QColor colorPen(10, 25, 180, 255);
QPen pen;
pen.setWidth(50);
pen.setColor(colorPen);
this->graph()->setLineStyle(QCPGraph::lsLine);
this->graph()->setPen(QPen(colorPen));
this->xAxis->setLabel("X-axis");
this->yAxis->setLabel("Y-axis");
this->rescaleAxes();
this->replot();
}
void QCustomPlot_custom::mouseMove(QMouseEvent* event)
{
//Cursor coordinates:
double x = this->xAxis->pixelToCoord(event->pos().x());
double y = this->yAxis->pixelToCoord(event->pos().y());
manageCursor(x, y, QPen(Qt::DashDotLine));
if (USING_LAYER)
cursorLayer->replot();
else
this->replot();
}
void QCustomPlot_custom::manageCursor(double x, double y, QPen pen)
{
if (cursor.hLine)
this->removeItem(cursor.hLine);
cursor.hLine = new QCPItemLine(this);
cursor.hLine->setPen(pen);
cursor.hLine->start->setCoords(-QCPRange::maxRange, y);
cursor.hLine->end->setCoords(QCPRange::maxRange, y);
if (cursor.vLine)
this->removeItem(cursor.vLine);
cursor.vLine = new QCPItemLine(this);
cursor.vLine->setPen(pen);
cursor.vLine->start->setCoords(x, -QCPRange::maxRange);
cursor.vLine->end->setCoords(x, QCPRange::maxRange);
//Coordinates as text:
if (cursor.cursorText)
this->removeItem(cursor.cursorText);
cursor.cursorText = new QCPItemText(this);
cursor.cursorText->setText(QString("(%1, %2)").arg(x).arg(y));
cursor.cursorText->position->setCoords(QPointF(x, y));
QPointF pp = cursor.cursorText->position->pixelPosition() + QPointF(50.0, -15.0);
cursor.cursorText->position->setPixelPosition(pp);
cursor.cursorText->setFont(QFont(font().family(), 8));
//Add to layer:
if (USING_LAYER){
cursor.hLine->setLayer(cursorLayer);
cursor.vLine->setLayer(cursorLayer);
cursor.cursorText->setLayer(cursorLayer);
}
}
The function that initializes the class member:
void Qt_PlotTest::testPlot(){
//Create some data and initalize plot:
QVector<double> yData, xData;
int imax = 100000;
for (int i = 0; i < imax; i++){
double x = double(i) / imax;
xData.push_back(x);
yData.push_back(pow(x, 2)*( 1.0 + 0.5*cos(20*x) + 0.1*sin(500*x - 0.1)));
}
ui.custom_QWidgetPlot->init(xData, yData);
}
When using the layer method, the cursor doesn't render. I tried understanding the documentation, but it is not clear for me how to correctly use QCPLayers.
How should I do this?
After adding layer
this->addLayer("cursorLayer", 0, QCustomPlot::limAbove);
don't call QCPLayer constructor to get layer pointer. Use provided getters with name of the layer or index:
QCPLayer * QCustomPlot::layer ( const QString & name) const
QCPLayer * QCustomPlot::layer ( int index) const
cursorLayer = this->layer("cursorLayer");
Also every Graph and Item is added to currentLayer and in your case it's not cursorLayer it's the main. You need to change current layer
bool QCustomPlot::setCurrentLayer ( const QString & name)
bool QCustomPlot::setCurrentLayer ( QCPLayer * layer)
I.e.:
this->setCurrentLayer("cursorLayer");
this->addGraph();
...
this->setCurrentLayer("main");
Or you can specify layer for each QCPLayerable
bool QCPLayerable::setLayer ( QCPLayer * layer)
bool QCPLayerable::setLayer ( const QString & layerName)
someGraph->setLayer("cursorLayer);
As #EligijusPupeikis reminded me, I am deleting and re-creating the cursor every time I move it.
I didn't think this would have any effect to my issue, but apparently it is, because reploting a layer which has new items in it requires that the plot has be reploted first (source: will check the qcustomplot doc and add link).
So my code now looks like this:
QCustomPlot_custom::QCustomPlot_custom(QWidget* parent)
{
connect(this, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
QCustomPlot::setInteraction(QCP::iRangeDrag, true);
QCustomPlot::setInteraction(QCP::iRangeZoom, true);
}
void QCustomPlot_custom::init(QVector<double> xdata, QVector<double> ydata)
{
this->addGraph();
this->graph(0)->setData(xdata, ydata);
QColor colorPen(10, 25, 180, 255);
QPen pen;
pen.setWidth(50);
pen.setColor(colorPen);
this->graph()->setLineStyle(QCPGraph::lsLine);
this->graph()->setPen(QPen(colorPen));
this->xAxis->setLabel("X-axis");
this->yAxis->setLabel("Y-axis");
this->rescaleAxes();
this->replot();
if (USING_LAYER){
this->addLayer("cursorLayer", 0, QCustomPlot::limAbove);
cursorLayer = this->layer("cursorLayer");
//cursorLayer = new QCPLayer(this, "cursorLayer");
cursorLayer->setMode(QCPLayer::lmBuffered);
}
//Cursor:
QPen qpen = QPen(Qt::DashDotLine);
cursor.hLine = new QCPItemLine(this);
cursor.hLine->setPen(qpen);
cursor.vLine = new QCPItemLine(this);
cursor.vLine->setPen(qpen);
cursor.cursorText = new QCPItemText(this);
cursor.cursorText->setFont(QFont(font().family(), 8));
//Add to layer:
if (USING_LAYER){
cursor.hLine->setLayer("cursorLayer"); //"cursorLayer"
cursor.vLine->setLayer("cursorLayer");
cursor.cursorText->setLayer("cursorLayer");
}
}
void QCustomPlot_custom::mouseMove(QMouseEvent* event)
{
//Cursor coordinates:
double x = this->xAxis->pixelToCoord(event->pos().x());
double y = this->yAxis->pixelToCoord(event->pos().y());
manageCursor(x, y);
if (USING_LAYER)
this->layer("cursorLayer")->replot();
else
this->replot();
}
void QCustomPlot_custom::manageCursor(double x, double y)
{
cursor.hLine->start->setCoords(-QCPRange::maxRange, y);
cursor.hLine->end->setCoords(QCPRange::maxRange, y);
cursor.vLine->start->setCoords(x, -QCPRange::maxRange);
cursor.vLine->end->setCoords(x, QCPRange::maxRange);
cursor.cursorText->setText(QString("(%1, %2)").arg(x).arg(y));
cursor.cursorText->position->setCoords(QPointF(x, y));
QPointF pp = cursor.cursorText->position->pixelPosition() + QPointF(50.0, -15.0);
cursor.cursorText->position->setPixelPosition(pp);
}
As a test, if I plot 10 000 000 points, and set USING_LAYER to false, I will notice a clear lag on the cursor when moving the mouse. While setting it to true, will result in a smooth cursor movement.

Cocos2d-x Polymorphism

I am currently working on subdividing my cocos2dx-cpp game into a more modular system. I want to have one layer to receive all Touches and direct those touches to the affected CCSprite-derived objects.
The derived objects are stored in a CCArray in an EntityManager (which helps me create and manage the entities).
The problem I am facing is that I can't seem to access the correct virtual method for my derived CCSprites.
Here is the code from my Touch layer (called TouchManager):
void TouchManager::ccTouchesBegan( cocos2d::CCSet* pTouches , cocos2d::CCEvent* pEvents )
{
cocos2d::CCSetIterator i;
cocos2d::CCTouch* touch;
cocos2d::CCPoint tap;
auto entities = EntityManager::sharedManager()->getVisibleEntities();
for ( i = pTouches->begin() ; i != pTouches->end() ; ++i )
{
touch = ( cocos2d::CCTouch* ) ( *i );
if ( touch )
{
tap = touch->getLocation();
for ( unsigned int entityIndex = 0 ; entityIndex < entities->size() ; ++entityIndex )
{
auto entity = entities->at( entityIndex );
// OLD: auto entity = ( TouchableSprite* )entities->objectAtIndex( entityIndex );
if ( entity->boundingBox().containsPoint( tap ) )
{
entity->setTouch( touch );
entity->onTouch( tap );
}
}
}
}
}
I want to have the TouchManager detect the entity that has been touched, and send the Touch to it. But there is my problem: it detects the touch but doesn't send it further. Either I have a crash or nothing at all.
I have created a Touchable interface class:
#include "cocos2d.h"
class Touchable : public cocos2d::CCSprite
{
cocos2d::CCTouch* m_pTouch;
public:
virtual cocos2d::CCTouch* getTouch();
virtual void setTouch( cocos2d::CCTouch* touch );
virtual void onTouch( cocos2d::CCPoint location ) = 0 ;
virtual void onMoved( cocos2d::CCPoint location ) = 0 ;
virtual void onEnded( cocos2d::CCPoint location ) = 0 ;
};
as well as a TouchableSprite base class:
#include "cocos2d.h"
#include "Touchable.h"
class TouchableSprite : public Touchable
{
//cocos2d::CCTouch* m_pTouch;
public:
//virtual cocos2d::CCTouch* getTouch();
//virtual void setTouch( cocos2d::CCTouch* touch );
static TouchableSprite* createSpriteWithFile( const char* fileName );
void resetPosition( float positionX = 0.0f , float positionY = 0.0f );
virtual void onTouch( cocos2d::CCPoint location ) ;
virtual void onMoved( cocos2d::CCPoint location ) ;
virtual void onEnded( cocos2d::CCPoint location ) ;
TouchableSprite(void);
~TouchableSprite(void);
};
with simple implementation (TouchableSprite.cpp):
#include "TouchableSprite.h"
TouchableSprite::TouchableSprite(void)
{
}
TouchableSprite::~TouchableSprite(void)
{
}
TouchableSprite* TouchableSprite::createSpriteWithFile( const char* fileName )
{
auto sprite = new TouchableSprite();
if ( sprite && sprite->initWithFile( fileName ) )
{
sprite->autorelease();
return ( TouchableSprite* )sprite;
}
CC_SAFE_DELETE( sprite );
// should not reach this point
return NULL;
}
void TouchableSprite::resetPosition( float positionX , float positionY )
{
this->setPosition( ccp( positionX , positionY ) );
}
void TouchableSprite::onTouch( cocos2d::CCPoint location )
{
}
void TouchableSprite::onMoved( cocos2d::CCPoint location )
{
}
void TouchableSprite::onEnded( cocos2d::CCPoint location )
{
}
And finally, here's my derived class (in this case, ControlStickSprite):
#include "cocos2d.h"
#include "RenderSystem.h"
#include "EntityManager.h"
#include "TouchableSprite.h"
class ControlStickSprite : public TouchableSprite
{
ControlStickSprite* m_sprite;
public:
cocos2d::CCNode* create( cocos2d::CCNode* parent );
void onTouch( cocos2d::CCPoint location ) ;
void onMoved( cocos2d::CCPoint location ) ;
void onEnded( cocos2d::CCPoint location ) ;
ControlStickSprite(void);
~ControlStickSprite(void);
};
with simple implementation for testing (skipping the Create part because it works):
void ControlStickSprite::onTouch( cocos2d::CCPoint location )
{
this->setScale( 0.5f );
}
void ControlStickSprite::onMoved( cocos2d::CCPoint location )
{
this->setPosition( location );
}
void ControlStickSprite::onEnded( cocos2d::CCPoint location )
{
}
Please help me get this working! I'm not too familiar with the usage of virtual methods so maybe I missed something there. I'm also relatively new to C++ and cocos2dx programming.
Thanks in advance!
EDIT: Thanks to #musikov for fixing the first part! I updated the above code to reflect the changes. I replaced the CCArray with std::vector< TouchableSprite* > to eliminate the need for casting the from CCObject*.
Now, I am facing the problem that when touched, ControlStickSprite::onTouch() is never chosen; it's always TouchableSprite::onTouch().
Added ControlStickSprite::create and EntityManager::createEntity methods:
My ControlStickSprite::create() method is like this:
ControlStickSprite* ControlStickSprite::create( cocos2d::CCNode* parent )
{
// auto parent = this->getParent();
auto entityType = "control-stick";
auto scale = 6.0f;
auto rotation = 0.0f;
auto positionX = RenderSystem::sharedRenderSystem()->getScreenWidth() * 0.9f ;
auto positionY = RenderSystem::sharedRenderSystem()->getScreenHeight() * 0.25f ;
auto sprite = EntityManager::sharedManager()->createEntity(
parent ,
entityType ,
scale ,
rotation ,
positionX ,
positionY
);
m_sprite = ( ControlStickSprite* )sprite;
return m_sprite;
}
which makes use of my EntityManager:
cocos2d::CCNode* EntityManager::createEntity( cocos2d::CCNode* parent , const char* entityType , float scale , float rotation , float positionX , float positionY )
{
std::string extension = ".png";
std::string fileName = entityType + extension;
auto entity = TouchableSprite::createSpriteWithFile( fileName.c_str() );
entity->setRotation( rotation );
entity->setScale( scale );
entity->resetPosition( positionX , positionY );
parent->addChild( entity );
// add to VisibleEntities vector
this->addEntity( entity , true );
return entity;
}
The only thing I can think of is that the createEntity() method creates a TouchableSprite* but returns a CCNode*, which I then cast to a ControlStickSprite*. Am I doing this wrong again? :)
Thanks for all your help!
You have wrong create method implementation in TouchableSprite and maybe in ControlStickSprite too.
Your create method creates Sprite instance and casts it to TouchableSprite class. It's completely wrong :)
That's why your program crashed on setTouch method call - because your calling this method in Sprite instance.
You need to change your create method:
TouchableSprite* TouchableSprite::createSpriteWithFile( const char* fileName )
{
auto sprite = new TouchableSprite();
if ( sprite && sprite->initWithFile( fileName ) )
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE( sprite );
// should not reach this point
return NULL;
}
Added controlstick implementation
ControlStickSprite.h
#include "cocos2d.h"
#include "RenderSystem.h"
#include "EntityManager.h"
#include "TouchableSprite.h"
class ControlStickSprite : public TouchableSprite
{
public:
static ControlStickSprite* create();
bool init();
void onTouch( cocos2d::CCPoint location ) ;
void onMoved( cocos2d::CCPoint location ) ;
void onEnded( cocos2d::CCPoint location ) ;
ControlStickSprite(void);
~ControlStickSprite(void);
};
ControlStickSprite.cpp
bool ControlStickSprite::init()
{
auto entityType = "control-stick";
std::string extension = ".png";
std::string fileName = entityType + extension;
if (initWithFile( fileName.c_str() )) {
// auto parent = this->getParent();
auto scale = 6.0f;
auto rotation = 0.0f;
auto positionX = RenderSystem::sharedRenderSystem()->getScreenWidth() * 0.9f ;
auto positionY = RenderSystem::sharedRenderSystem()->getScreenHeight() * 0.25f ;
sprite->setRotation( rotation );
sprite->setScale( scale );
sprite->resetPosition( positionX , positionY );
return true;
}
return false;
}
ControlStickSprite* ControlStickSprite::create()
{
auto sprite = new ControlStickSprite();
if ( sprite && sprite->init() )
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE( sprite );
// should not reach this point
return NULL;
}
EntityManager:
void EntityManager::addEntity( cocos2d::CCNode* parent , TouchableSprite* entity )
{
parent->addChild( entity );
// add to VisibleEntities vector
this->addEntity( entity , true );
}
Remember to call EntityManager::addEntity(parent, entity) after ControlStickSprite::create(), if you decide to use this solution

How should i scale this for multiple buttons in SDL?

I have a simple script which displays one button (which is a png image) and when the user clicks it the application quits.
But i want to add multiple buttons which is where im finding my current thinking will lead to a very long if:else situation and I am wondering if that is the only way.
This is how i have my current menu set up in a main.cpp file.
bool handle_mouse_leftClick(int x, int y, SDL_Surface *button) {
if( ( ( mouseX > x ) && ( mouseX < x + button->w ) ) && ( ( mouseY > y ) && ( mouseY < y + button->h ) ) ) {
return true;
} else {
return false;
}
}
This is my detection function.
Below is my main function which acts as my game loop, i've removed non relevant code to keep it easier to follow:
//menu button
SDL_Surface *button;
button = IMG_Load("button.png");
while(!quit){
//handle events
while( SDL_PollEvent( &event ) ){
switch(event.type){
case SDL_QUIT: quit = true; break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {
if(handle_mouse_leftClick(btnx,btny,button)){
quit = true;
}
}
break;
}
}
The issue is should my main.cpp have all this checking going on, is going to get very long very quickly when i add more buttons so I'm wondering if I have missed a trick to simplify my efforts?
When you get right down to the basic logic/computation, I would say the answer is "no", you haven't missed any tricks. I've never found a way around checking each target one at a time - at least in terms of computation. You could make your code cleaner a lot of ways. You could have a GuiElement class that exposes a "bool IsIn( int x, int y )" method. Then your big case statement would look more like:
bool handle_mouse_leftClick(int x, int y, SDL_Surface *button)
{
if (Button1.IsIn( mouseX, mouseY )
{
// do this
}
else if (Button2.IsIn( mouseX, mouseY ))
{
// do that
}
else
{
return false;
}
}
You could then further reduce amount code with a list or table:
int handle_mouse_leftClick(int x, int y, SDL_Surface *button)
{
for (unsigned int i = 0; i < buttonList.size(); i++
{
if (buttonList[i].IsIn( mouseX, mouseY ))
{
return i; // return index of button pressed
}
return -1; // nothing pressed
}
}
But it's still ultimately looking at each rectangle one at a time.
Couple caveats:
I don't think there's much real computational overhead in checking the hit boxes of each item - unless you're doing it at some crazy high frame rate.
Sure, you could optimize the checking with some kind of spatial index (like a b-tree or quad-tree organized by the locations of the buttons on the screen), but ... see #1. ;-)
If instead of 10 or 15 buttons/controls you have THOUSANDS then you will likely want to do #2 because #1 will no longer be true.
********* Update ***********
Here's a brief sample of a class that could be used for this. As far as .h vs main.cpp, the typical approach is to put the header in a "Button.h" and the implementation (code) in a "Button.cpp", but you could just put this at the top of main.cpp to get started - it has all the logic right in the class definition.
You'll notice I didn't really write any new code. The "IsIn()" test is your logic verbatim, I just changed the variable names to match the class. And since you already have a single button, I'm assuming you can reuse the code that renders that button the Render() method.
And lastly, if is not something you're familiar with, you don't have to create a list/vector at all. The code the renders the buttons could just call "okButton.Render()", followed by "cancelButton.Render()".
Sample "button" class:
class Button
{
private:
int m_x, m_y; // coordinates of upper left corner of control
int m_width, m_height; // size of control
public:
Button(int x, int y, int width, int height, const char* caption)
{
m_x = x;
m_y = y;
m_width = width;
m_height = height;
// also store caption in variable of same type you're using now for button text
}
bool IsIn( int mouseX, int mouseY )
{
if (((mouseX > m_x) && (mouseX < m_x + m_width))
&& ((mouseY > m_y) && (mouseY < m_y + m_height ) ) ) {
return true;
} else {
return false;
}
}
void Render()
{
// use the same code you use now to render the button in OpenGL/SDL
}
};
Then to create it/them (using the list approach):
Button okButton( 10, 10, 100, 50, "OK" );
buttonList.push_back( okButton );
Button cancelButton( 150, 10, 100, 50, "Cancel" );
buttonList.push_back( cancelButton );
And in your render loop:
void Update()
{
for (unsigned int i = 0; i < buttonList.size(); i++
{
buttonList[i].Render();
}
}

Algorithm for Calculating Directional Movement

I'm trying to write a basic Asteroids game in C++ using SFML. While I'm able to move my triangle (the player) around rather easily, the way in which it moves I'm not quite satisfied with.
Currently, the movement isn't relative to the direction it's facing. For example, if I press "w" to move forward, the triangle will simply move up the screen regardless of its direction.
What I'd like for the triangle to do is move forward when the "w" key is pressed, but make the actual direction its moving (in respect to the dimensions of the screen) relative to the actual direction its facing.
So, if the forward tip of the triangle is facing right, when "W" is pressed, it will move right. If "S" is pressed in this case, it will move left. "A" would make it move up, and of course "D" would make it move down.
My current code does not accomplish this one bit - I think there's a really hacky solution to this, but I'd rather approach this from a more mathematical and elegant perspective if possible.
So, what would be the best way to accomplish this?
I have three methods of relevance: one for handling keyboard input, the other for handling mouse movement, and an update method.
Code
void Game::OnKeyPress( void )
{
int count = 0;
const float playerCenterMag = Magnitude( mPlayer.GetShape().GetCenter() );
//Shoddy attempt resulting in absolutely nothing of note
const float moveRateX = ( cosf( mMouseAoR ) * playerCenterMag ) + mPlayer.MoveSpeed;
const float moveRateY = ( sinf( mMouseAoR ) * playerCenterMag ) + mPlayer.MoveSpeed;
if ( mInput.IsKeyDown( sf::Key::W ) ) // Up
{
mPlayer.Position.y -= moveRateY;
++count;
}
if ( mInput.IsKeyDown( sf::Key::S ) ) // Down
{
mPlayer.Position.y += moveRateY;
++count;
}
if ( mInput.IsKeyDown( sf::Key::A ) ) // Left
{
mPlayer.Position.x -= moveRateX;
++count;
}
if ( mInput.IsKeyDown( sf::Key::D ) ) // Right
{
mPlayer.Position.x += moveRateX;
++count;
}
std::cout << "Key Press Rate => " << count << " seconds" << std::endl;
}
void Game::OnMouseEvent( void )
{
mPlayer.GetShape().Rotate( mMouseAoR + 5 );
}
void Game::Update( void )
{
const int x = mInput.GetMouseX();
const int y = mInput.GetMouseY();
mMouseAoR = ( y == 0 ) ? 0
: tanf( x / y );
PrintInfo();
}
There are two versions of this game. If you want, say, 'W' to "thrust" forward, then you need to add moveRate at each cycle and use W to change the rate.
Here apparently you move only if a button is pressed, so you need to update always both X and Y:
if ( mInput.IsKeyDown( sf::Key::W ) ) // Up
{
mPlayer.Position.x += moveRateX;
mPlayer.Position.y += moveRateY;
++count;
}
The moveRate changes according to orientation. When moving laterally, you would do
mPlayer.Position.x += moveRateY;
mPlayer.Position.y += moveRateX;
when moving left (or right), and -= in the other direction.