Directional Lights
Asked Answered
B

3

11

I'm working on a game idea (2D) that needs directional lights. Basically I want to add light sources that can be moved and the light rays interact with the other bodies on the scene.

What I'm doing right now is some test where using sensors (box2d) and ccDrawLine I could achieve something similar to what I want. Basically I send a bunch of sensors from certain point and with raycast detect collisions, get the end points and draw lines over the sensors.

Just want to get some opinions if this is a good way of doing this or is other better options to build something like this?

Also I would like to know how to make a light effect over this area (sensors area) to provide a better looking light effect. Any ideas?

Bioscopy answered 16/10, 2012 at 22:26 Comment(2)
Can you give some more details about what kind of lighting effects you want to produce? e.g. Should the lights cast shadows, or be masked out in certain areas? Do you want specular or diffuse lighting on the bodies in the scene? etc.Donalddonaldson
Imagine an effect like a lantern, where the light "get darker" over the end of the range of the lantern.Theism
K
1

I can think of one cool looking effect you could apply. Put some particles inside the area where light is visible, like sparks shining and falling down very slowly, something like on this picture

Any approach to this problem would need to use collision detection anyway so your is pretty nice providing you have limited number of box2d objects.

Other approach when you have a lot of box2d objects I would think of is to render your screen to texture with just solid colors (should be fast) and perform ray tracing on that generated texture to find pixels that are going to be affected by light. That way you are limited to resolution not the number of box2d objects.

Kennithkennon answered 26/10, 2012 at 9:23 Comment(0)
L
0

There is a good source code here about dynamic and static lights in a 2D space. It's Ruby code but easy to understand so it shouldn't be long to port it to Obj-C/Cocos2D/box2D.

I really hope it will help you as it helped me.

Lenes answered 5/11, 2012 at 17:27 Comment(0)
M
0

Hm, interesting question. Cocos2D does provide some rather flexible masking effects. You could have a gradient mask that you lay over your objects, where its position depends on the position of the "light", thereby giving the effect that your objects were being coloured by the light.

Materials answered 7/10, 2013 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.