Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

can't get Raycast collided object's parameters
Started by marcelopotty0 Dec 03 2018 04:26 PM

1 reply to this topic
raycast raycasting physics camera movement
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

marcelopotty0

[GLOBAL: userInfoPane.html]
marcelopotty0
  • Observer

  • 11 posts
  • Corona SDK

To make the character's movement I made something like this:

 

 local function onEnterFrame()

   -- "jugador" means player in spanish
   --"todoGroup" is the group containing all the elements

                     if gamePad.isMoving then
                    
                     if gamePad.isMovingUp then
                     jugador:translate(0, -1* (movementSpeed * deltaTime ))
                     todoGroup:translate(0, (movementSpeed * deltaTime ))
                     jugador:setSequence("deAtrasCaminando")
                     end
                    
                     if gamePad.isMovingDown then
                     jugador:translate(0, (movementSpeed * deltaTime ))
                     todoGroup:translate(0, -1* (movementSpeed * deltaTime ))
                     jugador:setSequence("deFrenteCaminando")
                     end
                    
                     if gamePad.isMovingLeft then
                     jugador:translate(-1* (movementSpeed * deltaTime ), 0)
                     todoGroup:translate( (movementSpeed * deltaTime ), 0)
                     jugador:setSequence("deIzquierdaCaminando")
                     end
                    
                     if gamePad.isMovingRight then
                     jugador:translate((movementSpeed * deltaTime ), 0)
                     todoGroup:translate(-1* (movementSpeed * deltaTime ), 0)
                     jugador:setSequence("deDerechaCaminando")
                     end
                           pasosPermitidos = true
                        else

                      jugador:play()

                    
     end 


     Runtime:addEventListener("enterFrame",onEnterFrame)
But when the character collided with any static object, the camera continued moving without the character
 
so I solved this with RayCasting:
 

 local function onEnterFrame()
                        
                        local deltaTime = getDeltaTime()
                    
        local choquesDer = physics.rayCast( jugador.x, jugador.y, jugador.x + jugador.contentWidth/2 + 5, jugador.y , "closest" )
        local choquesIz = physics.rayCast( jugador.x, jugador.y, jugador.x - jugador.contentWidth/2 - 5, jugador.y , "closest" )
        local choquesAba = physics.rayCast( jugador.x, jugador.y, jugador.x , jugador.y + jugador.contentHeight/2 + 5 , "closest" )
        local choquesArri = physics.rayCast( jugador.x, jugador.y, jugador.x, jugador.y - jugador.contentHeight/2 - 5 , "closest" )


                 
        if ( choquesDer ) then
             
           if choquesDer[1].position.x > jugador.x then
             gamePad.isMovingRight = false
          end

        end


        if ( choquesIz ) then
           if choquesIz[1].position.x < jugador.x then
             gamePad.isMovingLeft = false
             print(choquesIz[1].object)
          end

        end


        if ( choquesAba ) then
           if choquesAba[1].position.y > jugador.y then
             gamePad.isMovingDown = false
          end

        end


        if ( choquesArri ) then
           if choquesArri[1].position.y < jugador.y then
             gamePad.isMovingUp = false
          end

        end






                     if gamePad.isMoving then
                    
                     if gamePad.isMovingUp then
                     jugador:translate(0, -1* (movementSpeed * deltaTime ))
                     todoGroup:translate(0, (movementSpeed * deltaTime ))
                     jugador:setSequence("deAtrasCaminando")
                     end
                    
                     if gamePad.isMovingDown then
                     jugador:translate(0, (movementSpeed * deltaTime ))
                     todoGroup:translate(0, -1* (movementSpeed * deltaTime ))
                     jugador:setSequence("deFrenteCaminando")
                     end
                    
                     if gamePad.isMovingLeft then
                     jugador:translate(-1* (movementSpeed * deltaTime ), 0)
                     todoGroup:translate( (movementSpeed * deltaTime ), 0)
                     jugador:setSequence("deIzquierdaCaminando")
                     end
                    
                     if gamePad.isMovingRight then
                     jugador:translate((movementSpeed * deltaTime ), 0)
                     todoGroup:translate(-1* (movementSpeed * deltaTime ), 0)
                     jugador:setSequence("deDerechaCaminando")
                     end
                           pasosPermitidos = true
                        else
                         --jugador:setSequence("deFrenteCaminando")
                           pasosPermitidos = false
                     end 
                      jugador:play()



     end 


     Runtime:addEventListener("enterFrame",onEnterFrame)

And this worked, but I got a problem, and is that if the character collides with anything that is a physic body, it would stop, so I tried something like this:

        if ( choquesDer ) then
             
           if choquesDer[1].object.name == "borderRight" then
             gamePad.isMovingRight = false
          end

        end

But it returned the parameter choquesDer[1].object.name = nil

 

 

So, is there any way to get the object's parameters with raycasting?

 

or is there other more efficient way to make the character stop when collides with certain objects?

 

 

 

Thanks for reading, have a good afternoon :)



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 368 posts
  • Corona SDK

It's done exactly as you've already described.

choquesDer[1].object returns the display object and so choquesDer[1].object.name returns its name. Your error is most likely that whatever object the ray cast hits simply doesn't have a name. If you draw a line from where the ray is cast to where it hits, you'll be able to see where the problem is.

Now, there are several ways of stopping a character's movement when it collides certain objects, but without knowing anything about your project, I can't really offer any alternative ideas to just adding physics bodies to them. Read about collision filters in the docs if you want to create custom rules for which physics objects can collide with each others.


 

 




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: raycast, raycasting, physics, camera, movement