x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Beginner Question: Finding a random point on a line trace

I read somewhere someone offering this as a solution for random spawning of actors, I can't find the example for the life me. My goal is to spawn actors randomly from a line trace originating from a character inside a static mesh building. Guards inside a castle'

I know one node should be a random float in range connected to a make a vector X, but how do i constrain that to a line trace?

Can someone show me an example that would work with 10+ actors spawning at the same time? My vector length is about 1000

alt text

alt text

this is me guessing

alt text

alt text

Product Version: Game Mod
Tags:
more ▼

asked Sep 21 '15 at 10:21 PM in Blueprint Scripting

avatar image

Samson F.
56 29 17 25

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

You're trying to spawn creatures at random along a line segment? That's pretty straight forward... Here are the things you already should know:

The start vertex

The end vertex

The length of the segment pseudo-code (end - start).Length()

Are there any obstacles blocking the line segment? To find out, this is where you would use the line trace. Look at the hit result and see if there are any hits.

Now, you can generate ten random numbers between 0 and the segment length. These will be the spawn distances from the start vertex.

Next, we need to find the actual spawn positions in 3d space along the line segment. This might be the part which confuses you. You have to do SegmentEnd - SegmentStart to get a vector which has an origin at [0,0,0]. The direction of this vector will be the same direction as your segment. To get the LOCAL spawn position along this vector: Normalize this vector, then multiply it by the random numbers you generated. This is a randomly scaled position along the local vector. We're not done yet. Now, you have to translate this position from local space to world space by adding the SegmentStart position to the randomly generated point. Now, this point is in 3D space along the line segment.

We're still not done though! The actual spawn position may not exactly be on this line segment. If your terrain has a valley or a hill, you want to spawn your creature along the contour of the terrain along this line. To do this, you do another line trace from a high position (spawnpos + (0,0,1000)) to a reasonably low position (spawnpos - (0,0,1000)). Find the intersection point -- this should be your terrain or some other object. This is the final spawn position for your creature.

more ▼

answered Sep 22 '15 at 12:59 AM

avatar image

Slayemin
819 49 29 73

avatar image Samson F. Sep 22 '15 at 02:40 AM

Confused with vector length vs vector origin, alt text

avatar image Samson F. Sep 22 '15 at 03:11 AM

I got your response in my inbox accidently deleted my comment, thank you again

"Let's say you have the vector [5,0,0].

A vector has a direction and a magnitude.

The direction is a line starting at the origin [0,0,0] and going to [5,0,0].

The magnitude is the length of the vector, which in this case is 5. The magnitude of the vector is disconnected from its direction, so no matter what direction it faces, its length will stay the same. The magnitude/length of a vector is always measured as its distance from [0,0,0].

I this case, if we normalize [5,0,0], it becomes a unit vector of [1,0,0]. This is now a direction vector, with a magnitude of 1. Notice how we can change the magnitude of the vector without changing its direction? We take advantage of this to get a random point on your line.

We can now take this unit vector and multiply it by any number we choose -- let's say 15.87. Now your vector becomes [15.87,0,0]. It retains its direction, but is now much further from the origin [0,0,0].

In your game, you're going to be working with positions which are not at the origin. These are points in world space and you're going to be working relative to them. To work with vectors, you need to convert these world space coordinates into local space coordinates -- this just means you're translating the origin to [0,0,0] so that you can use vector math.

Example: You have point A at [1,2,3] and point B at [6,7,8]. If you connect A to B, you get the line segment AB. This line segment has direction and magnitude, so it can be used as a vector. To get the line vector, you would subtract point B from point A: [6-1,7-2,8-3] => [5,5,5] [5,5,5] has the same direction and magnitude as line AB, but now its in local space because we shifted Point A such that it is centered on [0,0,0], and point B is on [5,5,5]. Now we can do vector operations on it! When we're done, we shift it back by adding point A back into it."

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question