[RESOLVED] Using Ray Cast To Detect Camara Boundary Area

0 favourites
  • 12 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • WHAT I'M TRYING TO DO

    The custom Camera System I'm trying to set up is to have the 3D Camera follow behind the Player and rotate around accordingly. The Camera will be set to the end of the Camera Arm (which, itself, will be attached to the Player). The Camera will be set to the Arm's image point at the far end.

    As the Arm will do the actual rotating - the Camera being "along for the ride" - to avoid having the Camera on the opposite side of the "3D Walls" (a series of 2D sprites stacked via Z Elevation) and thereby having the view obstructed, I want the width of the Arm to be dynamically reduced to keep the Camera within the boundaries of the Wall. This would mean the Camera is momentarily brought closer to the Player...but as the Player moves away from the Wall, the Arm will grow back to its maximum width and the Camera will likewise be farther from the Player.

    I want to use Ray Casting (Line of Sight behavior given to the Arm) to determine if the Arm is overlapping a Wall and automatically adjust the Width of the Arm so that it is not extending past the closest edge of the Wall to the Player. This would keep the Camera inside the immediate surrounding boundaries of the Player.

    GREEN TRIANGLE: Player

    BLUE ARROW: Player facing direction

    GREEN ARROW: Indicates Player rotation

    RED BOX: Camera Arm (origin point is at the right-most end, attached to the Player)

    ORANGE ARROW: Ray Cast

    The Ray Cast is meant to say, "This far to the Wall from the Player...the Arm should not be any longer than this value."

    PROBLEM

    Besides the fact that this isn't working and thus the need for this post asking for help?

    Right now, the Arm is not updating its width.

    I added a Global Variable to test if the Ray Cast intersection is returning a True value. ...it is not.

    From reading the C3 Manual, It is my understanding that using the action Cast LOS Ray from /origin/ to /target/ will return a True or False and can be used in conjunction with the condition LOS Ray Intersected.

    The problem I'm facing is that there doesn't seem to be a True value returned when I cast the Ray. I want to specify which Wall to cast to and yet nothing works. The Global Variable always remains its initial value of 0.

    This is the latest configuration of events I've tried (I've had it used as a dual condition with LOS Ray Intersected before moving it up to event line 3):

    I would be glad for some insight and help to better understand how Ray Cast works so I can utilize it properly.

    THANKS!

    EDIT: PROBLEM RESOLVED

    I discovered that objects that could potentially register as a "hit" or intersection alone the Ray Cast line must have a Solid behavior. So not just any object in the Layout will trigger a Ray Cast "hit".

    This was tested by opening the "Instant hit laser" C3 example (find it through the Start Page tab) where I added a basic sprite. The laser when right through my sprite but not through the Rock sprites included in the example. I noticed a major difference between my sprite and the Rocks - the Solid behavior. I disabled the Solid behavior on the Rocks and the laser when through them, just as it did with my sprite. I further tested this in my project and added the Solid behavior on the sprite I was testing an intersection on...and the thing worked! A "hit" was detected.

  • Are your walls 2d, then you place 3d objects where the 2d walls are?

    Line of site is 2d only and doesn't work in 3d.

    If you want to do 3d ray casting you can use the third party 3dobject plugin which has it.

  • mOOnpunk - Yeah, I should have clarified that better (I updated my post accordingly).

    They are just 2D sprites in the layout and then At Start Of Layout, I stack a series of other sprites at increments along the Z axis. So, yeah, not real 3D walls.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I haven’t used the LOS behavior too much so I’m not able to reason about how to use it off the top of my head.

    You can do it without line of sight. Basically set the box to full width and if it overlaps a wall make it shorter till it isn’t. Here’s a rough example of that.

    Every tick
    — box: set position to player
    — box: set angle to player.angle+180
    — box: set width to 200
    
    While
    Box overlaps wall
    — box: set width to self.width-10
    
    Every tick
    — camera: set position to player
    — camera: set angle to player.angle
    — camera: move forwards -box.width pixels

    Only fail case you may or may not have to deal with is if the player is in the wall then it could cause an infinite loop. Quick fix is to use a repeat instead of a while so it would at least not freeze.

    It’s rough since it changes width 10px at a time. You can make the step shorter but that will increase the loop length. A more efficient method would be to start with big steps and refine it with smaller steps.

    Ideally if you used raycasting you could cast a ray and get the xy where it hits. Then if the distance from that point to the player is less than the default camera distance then use that instead.

  • R0J0hound That's not a bad idea. I can see that working and probably having the same issues you just mentioned. I'll give that a shot and try to refine it if the cos( ) and sin( ) expressions don't work the way I want them to...they should, though, since there's an Example in the C3 start page that literally uses those math expressions.

  • R0J0hound So, here's where I'm at...

    (I tried to upload a pic to this reply, buuuut, there was an error.)

    NOTE: "Self" here refers to the Camera_Arm object that has the LOS behavior.

    Set global variable RayCastTest = 0

    [Empty event]

    [Action] --> Cast LOS Ray

    --> From ( Self.X , Self.Y ) to ( Self.X + cos(Self.ImagePointX(1)) * Self.Width , Self.Y + sin(Self.ImagePointY(1) * Self.Width )

    [If LOS ray is intersected] --> [Set RayCastTest variable to 1]

    Despite copying this setup straight from two C3 examples included with the editor, I'm still getting a false return on the intersection check.

    Maybe this isn't about the trig but about my understanding of how ray casting works...

    It's my understanding that if a ray is cast, it's true if there is ANY object that intersects with the ray...correct?

  • Where is the imagepoint? If the origin is one one side and the imagepoint is on the other then casting from self.x, self.y to self.imagepointx(1), self.imagepointy(1) should do it.

    Or if you really want to use sin/cos you could do self.x, self.y to self.x+cos(self.angle), self.y+sin(self.angle)

    I’m only commenting on the math. Other than that the logic seems fine at a glance.

  • R0J0hound Correct. The Origin Point is at one end; the Image Point at the opposite end.

    I tried both of your suggestions and still nothing worked...

    Further, I disabled the collision check property of the Camera_Arm (thinking maybe that had an influence...it doesn't). At the suggestion of a friend, I have specified sets of events grouped together in different event sheets for organizational and optimization practices (he released BioGun recently and it's a HUGE project). The Camera system is in its own event sheet so I copied the event lines over to the main sheet for the layout just in case *that* was somehow an issue. Also...not an issue.

    I'm not sure if this is due to a lack of my understanding of how a Ray Cast is calculated and what exactly triggers a "hit" or if there's a bug...

  • Are you specifying what objects are an obstacle for the Los behavior?

  • R0J0hound I've tried it both with and without declaring an LOS obstacle.

    Just now, with the Ray Cast action set to cast line from (Arm.X, Arm.Y) to (Arm.ImagePointX, Arm.ImagePointY) after declaring the obstacle. It is still not registering a hit.

    https://drive.google.com/file/d/1Hc50kk934WHXPTTZ_twaW8lz76U1sj4W/view?usp=sharing

    This is the link to my current project if you want to have a look.

    Thank you for your help as always. :)

  • There’s nothing in that file.

    All a ray cast does is check for collision between two points. Quick test works fine.

  • R0J0hound Nothing in the file?? Odd. I set it so anyone with the link can view it.

    In any case, the example you just posted is basically the same setup as what I have as well.

    I tested something further...

    I opened the Instant Hit Laser example in the C3 example browser.

    Naturally, the use of Ray Cast intersection works...on the two Rock sprites. I added a new sprite but the laser object (via Ray Cast) doesn't detect the new sprite I added. It goes right through.

    But then I noticed something JUST NOW... SOLID BEHAVIOR!!!

    For some reason, Solid behavior is the key! I removed the Solid behavior from the Rock sprite objects in the example and the laser went straight through, just like my sprite that I added. I went back to my project and added Solid behavior to my Wall object and it worked!! The global variable I was testing with also changed to "1" to confirm the Ray Cast intersection.

    PROBLEM SOLVED!

    Again, thank you for your time in trying to help me sort through this!

    I'll keep on saying it every time: you are a blessing and I'm glad you're part of this community. I've learned a lot from you over the years.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)