In SpriteKit, didEndContact occurs even though the sprites are still overlapping - events

Why? I can clearly see the two sprites are overlapping! I am killing myself for hours and hours over having the contacts precise and consistent, but can't find a way to do it. Either the didBeginContact or didEndContact is triggered in the wrong time. Does anyone have a consistent solution for this? I tried changing the anchorPoints to various positions (instead of (0.5, 0.5) I tried other values, like (0.5, 0)), but that seems to have messed all the things up pretty badly. I can't set sprites to have physicsBody rectangle based, because I have non-rectangular sprites (obviously). So my sprites' physicsBody-s are mostly texture based. I can see with my own two eyes that sprites are still overlapping and haven't ended the contact, and the event still triggers!!! Why is this happening? I have the scene being the contact delegate and everything else is properly connected. The sprites (let's call them SKSpriteNode A and SKSpriteNode B) that collide are on the scene and have contact bitmasks set properly for each other. The scene is the only contact delegate for the physicsWorld, so only the scene can trigger and go in the event handler, so I know it is working - at least some of the time. Code in scene:
-(void) didEndContact:(SKPhysicsContact *)contact {
NSLog(#"didEndContact occured!");
}
I have my custom class StaticLevelElement set so it extends SKSpriteNode:
#interface StaticLevelElement : SKSpriteNode
The B has been set as such StaticLevelElement with a triangular image:
StaticLevelElement * B = [StaticLevelElement spriteNodeWithImageNamed:#"triangle"];
I tried to set the B's physicsBody to have texture based physicsBody:
B.physicsBody = [SKPhysicsBody bodyWithTexture:[SKTexture textureWithImageNamed:#"triangle"] size:B.size];
And while the sprites A and B are still colliding and overlapping, the didEndContact occurs! I don't want it triggered when the sprites are still overlapping! How can I resolve this?

Try not to use the texture based physicsBody, but either the polygon based, or rectangle based physicsBody.

Related

Why will my physics 2D raycast not display or work correctly?

Basically i wanted to make a raycast for jumping but for some reason the distance of the raycast is wrong and never changes and the position of it is so wrong i cant even seem to find it:
ray2D = Physics2D.Raycast(transform.position, -Vector2.up, .1f, GroundedLayers);
Debug.Log(ray2D.distance);
isGrounded = groundCheck();
Grounded layers do not include the player, distance always returns as 0, grounded is always false and the gizmos line only draws when i open the prefab and not at all in the scene:
here the distance is set to .1f but displays the same as 1f
(EDIT: ok so i checked and for some reason the raycast happens around 0, -1 even tho thats nowhere near the player transform?)
Try Vector2.down instead?
Alternative things to try:
Is the player in the scene the same as the prefab?
Increase the distance.
Add a new GameObject at the feet of the player, reference that in your script and call the Raycast from there.
Is ray2d a RayCastHit2d?
Are your ground object tags correct, including all grammer/caps?
Try outputting in debug, ray2d.collider.name to see what you're actually hitting.
If you're using a rigid body, try rigidbody2d. position instead of transform.position
Do you have the player selected in the hierarchy? This is needed to see the gizmos.
I found that the raycast was detecting an invisible trigger so i had to put it on another layer :)

Group of objects on top but added to a camera

As a precision I already noticed threads about this but didn't find a way to achieve exactly what I need.
Basicaly I have a board of objects that I need remaining always on top of everything but also attached to the camera.
I first tried to add the group to the camera and it stayed as wished always in the viewport. But in this configuration the group of objects still be a part of the scene so while zooming to regular objects in the "editor" the board goes into/among these objects of the scene.
My second trial was based on this thread and work wonderfully in order to get all of the board objects rendered above everything. But on such a configuration when rotating around the axis (with orbit control) both scenes rotates. So I tried to update the foreground scene with coordinates of the camera but the update was not immediate and this scene is flickering (I suppose that while rotating the update function is not called immediately).
My best wish would have been to "attach" the foreground scene to the camera so that it would stay on top and sticked on the screen/viewport but I don't even know if it is possible and how to do that (as only groups of objects seem to be capable to be attached to the camera).
I am really stuck on that point. Thanks you for any help!
If this is what you need,
just set object.material.depthTest = false; and object.renderOrder = 1000; for all objects you need to be always on top and attached to the camera.

Three.js Position and visibile issue

Currently, I am trying to dive deeper into grouped objects or better hierachic ordered objects. However I got a strange issue with the position and the visibility of a object3D and childs of those.
So i have a set of objects, a light source with is the main object so far, and a few spheres for example, which are childs of the the main object.
The problem is, that child objects, which are positioned behind another object ( siblings ) ( from the camera view ) are visible. And all childs objects appear infront of the main object while those actually positioned behind the main object.
Sadly i can't reproduce this in a codePen so i had to take some pictures of the scene, many apologies for that.
In the pictures, the camera rotates clockwise around the light source (main object to the left).
So basically what I am doing is:
mObj = new THREE.Object3D();
mObj.add(new THREE.Mesh(someGeometry, someMaterial);
cObj = new THREE.Object3D();
cObj.add(new THREE.Mesh(someGeometry, someMaterial);
mObj.add(cObj);
scene.add(mObj);
Is that related to the object hierachic order or something else?
The second more negligible issue is, that on one of my pc's, those parts of objects which are dark (because of no lighting), generate those strange black/grey faces, which i cant explain. Maybe Graphicscard/driver or something?
What's the distance between these objects? It could be a floating point rounding issue. I've run into it myself.
If it's so, and you're getting flickering models, you'll need to keep your camera and the active model at the origin, and move the universe around you to keep precision in the important places (near the player).

Scroll view when character reaches border

I'm making a game similar to Mario. At the moment my character can walk left and right and cannot go further when it reaches the border. Now, I want the view to scroll when the character is x amount of pixels away from the border. It shouldn't scroll just horizontally, but vertically as well.
At the moment I have 1 class that extends cclayer. This class draws a sprite and blocks. I have searched where to go from here, but I haven't really found anything useful/up to date.
So would be great if someone has some tips.
Also, in the mentioned class I also create a box2d world with gravity. If I have to make multiple layers do I create multiple box2d worlds for each layer?
You can use CCFolow action. This follow a node. Example:
CCSprite * hero = CCSprite::create("hero.png");
CCLayer * gamelLayer = GameLayer::create();
CCFollow *follow = CCFollow(hero, CCRect(0, 0, 4056, 3062));
gameLayer->runAction(follow);
When you change position of hero, the gameLayer move too, but always we see on the screen fragment of CCRect(0, 0, 4056, 3062). In this situation you don't need to use Scrolview, but normal CCLayer.

How to animate horizontal bar with right cap, by UIView animation in iPhone

I got wiered but unsurprising animation from call UIView animation, I don't know if it works the same by using CALayer animation. I guess it is.
please see this picture: what I want is to animate a horizontal bar growing horizontally, what I do is:
Subclass a UIView, called UIViewBar, and in its drawRect:rect method, I draw the shape of rectangle and a right cap, it's done by
CGContextMoveToPoint A
CGcontextAddLineToPoint B
CGContextAddArcToPoint D
CGContextAddLineToPoint C
//then close the path and fill the context
Then I call this UIViewBar (initWithFrame:rect) in my UIViewController, and the shape is what I want, looks fine. BUT when I perform [UIView animation] say from original rect to rectGrew it does not perform nicely. Instead, it perform as the picture says.
Well, after thinking, it's not totally unreasonable because when the UIView horizontally stretches itself, it does not know the cap will not be changed. So is there another way to do this?
Actually, I'm very new to drawing, graphic, and animation. What I know is that (if wrong please kindly correct me):
If I want to "draw something", I should override the UIView's drawRect method, and this is done by initing the UIView, should not or better not to be called outside. Say I drew a rectangle in a UIView by CGContextFillRect.....
But what if I want to animate this rectangle? I cannot make animation based on Quartz2D, but from UIView animation or CALayer(deeper tech), so What I have to do is to make the rectangle itself as a seperate UIViewRect seperately, so I can call the [UIViewRect animation] method, AM I RIGHT ON THIS
If so, I have to make every rectangle that I want to animate to be UIView respectively. Doesn't it affect performance? Or it is just the way apple prefers to do ?
Any suggestion will be much helpful, GREAT thanks.
Wow, it's the first time that no answer posted at all! TO answer my question: it requires to consider animate custom property of CALayer. The custom property is the AB width (the width without cap width, that should work). If someone wants detailed code. Please google custom property animate in CALayer.

Resources