Hello guys
I have a small problem while designing a iphone game with a grid using cocos2d.
The game needs a 10x10 grid in the middle of the screen (it is not covering the entire screen).
A line is drawn at runtime where the user touches two points in the grid.
Question: would tilemap be ideal for this problem? As i need to verify the co-ordinates do belong to the grid or not when the user touches a point would tilemap be useful?
Question: Is there any better way of solving this in cocos2d. Please help me out.
Thanks
I wouldn't recommend using tilemap for this. Personally I'd do it all with math.
Lets for arguments sake say your grid squares are 10px by 10px.
You now instantly know the positions of the rects for each square.
top right square would be (90, 0, 10, 10), this obviously doesn't include the positioning of your grid, but you can easily add that onto this by adding.. (90+gridPos.x, 0+gridPos.y, 10, 10).
Then you just check your touches intersect the rects of the grids.
Drawing a line is fairly simple, i imagine you'd draw it from the center of the 2 grid points.
So if the line started in the top right grid square it's initial point would start at (90+gridPos.x, 0+gridPos.y, 5, 5), or (90+gridPos.x, 0+gridPos.y, gridSquareHeight/2, gridSquareWidth/2)
Using cocos2d it's pretty easy to also make every square a touchable sprite, that can react when touched however you like, sending a message back to a delegate or even just doing a visual effect.
There are tonnes of possibilities for solving this problem.
Related
9 circles in a 3 x 3 grid with random lines on three of them connecting them to other circles (image)
I'm trying to more of less re-create the image above using react. The circles are div blocks and I have no clue how to go about drawing the lines that "connect" one circle to the other.
I tried using react-lineto, but it's simply not rendering anything when I use it. I can use the hr tag and use position absolute or something and just play around with where it's placed by toying with top and left props, but I don't think I'd be able to create diagonal lines.
Open to whatever packages or strategies you guys think could help me connect two dots (which are styled div blocks) with a line that could be vertical, horizontal, or diagonal.
Below is what I added to use react-lineto that didn't work. Straight from the documentation. I don't care to get react-lineto working as much as I just care to be able to accomplish this task in general.
<div className="A"></div>
<div className="B"><div/>
<LineTo from="A" to="B" />
I am using the TiledMap editor, with a set of Tiles I got from opengameart,
I am using an isometric map, yet I just can't get these tiles to connect without this "stair-stepped" effect.
Any help is appreciated.
The editor "blindly" renders rows of tile-images, These rows are too far apart, leading to this staircase effect. Reduce the map tile height (65 in your screenshot) so that it fits.
I have created a grid in Qt of the size of graphics view. Whenevr I zoom in/out, the grid is visible in a small area. Drawing is possible in the entire area but grid is confined to a small area.
I have created the grid using the following code:
for(int x = 0; x <= ui->graphicsview->width(); x += 10){
scene->addLine(x,0,x,ui->graphicsView->height(),QPen(Qt::green));
}
for(int y = 0; y <= ui->graphicsView->height(); y += 10){
scene->addLine(0,y,ui->graphicsView->width(),y,QPen(Qt::green));
}
How do I make the grid infinite so that on zoom out, the grid still persists over the area equal to size of graphics view?
You can draw "infinite" lines with the QGraphicsView framework.
Subclass the QGraphicsView or the QGraphicsScene and implement your grid drawing in the QGraphicsView::drawBackground() or QGraphicsScene::drawBackground().
In the drawBackground() method you have to compute intersection points between exposed rectangle (the rect argument of this method) and your grid lines.
Then, use the QPainter::drawLine() with the computed intersection points to draw lines.
I know, this is quite brief explanation of the concept, but I hope it will help.
If not, I can explain it in detail. A couple years ago a was facing similar problem.
The view is only the size of the "camera" looking into a fraction of the scene. If the scene isn't populated with really long lines in all directions, it won't show up on the view when you zoom out. Making the lines the size of the current view is almost worthless once the view changes. Decide what "infinity" means for your application and add all the appropriate lines to the scene. You may need a loading screen as your program is adding all of them.
Or you could do some lazy loading, where you only add the lines to the scene, as the view is panned or zoomed out.
Look into the example of the 40000 chips included with the Qt libraries to see how to do nearly infinite objects, and how to handle level of detail changes as you start to zoom out really far.
Hope that helps.
I'm working on a simple widget system, and I'm implementing some containers right now.
Here's the situation I find myself in:
I have a Widget base class, a Container class, which is a widget that can contain other widgets, and several widget sub classes like Button.
I have two types of container: Container itself, which positions children absolutely, and Box, which will stack widgets next to each other, either horizontally or vertically.
Each widget draws itself at x=0, y=0. Therefore, containers need to add an offset to the drawing context before the widgets are told to draw themselves.
Each widget does its own hit testing based on its x/y position.
So far, it works fine. But it falls apart now that I'm implementing Box: What I do is that I overwrite the drawfunction inherited from Container to draw them all in next to each other, instead of based on their x/y position. Quite simple.
But event handling is totally off now, as the widget's x/y position has become meaningless.
I think I have two options:
Have the widget do hit testing at position x=0, y=0, like drawing. Then recalculate the mouse position to match that in Container.
Make each layout set x/y for its children, and make children draw themselves at their x/y position again. No more offsets for the drawing context
The first one is a bit ugly, I think. The second one is pretty complicated to implement, since I need to react to position changes in widgets.
How to other widget systems like Qt, Gtk and wxWidgets generally tackle this? I've looked at the source of some of these, but can't quite figure that out, it's too sophisticated. I don't have any resizing or packing issues to consider.
You are trying to implement your own layout system. You should expect it to be difficult.
I would advise against the first method. The x,y coordinates of a widget are not only used by the widgets themselves, but by anyone outside of the container who wants to do something with the widget.
The second solution is what I've chosen to implement custom widgets made of several smaller widgets and it's not that hard to put together if you don't want too many features.
Just get the widgets when they are added to your container, set their position to the current free spot, and move on to the next.
I want to know how Qt does a border when using QPainter's drawRect. The reason for this is I am trying to draw three rectangles next to each other, but I'm having trouble getting them to touch perfectly at all pen sizes.
QPainter's documentation for drawRect says:
A stroked rectangle has a size of [the input rectangle] plus the pen width.
So it goes like this:
I just wanted to add to the answer and address the question about truncation.
Truncation might be happening because you are using the QRect and not QRectF. QRectF gives you floating point precision. Similarly, you can use QPen::setWidthF(qreal width) to ensure your border is not getting truncated either.