playMaker

Author Topic: [SOLVED] AddForce2D to Target World Position  (Read 2511 times)

picklesandtickles

  • Playmaker Newbie
  • *
  • Posts: 13
[SOLVED] AddForce2D to Target World Position
« on: January 22, 2018, 07:40:27 PM »
Hello,

I'm having trouble with AddForce2D. I apply force towards a vector3, which I have set to a position in the World, but the object receiving the force moves in the same direction every time.

The Target position is wherever the mouse clicks, and that part works fine (Attached Position Target). I use a global variable for the position in the world and call it when applying force to the hand object (Fire Right Hand attachment).

Unfortunately, wherever I click on the screen, the force is applied in the same direction each time. See a quick video here:
Perhaps I'm just using AddForce2D incorrectly. Any help is greatly appreciated.
« Last Edit: February 05, 2018, 11:43:56 AM by picklesandtickles »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: AddForce2D to Target World Position
« Reply #1 on: January 23, 2018, 01:51:13 AM »
Hi,

 Adding force once and not everyframe is likely not going to have any impact on the rigidbody.

 I would first try on an empty floating sprite (no gravity) and apply force to it and see how it reacts. then build up from it or go back to your project.

 Bye,

 Jean

picklesandtickles

  • Playmaker Newbie
  • *
  • Posts: 13
Re: AddForce2D to Target World Position
« Reply #2 on: January 23, 2018, 11:33:03 AM »
Hello Jean,

Thanks for the quick response and good advice. I will start with the basics and build from there. I'll come back to post my results when I get it working with the gravity and connected joints.

Thanks again.

picklesandtickles

  • Playmaker Newbie
  • *
  • Posts: 13
Re: AddForce2D to Target World Position
« Reply #3 on: January 23, 2018, 06:32:49 PM »
Hello,

Using a no gravity test scene, I was able to experiment with Add Force 2D and move an object towards the mouse. Thanks for your help with that!!!

My last question related to this is how do you put a lot more force behind the object, but in the same direction?

Basically, I can get force to be added at a vector2 or vector3, but it doesn't do much, even with reduced gravity and object mass. How would you recommend increasing force in the same direction?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: AddForce2D to Target World Position
« Reply #4 on: January 25, 2018, 02:19:55 AM »
Hi,

 if the direction is ever changing ( like if the mouse moves around but you still want the object to ultimatly reach the mouse), then you need to computer the force by decompositing it.

1: you get the direction ( mouse position - current position)
2: normalize that direction, so that it's lenght is 1
3: multiply by a force factor
4: use it as the force.

with the above you will likely over shoot, so the force factor should be in relation with the distance, the more the distance the more the force,m but as you get closer to the final position, you decrease the force.

Bye,

 Jean

picklesandtickles

  • Playmaker Newbie
  • *
  • Posts: 13
Re: AddForce2D to Target World Position
« Reply #5 on: January 26, 2018, 07:26:58 AM »
Hello Jean,

Thanks for your reply. I tried what you recommended, but I may be missing a step. The force is applied in the same direction each time, despite mouse position. Here's my setup in three simple steps:

https://imgur.com/a/FGb5d

If you could take a look, I'd greatly appreciate it.


picklesandtickles

  • Playmaker Newbie
  • *
  • Posts: 13
Re: AddForce2D to Target World Position
« Reply #6 on: January 26, 2018, 09:01:23 AM »
Wait wait wait.... I just looked at my screenshots again. I actually think my gbl_target_world_position and Object position are the same thing.

In #2, I'm getting mouse position, but saving it as Object position, then subtracting the target world position from the object position, which doesn't make sense then.

I'm at work now, but I'll tweak it when I get home later to actually pull the Object position, rather than the mouse position, and I'll see what happens.

picklesandtickles

  • Playmaker Newbie
  • *
  • Posts: 13
Re: AddForce2D to Target World Position
« Reply #7 on: February 03, 2018, 08:42:21 PM »
Hello again,

I'm writing to let you know of the solution I found to this. I used your advice, Jean, but the only way I could get it to work right was to essentially calculate the slope of the line between the player and the target. I used the target position for x1 and y1, then said if x2 was x1 multiplied by a force factor, what would y2 be. It was easy to figure out the slope and position of x2 and y2 from there.

Aside from working through the slope equation using variables, I also had to apply Impulse rather than Force. It just gave better results considering what I was looking to achieve.

Oh, and finally, I had to increase iterations in the Physics 2D Settings, to keep the hinge joints from separating so much when the force was applied. All of that combined gave a nice result. See attached for a quick look at the FSM.

Here's a quick video of the force being applied. I need to tweak the force factor just a bit, but It's nearly there:

Thanks so much for your help with this. I really appreciate the Playmaker community!!!