Change a line composed of vextor3's to a constant.force on an object

Hello and thanks for your attention.

I'd like to use a line composed of vector3 vertex positions to apply force to objects, via constant.force, that intersect it.

The fiction is: think of a leaf getting caught up in a current of water in an otherwise still pond.

The scripting documentation on constant force is a little light. Can I take line segments defined by vextor3's and apply them as (appropriate to fiction) constant forces on objects?



more ▼

asked Nov 18, 2009 at 03:20 AM

avatar image

516 77 61 93

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

First have a look at the documentation for Rigidbody.AddForce.

Note that the first parameter is a vector along which the force is to be applied. This force will be applied at the object's center of gravity - it won't start spinning, just moving in the indicated direction.

To apply a force that is offset from the center of gravity, see Rigidbody.AddForceAtPosition. This will move and spin your object. The "position" parameter in this case might be the point on the "force line" that is closest to your object, but you can probably get away with using other positions, for example, if the points defining your "force path" are close together, you might be able to use the nearest one.

To get the direction to apply the force, subtract the first point (A) (of the force line segment) from the next point along the path (B). This gives you a vector that represents the direction from point A to point B.

more ▼

answered Nov 19, 2009 at 02:36 AM

avatar image

5.7k 14 69 71

Nailed it! Subtracting a from b seems to do the trick! Thanks!

Nov 21, 2009 at 11:49 PM Goody!
(comments are locked)
10|3000 characters needed characters left

Well, you probably want to check how close the leaf is to the "water-line", and only apply the force if the leaf is sufficiently close. You can write a function that calculates the distance between a line segment and a point.

However, rather than doing this 100% through scripting, you might want to consider this approach instead:

Make a box trigger object. Scale it to be thin and long, almost like a line, except that you can control the thickness. You could make it long along the Z axis and thin in the X and Y axis.

Now make a script on this line with a OnTriggerStay function that applies a force to the GameObject (leaf) that hit the trigger - you can just apply the force along the z axis of the object.

This way you can setup the position and alignment of the "water-line" in the Scene view, and also control the exact width. Furthermore, it requires much less math than if you wanted to script it all, and is also more efficient if you have many "water-lines" at once (unless you know how to optimize the code properly, which is even more work).

more ▼

answered Nov 18, 2009 at 02:11 PM

avatar image

runevision ♦♦
9.3k 55 118 314

I think for the leaf interacting with the line I'm going to try fiddling with larger or smaller than you'd expect collision. Thanks for the suggestion but the line can, and most likely will, curve multiple times as the user will be defining it with a touch drag on an Iphone screen. I'll re-ask my original question another way: Is there a way to apply a constant force on an object that's "push direction" is defined by two pre-existing vextor3's?

Nov 19, 2009 at 01:37 AM Goody!

Thinking about Rune's approach, there's no reason the box triggers couldn't be defined dynamically by your script. (Although to do so you still need same math.) With this approach you could create a static test case in the editor; once working, then create the boxes dynamically. Your test case should try a lot of boxes, just to make sure it'll perform... I think in the end both approaches are a similar amount of work, but for a game with static paths that can be created ahead of time in the editor, Rune's approach might be easier.

Nov 20, 2009 at 04:01 PM Bampf

I've considered that approach as well but I worry that I'll be drawing 50+ little colliders and kill frame rate. If I can't get it going with raycasting I'll try that next. Rune, any idea how to detect when it's near the line andd draw it in to the center of the line? Two or three lines running parallel? :)

Nov 21, 2009 at 11:53 PM Goody!

If you want to go the non-box approach, you can follow the link I just added in the answer. It has some line segment to point distance solutions. As for drawing the leaf closer to the line, that's a closely related problem of finding the closest point on a line segment, and then adding a force that pushes it towards that point.

Nov 23, 2009 at 09:47 AM runevision ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

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



Answers and Comments



asked: Nov 18, 2009 at 03:20 AM

Seen: 2174 times

Last Updated: Nov 18, 2009 at 02:14 PM