[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

## Basic trig question related to raycasting. Started by Appletreeman Dec 27 2018 05:00 AM

6 replies to this topic
maths trigonometry physics raycast

Best Answer davebollinger , 27 December 2018 - 05:23 AM

why can't you use a fixed distance?  it'll simplify the problem.  presumably you're just trying to prevent the player from shooting off-screen targets, right?  so shoot a ray of fixed length (>= longest screen axis) and if you do hit something then check that something's coordinates and see if IT is on-screen, else ignore the hit.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

### Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
• Contributor

• 420 posts
• Corona SDK

Hi all,

A quick and (hopefully) easy question with regards trig/raycasting.

In my game I have a player that can move anywhere on the screen and can face in any direction.

One of the weapons available is a laser so what I'd like to do when the player fires it is to raycast in the direction that the player is facing to the edge of the screen and then have the first hit returned.  What I'm struggling with is the target x/y for the ray cast as it's not a fixed distance from the player and maths/trig still remains a dark art to me   I hope my explanation makes sense.

Any help at all would be appreciated.

Many thanks

Chris

[TOPIC: post.html]
#2

### davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
• Corona Geek

• 1,276 posts
• Enterprise

why can't you use a fixed distance?  it'll simplify the problem.  presumably you're just trying to prevent the player from shooting off-screen targets, right?  so shoot a ray of fixed length (>= longest screen axis) and if you do hit something then check that something's coordinates and see if IT is on-screen, else ignore the hit.

• Appletreeman likes this

[TOPIC: post.html]
#3

### Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
• Contributor

• 420 posts
• Corona SDK

why can't you use a fixed distance?  it'll simplify the problem.  presumably you're just trying to prevent the player from shooting off-screen targets, right?  so shoot a ray of fixed length (>= longest screen axis) and if you do hit something then check that something's coordinates and see if IT is on-screen, else ignore the hit.

That's a very good idea which I quite clearly didn't think of.  I'll see if I can figure out the maths by myself but if not then I'll be back

[TOPIC: post.html]
#4

### XeduR @Spyric

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

• 428 posts
• Corona SDK

It's exactly as Dave said.

Just use a fixed distance for the raycasting. If you take the character's rotation, you'd find the end location of the ray by using x = cos(angle)*fixedDistance and y = sin(angle)*fixedDistance. Then you only need to check if the ray hit somewhere, at which point you could draw the laser from the character to the hit location. Otherwise you'd draw it from the character to the calculated end location of the ray.

I'd recommend that you take a look at the raycast sample project in Corona. It's quite easy to follow and should include everything that you need, including making a cool laser weapon that would bounce off surfaces if you wanted.

[TOPIC: post.html]
#5

### roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
• Corona Geek

• 7,463 posts
• Corona SDK

@Appletreeman,

Are you talking about using the built-in physics raycasting mechanism or a DIY mechaism?

If the latter, when you cast your ray are you treating the bodies the ray may 'hit' as circles, rectangles, or polygons?  My suggestion here, and you may already be doing this, is to treat each body you are testing for a hit as a circle.  This will allow you to devolve the comparisons to a squared-length  and a single distance making the entire thing very efficient (it not always 100% perfect visually)

re: squared length - I probably wasn't entirely clear here.  You want to avoid the use of sqrt() calls.  You can do this by treating all distances as squared distances.

• maximum ray distance becomes distance * distance

Hopefully this doesn't further confuse things.

Edited by roaminggamer, 27 December 2018 - 12:31 PM.

[TOPIC: post.html]
#6

### Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
• Contributor

• 420 posts
• Corona SDK

Cheers for all the help guys.  I'll be having another crack at it this evening when I get back from visiting family.

Will report back to let you know how I get on.

[TOPIC: post.html]
#7

### Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
• Contributor

• 420 posts
• Corona SDK

Apologies for the delay in replying.

@Roaminggamer - I'm using the built-in physics raycasting.

I went with Daves' suggestion of using the fixed length like thus:

```local tx = self.x - (math.cos(math.rad(self.rotation+90)) * w )
local ty = self.y - (math.sin(math.rad(self.rotation+90)) * w )
local hits = physics.rayCast(self.x, self.y, tx, ty, "closest")
```

and it works like a charm.

many thanks for the help.

[topic_controls]

[/topic_controls]