Light Surface Moves When it Shouldn't

K

krisstastic

Guest
I'm using a simple "light" effect with surfaces. It all works great, but when I walk, the light surface moves in parallax to the character. For example, if I walk to the right, you can see the light surface move to the left slightly and then follow the view accordingly. The character has code that moves the view in a way that slows down the view so it isn't so jumpy. This code moves the light surface, but it shouldn't.

The Light Creation code:
CREATE EVENT-

globalvar light;
light=surface_create(view_wview,view_hview)
brightness=200
col=make_color_hsv(80,0,brightness)

STEP EVENT-

col=make_color_hsv(80,0,brightness)
surface_set_target(light);
draw_set_color(col);
draw_rectangle(0,0,room_width,room_height,false);
surface_reset_target();

DRAW EVENT-

draw_set_blend_mode(bm_subtract);
draw_surface(light,view_xview,view_yview);
draw_set_blend_mode(bm_normal);

Light source code:
END STEP EVENT-

size=sprite_get_height(sprite_index)-16;
draw_set_blend_mode(bm_subtract);
surface_set_target(light);
draw_ellipse_color(x-size/2-view_xview,y-size/2-view_yview,x+size/2-view_xview,y+size/2-view_yview,c_white,c_black,false);
surface_reset_target();
draw_set_blend_mode(bm_normal);

Character camera code:
STEP EVENT-

view_xview[0]+=((x-(view_wview[0]/2))-view_xview[0])*0.1
view_yview[0]+=((y-(view_hview[0]/2))-view_yview[0])*0.1

Removing the camera code fixes it, but I've gotten it to work before, I just don't remember how. Any help is appreciated.
 
S

shanedaman112

Guest
Looks like this may be your problem
draw_ellipse_color(x-size/2-view_xview,y-size/2-view_yview,x+size/2-view_xview,y+size/2-view_yview,c_white,c_black,false);
Try putting size/2 in parenthesis. So x-(size/2)-view_xview,y-(size/2)-view_yview, etc...
If that doesn't work, keep in mind I rarely use surfaces so I'm a bit rusty with them.
 
K

krisstastic

Guest
Looks like this may be your problem
draw_ellipse_color(x-size/2-view_xview,y-size/2-view_yview,x+size/2-view_xview,y+size/2-view_yview,c_white,c_black,false);
Try putting size/2 in parenthesis. So x-(size/2)-view_xview,y-(size/2)-view_yview, etc...
If that doesn't work, keep in mind I rarely use surfaces so I'm a bit rusty with them.
That did something, made it better definitely. I just found a fix though too, it had to do with the depth of the systems? The Light Creator depth had to be more than the Light Source depth. I still don't understand why that matters though.
 
S

shanedaman112

Guest
Hmm weird. I don't know why depth would matter if it's a 2d game. Anyway, glad I could help.
 
K

krisstastic

Guest
Hmm weird. I don't know why depth would matter if it's a 2d game. Anyway, glad I could help.
So I was wrong. I tried what you said and it didn't fix it. Changing the depth makes the bug less apparent, but when I add fog, its obvious its not fixed. What I need is the light source to not move, but it does no matter what unless I take out the Characters camera code? I don't know what I need to change, or what part may be written incorrectly, since I've gotten it to work fine before. I followed this tutorial
 
S

shanedaman112

Guest
Could you link your source code? Since you're not sure where the problem is coming from, letting me poke around a little would be faster than posting code snippets. And the tutorial probably won't help unless I can compare it to your code, sorry.
 
K

krisstastic

Guest
Could you link your source code? Since you're not sure where the problem is coming from, letting me poke around a little would be faster than posting code snippets. And the tutorial probably won't help unless I can compare it to your code, sorry.
All the code for the light creator, and light source are at the top. The Characters code is just regular platforming stuff, with the camera code in its Step Event.
What other code do you need me to post?
 
S

shanedaman112

Guest
All the code for the light creator, and light source are at the top. The Characters code is just regular platforming stuff, with the camera code in its Step Event.
What other code do you need me to post?
Ok, in that case, I'll study through the posted code again.
 
K

krisstastic

Guest
Here's what happens when you walk all the way to the right. The lights move to the left. The character is the purple block
 

Attachments

S

shanedaman112

Guest
I'm really stumped... There may be a problem we just don't see. Do you have multiple views on? Im thinking it has to do with your camera code
 
K

krisstastic

Guest
I'm really stumped... There may be a problem we just don't see. Do you have multiple views on? Im thinking it has to do with your camera code
Theres only one view. Deleting the camera code fixes the issue, but I dont know why?
 
S

shanedaman112

Guest
Try this as your camera code
var wv,hv;
wv = view_wview/2
hv = view_hview/2
view_xview = max(0,min(room_width-view_wview,x-wv))
view_yview = max(0,min(room_height-view_hview,y-hv))
 
K

krisstastic

Guest
Sorry to reply so late, I don't get much time to work. But that worked perfect!! Completely fixed the issue, thank you. Would you mind, if you can, explaining why that works?
 
S

shanedaman112

Guest
I'm not sure if you got that camera code from the video, but it's really inefficient. It's constantly adding and subtracting a sum that depends on where things were last step and the math is really messy. What I did was just have the camera stay directly centered on the player at all times. The vars wv and hv I declared are exactly 1/2 the width and height of the view. If you didn't mind if the view went beyond the room boundaries, you could just have
Code:
view_xview=x-wv
view_yview=y-hv
However, you probably wouldn't want the view going into the great void (the outside of the room). So, I used some basic math. min(x1,x2,x3,etc) picks the smallest of the numbers entered. max(x1,x2,x3,etc) picks highest number. So using the min and max functions it uses the logic
Code:
if(x-wv < 0) then view_xview = 0
else if(x-wv > room_width-wv) then view_xview = room_width-wv
else view_xview = x-wv
It then repeats that similar logic with view_yview. Hope that makes sense and glad I was of help.
 
Last edited by a moderator:
D

Diveyoc

Guest
Do you think I have a similar problem? I have a day/night cycle and when my lights draw everything is fine, except when I move, the lights move with me.
Here's my surface/light draw event and a photo of what happens:

if surface_exists(global.surf)
{
surface_set_target(global.surf);
draw_clear(c_black);
with (obj_light)
{
draw_set_blend_mode(bm_subtract);
draw_sprite_ext(sprite_index, image_index,x, y, 2, 2, 0, c_white, 1); (I've tried different variations of the x,y, position but can't get anything to work.)
draw_set_blend_mode(bm_normal);
}
surface_reset_target();
draw_surface_ext(global.surf,view_xview, view_yview,1,1,0,c_white,alpha);
}

https://drive.google.com/open?id=0B4rFK6Lf8hKFQk1ETTVHQUxocU0
 
D

Diveyoc

Guest
Actually, just figured this out with help from Pixelated_Pope.

Changed:
draw_sprite_ext(sprite_index, image_index,x-view_xview,y-view_yview , 2, 2, 0, c_white, 1);
 
Top