Adam's Lair Forum

game development and casual madness
It is currently 2017/03/24, 10:14

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: 2017/02/09, 20:24 
Junior Member
Junior Member
User avatar

Joined: 2017/02/09, 19:26
Posts: 38
Role: Hobbyist
Hi, I'm new to Duality, and I must say it: The workflow is amazing! Really. The best I've ever seen, at least for 2D and C#.
Given how it works, I think soon I'll be able to contribute something ;)

Said that, I have a question. How can I detect if a sprite is overlapping another sprite (using parallax).
Gfycat Video - Click to Play. [Source]

I want/need to detect when the player is overlaping the black sprite, but I just don't know where to start.

I'm using rigid bodies, but given the parallax distance and the fact that the player must be able to pass through the black sprite I'm not sure if I can use that.

If anyone can help I'll be very grateful.


Last edited by YMR on 2017/02/10, 19:37, edited 2 times in total.

Top
 Profile  
 
PostPosted: 2017/02/09, 21:39 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 819
Location: Italy
Role: Hobbyist
Hello there, welcome to the forum! :mrgreen:

First of all, nice gif! it already has me intrigued.

For your question, maybe you could play with the GameObject hierarchy and add a child GameObject to your black sprite, which sits at the same Z as the player (to avoid the parallax issue - although it could introduce other side effects if you were to interact with it near the edges of the screen and, as you said, the parallax distance is significant) and that contains a RigidBody sensor, to detect when the player enters or leaves it.

Hope what I said is understandable :)

Good luck with your endeavors!

_________________
Come on Duality's Discord channel. It's entertaining and productive! :mrgreen:


Top
 Profile  
 
PostPosted: 2017/02/09, 22:58 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 1932
Location: Germany
Role: Professional
Hey there and welcome!

Given the parallax effect in your situation, you might have to roll a custom solution to detect overlaps in this case. What Duality can do for you is transform coordinates from world into screen space: Just ask the Camera that is rendering the scene:

Code:
Camera mainCam = this.GameObj.ParentScene.FindComponent<Camera>();
Vector3 screenPos = mainCam.GetScreenCoord(yourObjectPos);
// etc.


So you can transform the position of your character sprite into screen space, as well as the overlapping "negative" sprite.

For arbitrary polygons, you will probably have to transform all (world space) corner points of your "negative" polygon into screen space and then and then use math tricks to check if the sprite center is inside that polygon.

It's easier if your negative is always a rectangle - in that case, you can transform only its center into screen space and check if the character is inside the rect area by comparing coordinates. Should your rectangle be rotated like in the sample gif, you can rotate the screen space character position around the rectangle center in the opposite direction to cancel out the rotation and then do the check as if there was none.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/02/10, 03:07 
Junior Member
Junior Member
User avatar

Joined: 2017/02/09, 19:26
Posts: 38
Role: Hobbyist
Hi again, Thanks for the welcome and your quick answers!

Nothing to be intrigued yet (too pre-pre-pre-alpha) but it's good to know that it looks interesting ;)

The RigidBody approach was proving difficult, so I tried the camera approach, and now it's almost done :)
Gfycat Video - Click to Play. [Source]

To test it I just applied a red ColorTint to the body.
Here is the code:
Code:
Camera mainCam = this.GameObj.ParentScene.FindComponent<Camera>();
Vector3 playerScreenPos = mainCam.GetScreenCoord(playerTransform.Pos);

mainCam.RenderPickingMap(new Point2(1280, 720), false);
List<ICmpRenderer> renderers = mainCam.PickRenderersIn((int)playerScreenPos.X - h, (int)playerScreenPos.Y - v, h * 2, v * 2).ToList();

bool isOverlapping = renderers.Where(x => (x as SpriteRenderer)?.SharedMaterial.Name == "Transformer 01").Count() > 0;
Besides the remaining polishing, now my main concern is if I'm killing the performance by using the renderers. That, and to find a way to get the resolution of the viewport.

Thanks!

PS: For the GIF I used this amazing tool: ScreenToGif.


Last edited by YMR on 2017/02/10, 19:38, edited 1 time in total.

Top
 Profile  
 
PostPosted: 2017/02/10, 18:40 
Junior Member
Junior Member
User avatar

Joined: 2017/02/09, 19:26
Posts: 38
Role: Hobbyist
So far, my concern about performance seems baseless, and testing this with complex shapes looks right, so I think it'll stay this way.

Thanks a lot! I had no idea how to approach this, and now most of the base functionality is done. Of course, there's still a lot to do, but the game mechanics part is almost finished :D
Gfycat Video - Click to Play. [Source]

Oh, the resolution thing (DualityApp.TargetResolution) was easy to find thanks to the help docs :)


Last edited by YMR on 2017/02/10, 19:44, edited 1 time in total.

Top
 Profile  
 
PostPosted: 2017/02/10, 19:20 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 1932
Location: Germany
Role: Professional
The camera picking you appear to be using wasn't really what I suggested and it's quite slow in general, but if your tests indicate it's fast enough, keep it and I'm glad you found a solution that fits your needs xD (On the plus side, since camera picking is rendering-based, your shapes can be as complex as you want them to be if you use the alpha channel of their texture as a mask.)

Pro-Tip for sharing gifs: Use ShareX or a similar tool, or just upload your stuff to gfycat and then use the gfycat tag in the forum to replace bandwidth-heavy gifs with super-optimized play-on-click gif-videos. There are lots of connections where embedding a few gifs like that really slow down page loading.

Your game looks quite interesting btw. :+1:

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/02/10, 19:51 
Junior Member
Junior Member
User avatar

Joined: 2017/02/09, 19:26
Posts: 38
Role: Hobbyist
I think this render approach wont allow lots of 'inverted' shapes. I tested it with 100 and the performance was really not cool, but fortunately I won't need more than 5 per level, so for now it is fast enough :)

Thanks for the pro-tip! I just updated the posts :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group