Cocos2d-x adding objects to isometric tilemap - cocos2d-x

I'm making game with isometric map using cocos2d-x (it's something like age of empires game, but simpler). I have "default" map with resources, trees and so on and user should be able to place new objects (buildings) to the map. And there is a problem: z order of the objects. When I add new object, it goes over all tiles on the map:
Image
User-added building (farm) should not go over the tree because it's "behind" it. I'm adding new objects to the map as child sprites to tile map:
CCSprite* tmpCastle=CCSprite::create("castleMenu.png");
tmpCastle->setPosition(touchLocation);
tileMap->addChild(tmpCastle);
I have no idea how to solve this.

I had a similar problem in a project I developed few months back. That's how I solved it:
1- Calculate z dynamically based on their position.
2- Set anchor point to ccp(0.5, 0) (as most of the items start from center bottom e.g, Tree)
3- The object that has lesser x and y co-ords are the items above.
So add the following code after your sprites:
tmpCastle->setAnchorPoint(0.5, 0);
int z = (TILEMAP_WIDTH - tmpCastle->getPositionX()) + (TILEMAP_HEIGHT
- tmpCastle->getPositionY());
tmpCastle->setZOrder(z);

Related

Rotate model in Autodesk Forge Viewer

Hi I am developing an application using the viewer api. I have a master model. And I load other models on this model. I can move and resize these models that I have uploaded later. I achieved rotation using makeRotationX ,makeRotationY,makeRotationZ functions. But when I rotate the loaded model, it moves it to the starting point. What could be the reason for this? So for example, I add the cube to the left, but it moves to the main 0,0,0 point and does the rotation.
I just change the rotation Y value and this is the result.
Code :
cubeModel.getModelTransform().makeRotationY(inputValue * Math.PI / 180);
viewer.impl.invalidate(true, true, true)
This is because the model's THREE.Matrix4 transform that you retrieve using model.getModelTransform() may already include some transformation (in this particular case, the transformation is offsetting the red cube to the left). And the makeRotationY method doesn't append any transformation, it simply resets the matrix to a new transformation that only rotates around the Y axis.
Instead, what you'll want to do is something like this:
let xform = model.getModelTransform().clone();
// modify the xform instead of resetting it, for example
let rotate = new THREE.Matrix4().makeRotationY(0.1);
let scale = new THREE.Matrix4().makeScale(0.1, 0.5, 0.1);
xform.premultiply(rotate);
xform.multiply(scale);
// since we cloned the matrix earlier (good practice), apply it back to the model
model.setModelTransform(xform);

Mapbox GL JS: Stacking order of features in layer – what determines it? Can you change it?

I have a vector tile layer with about ~120 features ranging in size from over a million acres down to less than 20. I am having issues with mouseover whereby the large features are occluding the small ones, which are behind them.
I assume this has to do with the source data, which was sorted alphabetically by location name. As it happens, "Carson National Forest," which is big, is on top of "Valle Vidal" which is much smaller. So I can't select the latter with mouse interaction.
Is the solution to sort the features in ascending order by acreage and reimport the dataset, or is there something I can do within the Mapbox GL JS API to swap their positions?
Thanks!
There are two solutions to your problem.
I'll assume that your polygons have an area attribute.
Find the smallest polygon in mouseover
First, big polygons don't actually block small polygons from firing a "mouseover" event. The event is fired with all the features underneath the mouse. So you could intentionally select the smallest one.
map.on('mouseover', e => {
e.features.sort((a, b) => a.properties.area - b.properties.area);
const smallestFeature = e.features[0];
});
This method also works if you don't have an area attribute - you could use Turf to calculate it dynamically, although it would be incorrect at tile boundaries.
Use fill-sort-key to sort polygons
The other method is to use the fill-sort-key attribute in the layer definition:
layout: {
'fill-sort-key': ['-', ['get', 'area']]
}

Rotation issue While Reflected (2D Platform Game)

