How to add 3D Sprite on tmx file background using Cocos-2dx? - cocos2d-iphone

We have been working on sample game, wherein we want to show 3D sprite on .tmx file(background). But 3D sprite is never shown up, though we tried setting tileMap->setGlobalZOrder(-1).
Following is the sample code:
CCTMXTiledMap tileMap = new CCTMXTiledMap();
tileMap->initWithTMXFile("map.tmx");
tileMap->setGlobalZOrder(-1);
//tileMap->setGlobalZOrder(-1000);
this->addChild(tileMap, 0);
Sprite3D player = Sprite3D::create("player.c3b");
player->setScale(10.0f);
player->setPosition3D(Vec3(200, 500, 0));
this->addChild(player, 1);
But 3D Sprite is never shown up. Kindly help.
Your help is highly appreciated!
Thanks.

Set position to tile map and
Add player (sprite3d) as a child of your tileMap (background).
Try this:
winSize = Director::getInstance()->getVisibleSize();
tileMap->setPosition(winSize.width*0.5,winSize.height*0.5);
...
player->setPosition(tileMap->getContentSize.width*0.5,tileMap->getContentSize.height*0.5);//----relative position of your player,you can acc. to requirement
tileMap->addchild(player,1);
this may solve your problem.

Related

Touch detection for SpriteBatchNode

I am curious if anyone knows how to detect when a SpriteBatchNode has been touched since it's BoundingBox is always null. This is how I detect touch for single sprites.
Node *parentNode = event->getCurrentTarget();
Vector<Node *> children = parentNode->getChildren();
Point touchPosition = parentNode->convertTouchToNodeSpace(touch);
for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
childObject = *iter;
if (childObject->getBoundingBox().containsPoint(touchPosition)){
//do something
}
But in most cases I want my sprites to be animated hence using SpriteBatchNode. Any ideas? Can I get the BoundingBox of the grandchildren since they are a series of sprites?
Depends on which method you are using.
Armature skeletal animation: Are you using the cocostudio skeletal animation tool to create your animations? If you use that you will get a node with the correct bounding box wrapping your sprite tightly and adjusting when bones change position
Sprite sheet animation: If you are using a sprite sheet with a .plist file you can inspect the size reflected in the .plist file and set your batchNode size to the biggest one you find, or dynamically adjust it based on the sprite currently being shown. I think cocos does this by default.
Loading sprite frames: If you are loading individual sprites using spriteFrames, you can inspect the contentSize of the spriteFrame and set your bounding box manually.
I have used all 3 and were always able to get the boundingBox size. Let me know if this helps. I used this article to learn sprite sheet animations and just played around with cocos skeletal animation and figured that out as I was experimenting with it.
Well I figured it out by getting the BoundingBox of the grandchild, which is a sprite. I was then able to do whatever I wanted to the spritebatchnode.

Centering view on a moving position in SFML

I want to use sf::View in SFML in order to change the position of the view, such that the player sprite is always in the center of the screen. Thus I want to write a function which allows me to input a set of coordinates and thus center the screen around those coordinates. In addition I want to be able to set a limit to this, such that when the player reaches the side of the map, that axis of the camera stops following the player, as it has reached a "limit". How do I achieve this?
Thank you in advance.
The function you need is called sf::RenderWindow::setView .
Do something like this:
sf::RenderWindow window (sf::VideoMode(800,600),"Test");
sf::View view ();
view.setCenter (/*Set Center here*/);
window.setView (view);

Qt GraphicsItem transformation affects all items in scene

I have this embedded Qt application that uses the QGraphics framework to display a web view.
The dimensions of the web view are 1280*720 pixels, and the QGraphicsView is set to render the scene at these coordinates (0,0, 1280x720).
I'm trying to add a loading indicator on the top right corner (at 1100,50), which is a simple PNG image that I rotate every now and then using a QTimeLine.
Code looks like this (I found the transformation trick on the internet):
// loading_indic initialization:
QGraphicsPixmapItem *loading_indic =
new QGraphicsPixmapItem( QPixmap("./resources/loading_64.png") );
loading_indic->setPos(QPoint(1100.0,50.0));
QTimeLine timeline = new QTimeLine(1000);
timeline->setFrameRange(0,steps);
connect(timeline, SIGNAL(valueChanged(qreal)), this, SLOT(updateStep(qreal)));
timeline->start();
// called at each step of a QTimeLine:
void updateStep(qreal step) {
QTransform transformation = QTransform()
// place coordinate system to the center of the image
.translate( width/2.0, height/2.0)
// rotate the image in this new coordinate system
.rotate(new_angle)
// replace the coordinate system to the original
.translate( -width/2.0, -height/2.0);
loading_indic->setTransform(transformation);
}
Now, my problem is that when doing this, it looks like the WebView is translated as well, resulting in everything being displayed in the center of the screen.
Result looks like this:
The webview is supposed to fill the screen, and the loading indicator should be on top right...
My scene contains only two items:
Scene
|
\____ QGraphicsWebView
\____ QGraphicsPixmapItem // loading indicator
What am I doing wrong here?
Solved my problem..
I don't know why, but it looks like adding this PNG item to the scene was screwing up with the scene's rectangle.
Doing this:
_scene.addItem(loading_indic);
loading_indic->setPos(1100.0, 50.0);
_scene.setSceneRect(0.0,0.0,1280.0,720.0); // resets the scene's rectangle ?!
loading_indic->startAnimation();
solved the problem. Now my items are correctly placed on screen.
If somebody has an explanation to this, I'll gladly accept his answer.

