Jump to content


Photo

Laser/light stream bouncing [SOLVED]


  • Please log in to reply
1 reply to this topic

#1 Dilbertguy

Dilbertguy

    GMC Member

  • New Member
  • 42 posts

Posted 21 April 2011 - 01:20 PM

EDIT: I solved it! Since the light object has no mask, instance_position is the more appropriate function in this case.


I'm attempting to make a game where you have a laser (or in this case, a stream of light) that bounces off of mirrors to reach some kind of goal. I'm guessing you've played one of these games before, as they were popular back in the day (Aargon, anyone?).

Anyhow, I'm running into some issues with the light stream. My current system has obj_light, which has a direction set in the creation code (not the event!). It will calculate which direction it is going, determine if it needs to change direction (in the case of a mirror) or stop (a wall), draw the corresponding sprite to what it did, and move to the next spot. Here's the code (in the draw event):
var done, xx, yy, sprite, dirwas;
xx = x                      //set variables. xx and yy are position of current piece of light stream
yy = y
done = 0                    //setting done to 1 will break you out of the loop
dirwas = 0                  //used for making sprite not malfunction on straight pieces
sprite = 0                  //the image_index of the light stream piece we are placing each iteration of the loop


if dir = 360                //Ensure dir is within readable ranges (0-270)
    dir = 0
if dir > 360
    dir -= 360


do
{
    if dir = 0              //If you are going this direction (left)
    {
        sprite += 1         //Add 1 to sprite (sprites use binary to determine sprite to use)
        dirwas = 0          //Tell the code later what the direction was at first
    }
    if dir = 90             //If not that direction, check other directions
    {
        sprite += 2
        dirwas = 90
    }
    if dir = 180
    {
        sprite += 4
        dirwas = 180
    }
    if dir = 270
    {
        sprite += 8
        dirwas = 270
    }
    
    show_error(string(xx),0)                //Tell me where you are currently
    show_error(string(yy),0)
        
    if instance_place(xx,yy,obj_mirror1)    //If we are over a mirror of a certain orientation (mirror1)
    {
        if dir = 90 or dir = 270            //Determine which way to turn the light
            dir += 90
        if dir = 0 or dir = 180
            dir -= 90
        show_error("MIRROR HIT",0)          //Tell me that you hit a mirror.
    }
    if instance_place(xx,yy,obj_wall)       //If you hit a wall
    {
        show_error("WALL HIT",0)            //Tell me
        done = 1                            //Stop the loop (break added for extra protection against infinite loops)
        break
    }
    
    
    if dir = 360                            //Make sure dir is within acceptable ranges
        dir = 0
    if dir > 360
        dir -= 360
    
    if dir = 0                              //If the dir has changed, add to variable sprite
    {
        if dirwas != 0
            sprite += 1
    }
    if dir = 90
    {
        if dirwas != 90
            sprite += 2
    }
    if dir = 180
    {
        if dirwas != 180
            sprite += 4
    }
    if dir = 270
    {
        if dirwas != 270
            sprite += 8
    }
    if !done
        draw_sprite(spr_light,sprite,xx,yy) //If we didn't finish the light stream, draw a piece of it.
    show_error(string(sprite),0)            //Tell me what the image_index is.  
    if dir = 0                              //Move to the next spot according to the new direction.
    {
        xx += 32
    }
    if dir = 90
    {
        yy -= 32
    }
    if dir = 180
    {
        xx -= 32
    }
    if dir = 270
    {
        yy += 32
    }
    
    if xx < 0                               //If we're outside the room, end.
    or yy < 0
    or xx > room_width
    or yy > room_width
    {
        break
        done = 1
    }
    sprite = 0                              //Reset variables
    dirwas = 0  
}
until (done)
(If it helps, I put it up on pastebin here.)

It's a little messy, so I commented it so you can tell what I'm having it do.

Now, the problem: It goes through all mirrors/walls. It just goes straight down (dir = 270 in the creation code) and off of the screen, going right through an instance of obj_mirror1 and an instance of obj_wall. I checked the x and y positions of these instances and compared it with the variables returned in the loop (via errors), and they match! I can't figure out why it's not working and I am entirely baffled. If anyone has any insight into this, that'd be great! Now solved.

Edited by Dilbertguy, 21 April 2011 - 04:26 PM.

  • 0

#2 Gamer3D

Gamer3D

    Human* me = this;

  • GMC Member
  • 1591 posts
  • Version:GM8.1

Posted 22 April 2011 - 08:23 PM

Time to introduce you to vectors.

If light is moving along the vector [lx, ly] and the reflective surface has a normal vector (a vector perpendicular to it) of [nx, ny], then we calculate the reflected light vector [rx, ry] like this:

var dot;
dot = 2 * (lx * nx + ly * ny) / (nx * nx + ny * ny);

rx = lx - nx * dot;
ry = ly - ny * dot;

For this calculation, I have assumed that [nx, ny] is non-zero.

Eh. You say you solved it, but still, this should help you clean up your code, so I'll post it anyway.
  • 2




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users