# [Maths] Gravitate towards target point?

#### LurkerAspect

##### Now officially a Super Lurker
Hi there I wasn't sure where to post this, so it's ended up here.

Complete overhaul: New problem: my object now gets stuck in orbit around it's target, even if the target is stationary. Does anyone know how I can fix that?

#### s3rius

##### Linux is only free if your time is worthless.
I'm not a math whizz, but you need to be a little more specific about what you're doing and how you're doing it.

##### Super Moderator
Staff member
I'm not a math whizz, but you need to be a little more specific about what you're doing and how you're doing it.
s3rius is right, we need more information. Also if you could post your current code (or even the maths in pseudo code) it would be very useful.

#### LurkerAspect

##### Now officially a Super Lurker
Yeah sorry I realised that now >.<

Here's what I'm doing:
Code:
``````target(object)  = target of projectile
projectile(object) = the projectile
velocityX(real) = X velocity of projectile
velocityY(real) = Y velocity of projectile
speed(real) = projectile's current speed = SquareRoot(velocityX^2+velocityY^2)
angle = angle towards target from projectile
acceleration(real) = the amount the projectile accelerates per second
maxSpeed(real) = the Maximum speed/terminal velocity the projectile is allowed to acheive = 900
interval(real) = periodic tick duration = 0.02

every interval {
if speed < maxSpeed {
set velocityX = velocityX+acceleration*(interval^2)*Cos(angle)
set velocityY = velocityY+acceleration*(interval^2)*Sin(angle)
}
}``````
I guess you would call this pseudo code.

Anyway, basically the problem now is that if the projectile misses it's target on the first run, it gets stuck in an infinite orbit trying to hit the target. The target, in this case, is stationary. The projectile is also launched with an initial velocityX and velocityY, which is why this problem occurs.

#### LocalDude

##### New Member
Have you tried to decrease the speed if the projectiles angle is too much of course

#### GFreak45

##### I didnt slap you, i high 5'd your face.
Should the gravitational pull be within a certain range of the object? Also seeing as a gravitational pull should increase in the direction of the object, you cant calculate the velocity in an x/y method, you calculate the velocity and angle, then calculate the x/y, if you calculate the x/y first the object will continue in the wrong direction if it moves slightly (Assuming objects can fight the gravitational pull), also shouldnt the pull be based on the distance from the object rather than an accelaration?

#### UnknowVector

##### I come from the net ... My format, Vector.
I'm assuming the "angle towards target from projectile" is measured counterclockwise from the positive x-axis, with the origin at the position of the projectile (i.e. atan2(target-projectile)). (For future reference: two points never defines an angle. Always make your third reference point explicit, in this case it's any old point along the x-axis and directly to the right of the projectile.) (Had to edit this paragraph because I said it wrong too XD)

Maybe you could try describing what you _want_, instead of what you're doing.

To the best of my understanding: you want a completely undodgable single-minded guided-rocket that is guarenteed to eventually hit whatever you shoot it at. You want this rocket to turn gradually in order to head towards it's target as close to straight on as it can get.

Code:
``````every interval {
if speed < maxSpeed {
set velocityX = velocityX+acceleration*(interval^2)*Cos(angle)
set velocityY = velocityY+acceleration*(interval^2)*Sin(angle)
}
}``````
This code is _very_ close. You are accelerating the projectile along the vector towards the target. You are also capping the projectiles speed. However, you should be multiplying the acceleration by the interval, not the square of the interval.

The fact that your homing missile can circle it's target is a natural consequence of this method.

Here's a good thread on this. Check the first post by MerlinJim.

Perhaps the best solution for you is too combine the absolute vectoring he talks about with your method, which he also mentions. After you calculate a new velocity for the missile and calcuate it's movement based on that velocity for the next timestep, apply a small absolute displacement in the direction of the target. Even if the missile circles the circles will get tighter and tighter over time. Possibly make the size of this displacement scale up depending on how close you are to the target or how long the missile has been in flight.

The result might look sorta strange, you'll have to test that out.

#### GFreak45

##### I didnt slap you, i high 5'd your face.
I have a good idea for how to combine the absolute velocity and movement based velocity, 1 total velocity and based on the distance from the object the absolute or the movement will have more effect, ie: far away movement has a much larger effect (0-100), getting closer it switches to more absolute (50-50) and really close it would be pure absolute (100-0), you could have this based on the original position of the object when the order was issued plus a certain amount to insure the absolute will have more effect if the original position is close

General chit-chat
Help Users
• No one is chatting at the moment.
• C Cherry.Grove:
My boyfriend is super into Bethesda modding so I'm probably going to join him in that for a bit, then move on to making mobile/browser games.
• C Cherry.Grove:
I mostly just want to look at my old projects because I know I posted uploads on here somewhere.
• C Cherry.Grove:
My one game was basically Among Us but you sabotaged an entire medieval city simulated drastically inefficiently with dynamic NPCs :S
• C Cherry.Grove:
of course I never finished it x D
• jonas:
xD
• jonas:
I think you can still run the original game, just there's a chance your map won't work with the newer patches
• jonas:
what development framework have you been looking at for mobile development?
• jonas:
I'm currently creating (as a hobby) a space invaders like game for mobile using Xamarin, after starting a few larger projects that I also may not have finished
• The Helper:
Awesome! you should look at the Atari VCS platform it does not have a huge user base but the one it has is super active at around 11k and they have no games in there store
• The Helper:
basically it is linux
• The Helper:
OMG it is BanLord as Blackveiled on the forum
• The Helper:
what is up buddy
• jonas:
thanks for the tip, that sounds cool. Never heard about the VCS before but seems like it should be a good match
• tom_mai78101:
The winter vacation left a toll on me. I missed staying up late and sleeping in late.
• C Cherry.Grove:
i haven't looked into it yet
• C Cherry.Grove:
If I could use something Python compatible it would save me some learning
• C Cherry.Grove:
But Unity might be easiest
• C Cherry.Grove:
looking at old posts here makes me happy. Makes me be all like "wow I was pretty smart in 2009"
• C Cherry.Grove:
mostly failing at life through adulthood really demotivated me for a while
• C Cherry.Grove:
....what I don't get is why the fuck they would force us to permanently convert to a version of the game everyone fucking hated : \
• C Cherry.Grove:
Raid Shadow Legends texture pack smh
• jonas:
Most people fail at life throughout their adulthood. Sometimes we get to learn from it. Usually it just feels like crap.
+1
• The Helper:
What does not kill you makes you stronger is what they say
• C Cherry.Grove:
for real!