Qt GUI Development - Displaying a 2D grid using QGraphicsView

I'm new to Qt development so I've being trying to research a solution to a user interface I need to design. My project is to simulate players in an online game moving around a global map. To represent the map I need to display a 2D grid, with each space in the grid representing a region of a map. I then need to display the location of each player in the game. The back-end is all fully working, with the map implemented as a 2D array. I'm just stuck on how to display the grid.
The research I have done has led me to believe a QGraphicsView is the best way to do this, but I can't seem to find a tutorial relevant to what I need. If anyone has any tips on how to implement this it would be much appreciated.
Thanks, Dan
A 2D Grid is nothing more than a set of horizontal and vertical lines. Suppose you have a 500x500 map and you want to draw a grid where the distance between the lines in both directions is 50. The sample code that follows shows you how you can achieve it.
// create a scene and add it your view
QGraphicsScene* scene = new QGraphicsScene;
ui->view->setScene(scene);
// Add the vertical lines first, paint them red
for (int x=0; x<=500; x+=50)
scene->addLine(x,0,x,500, QPen(Qt::red));
// Now add the horizontal lines, paint them green
for (int y=0; y<=500; y+=50)
scene->addLine(0,y,500,y, QPen(Qt::green));
// Fit the view in the scene's bounding rect
ui->view->fitInView(scene->itemsVBoundingRect());
You should check the QGraphicsView and the QGraphicsScene documentation as well as the corresponding examples. Also you can watch the graphics view training videos or some graphics view related videos from the Qt developer days.
Well if you have a constant grid size or even a limited number of grid sizes what i like to do is to draw a grid block in gimp or any other program and then set that as the background brush (draw only bottom and right side of the block) qt will repeat the image and will give you a full grid. I think this is good for performance too.
This is the grid image i used in one of my programs it's 10x10 pixels.
Then call QGraphicsScene setBackgroundBrush as the follwing:
scene->setBackgroundBrush(QBrush(QPixmap(":/grid/grid10.png")));
The more native way is this:
scene = self.getScene() # Your scene.
brush = QBrush()
brush.setColor(QColor('#999'))
brush.setStyle(Qt.CrossPattern) # Grid pattern.
scene.setBackgroundBrush(brush)
borderColor = Qt.black
fillColor = QColor('#DDD')
rect = QRectF(0.0, 0.0, 1280, 720) # Screen res or whatever.
scene.addRect(rect,borderColor,fillColor) # Rectangle for color.
scene.addRect(rect,borderColor,brush) # Rectangle for grid.
Sorry by PyQt...
Suppose a scene is set to the graphicsview then simply below one line will show the grid.
ui->graphicsView->scene()->setBackgroundBrush(Qt::CrossPattern);
There several other values can be passed for ex: Qt::Dense7Pattern
These are members of enum BrushStyle, just click on any used value in Qt creator and it will take you to the enum declaration where you can see all other possible values.
PS:
A scene can be set like this:
ui->graphicsView->setScene(new QGraphicsScene());

Cocos2d show only a part of a CCSprite

Is there any possibility to show only a part of an CCSprite?
It seams that contentSize property doesn't have a good result.
I think you might have to create a new sprite for this. The general pseudo code is this.
CCTexture2D *origTexture = originalSprite->getTexture();
CGRect rect = {0, 0, 20, 20};
CCSprite *destSprite = CCSprite::spriteWithTexture(origTexture, CGRect);
Both doc_180's and James' answers work by creating new CCSprite using a portion of the texture, but if you are using clipping method, you will get CCSprite that uses the full texture but have the ability to only draw a portion of it on screen. One advantage of this method is you are able to modify how big or small the portion that you want shown or hidden on the fly rather than having to re-create the CCSprite again and again (or replacing the texture again and again).
So, to use the clipping method, simply download the ClippingNode class from here, and add the CCSprite you want clipped to that ClippingNode. Then you call one of its methods to specify which region to limit the drawing to. I'm currently using it to create a progress bar so I know for sure it works great.
Get the [sprite displayedFrame], change the frame of that, and create a new sprite with that spriteframe: CCSprite *sprite2 = [CCSprite spriteWithSpriteFrame:frame]