I'm having a real hard time with finding the correct positioning of items in a scene.
1-rotate an item based on the angle of the mouse-to-item position. = Succesfull!
2-create an item, and send it from it's parent position to in direction to the mouse = Problems...
The following code is successful in other platforms such as Adobe Flash:
// When mouse is clicked =
Shot *tiro = new Shot();
double velocity = 10;
double radians = angle * PI / 180; // where angle is successfully defined previously
double velx = velocity * cos(radians);
double vely = velocity * sin(radians);
double x0 = ((redShip->scenePos().x()+redShip->boundingRect().width()/2)+50 ) * cos(radians);
double y0 = ((redShip->scenePos().y()+redShip->boundingRect().width()/2)+50 ) * sin(radians);
double x = x0 + velx ;
double y = y0 + vely ;
tiro->newShot(scene,x0,y0);
tiro->setDirection(x,x);
// When mouse is clicked =
Shot *tiro = new Shot();
double velocity = 10;
double radians = angle * PI / 180; // where angle is successfully defined previously
double velx = velocity * cos(radians);
double vely = velocity * sin(radians);
double x0 = ((redShip->scenePos().x()+redShip->boundingRect().width()/2)+50 ) * cos(radians);
double y0 = ((redShip->scenePos().y()+redShip->boundingRect().width()/2)+50 ) * sin(radians);
double x = x0 + velx ;
double y = y0 + vely ;
tiro->newShot(scene,x0,y0);
tiro->setDirection(x,x);
To copy to clipboard, switch view to plain text mode
Shot.cpp:
{
scene->addItem(ball);
}
void Shot::setDirection(double x, double y)
{
dirx = x;
diry = y;
}
//updatePos() is called every second by a QTimer in Shot::Shot()
void Shot::updatePos()
{
x += dirx;
y += diry;
ball->setPos(x,y);
}
void Shot::newShot(QGraphicsScene * scene, double x, double y)
{
ball = new QGraphicsEllipseItem(x,x,10,10);
scene->addItem(ball);
}
void Shot::setDirection(double x, double y)
{
dirx = x;
diry = y;
}
//updatePos() is called every second by a QTimer in Shot::Shot()
void Shot::updatePos()
{
x += dirx;
y += diry;
ball->setPos(x,y);
}
To copy to clipboard, switch view to plain text mode
The above just creates a shot anywhere in a up-down / left-right diagonal
and sends it following the said diagonal. What am I doing wrong (never Qt's fault...)
Bookmarks