My player's arm is programmed to follow my mouse and rotate accordingly and I've programmed bullets to be fired using this rotational value
(Math.atan2(this._dy, this._dx) * 180 / Math.PI
where _dy is the y location of the mouse (-) the y of my player's arm and the _dx is the x location of mouse (-) the y of my player's arm.
However, when I program the player to reflect when the mouse has crossed the x-coordinates, the bullet angle is also reflected. How would I fix this issue?
I've already tried subtracting 180 from the angle but it still doesn't fire towards the direction of the mouse.
First, make sure you have this parent-child-sibling relationship:
"A" should be the parent of "B" and "C". "B" and "C" should have no direct link. Their connection is that they have the same parent. So when you want to move the character, move the parent, and both will move. Now, for the good stuff:
Use key frames and sibling relationship
beginner level approach
Make the character and the arm both children of the same parent display object container (Movie Clip in this case). Now, instead of flipping anything by xScale, which I assume you did, you can just have both MC children (arm and character) go to frame 2 (or whatever is available) where the graphics are flipped.
xScale body, move arm to frame 2, change z order
moderate level approach (best result)*
Alternatively, you could do that same "sibling" setup as above, and then scale the character but not the arm (I think scaling the arm will mess it up again, but you could have the arm go to frame 2 and have it drawn reversed so the thumb and handle are pointing the right way. Bonus points for changing the z stacking order so the arm goes to the other side of the body. xScale for only the body allows you to only have one set of frames for animation of his legs and torso etc. but also avoid scaling the arm at all).
Global properties
advanced approach
A third option is to use global rotation and global points. I won't illustrate that here because I'm not that advanced and it would take me a while to figure out the exact syntax. If you already have mastered global properties, try this; if not, try one of the ones above.
* Example (best result)
if (facingRight == true && stage.mouseX < totalChar.x){
// totalChar is on the stage
// and contains two children:
// armAndGun and bodyHeadLegs
totalChar.armAndGun.gotoAndStop(2);
// in frame 2 of the arm MC, draw the
// arm and gun in the flipped orientation
totalChar.addChild(bodyHeadLegs);
// re-ads body to parent so it's
// z-order is above the arm;
totalChar.bodyHeadLegs.xScale = -1;// flips body and any animation of legs and head
facingRight = false;
// use a variable or property like this
// to keep him from constantly flipping
}
You'll need similar code to flip him back the other way.

KineticJS Layers Layering

I have one stage and 4-5 layers with images and shapes on every layer. Every time I add an object in a layer that layer takes a greater z-index , index and comes at the top of the other layers.
.moveToTop .setZIndex or other functions don't work on layers. I tested it with the latest version 4.3.0.
How can I define the z-index of the layers and stay that way each time I add/edit an object?
Please help.
When you first create the layers, you have:
layer1 = new Kinetic.Layer();
layer2 = new Kinetic.Layer();
Then you add them to the stage:
stage.add(layer1);
stage.add(layer2);
This adding to stage defines your z-index initially;
To change the index you have to do layer1.remove() to remove it from the stage and then add it again to bring it to the top.

Java3D Zoon- Object starts disppearing az I zoom on it

First of all, I would like to say that I'm a newbie in Java3D. Please bear with my ignorance.
I have made an application with Java3D and I have the following problems with zooming.
It seems the MouseWheelZoom behaviour of Java3D moves the object along Z-axis. On the scene my Z-axis is not out of plane so by using MouseWheelZoom , the object doesn't get closer but it get out of screen. Is there a way to set the zoom direction to an arbitrary direction?
I have got around the problem by using MouseWheelListener and changing the viewing platform based on zoom steps. But there is another problem now. As the object gets closer than a certain distance, some parts of the object ( usually the corners) start disappearing so I can't zoom as much as I desire.
Could you please help ?
Regards,
Hassan
Question:
I guess you use an OrbitBehavior for MouseControl like:
orbit = new OrbitBehavior(canvas3d, OrbitBehavior.REVERSE_ALL);
If that is the case then try
orbit.setZoomFactor(-1d);
To reverse the zoom direction (the default zoom factor is +1d).
To your 2. Question:
You have to set a BoundingLeaf on your PlatformGeometry to encapsulate your "viewing area".
Try something like this
defaultBounds = new BoundingSphere(new Point3d(radiusGameMap, 0.0, radiusGameMap),
radiusGameMap * 6.0d);
BoundingLeaf boundingLeaf = new BoundingLeaf(defaultBounds);
PlatformGeometry platformGeom = new PlatformGeometry();
platformGeom.addChild(boundingLeaf);
where radiusGameMap is a double defining the radius of your whole map.