Finding An Intersecting Point

Alright. So, i have an object, (in this case, a planet), and an asteroid. the asteroid, is heading straight forward, towards the planet. the starting position of the asteroid is random. i need to find an intersecting point, so an explosion appears on the surface of the planet.

image

Ive tried to do the maths, but im pretty dumb and it doesnt really work :sweat_smile:

How would i get a vector3 from this?
My code :slight_smile: :

	local rndAstroid = astroids:FindFirstChild(sizeString):GetChildren()[math.random(1,#astroids:FindFirstChild(sizeString):GetChildren())]
	local explosion = rndAstroid:Clone()
	explosion:ClearAllChildren()
	explosion.Parent = workspace
	explosion.Material = Enum.Material.SmoothPlastic
	explosion.Color = Color3.fromRGB(255, 136, 38)
	explosion.Name = "Explosion"
	explosion.Transparency = 1


	rndAstroid.ParticleEmitter.Enabled = false
	rndAstroid.Parent = workspace

	rndAstroid.Position = Vector3.new(Planet.Position.X + math.random(-30,30), Planet.Position.Y + Planet.Size.Y * math.random(-5,5), Planet.Position.Z + math.random(-20,20) )

	local planetSurface = Vector3.new(Planet.Position.X + Planet.Size.X / 3.25, Planet.Position.Y + Planet.Size.Y / 3.25, Planet.Position.Z + Planet.Size.Z / 3.25)
	local part = Instance.new("Part", workspace)
	part.Anchored = true
	part.Size = Vector3.new(.5,.5,.5)
	part.Position = planetSurface 

	local GX = rndAstroid.Position.X - Planet.Position.X
	local GY = rndAstroid.Position.Y - Planet.Position.Y
	local GZ = rndAstroid.Position.z - Planet.Position.Z



	local finalgoal = {}
	finalgoal.Position = Planet.Position
	local tweentime = math.random(3,6)
	local tweenInfoB = TweenInfo.new(tweentime)

	local tweenB = tweenService:Create(rndAstroid, tweenInfoB, finalgoal)
	tweenB:Play()
	task.wait(tweentime / 2.5)
	rndAstroid.ParticleEmitter.Enabled = true


	repeat 
		task.wait()
	until rndAstroid.Position == Planet.Position 




	print(tostring(rndAstroid.Position.X),  tostring(Planet.Position.X))

	local finalexplosiongoal = {}
	finalexplosiongoal.Size = Vector3.new(explosion.Size.X * 2,explosion.Size.Y * 2,explosion.Size.Z * 2)
	finalexplosiongoal.Transparency = 1
	local tweenInfoExplosion = TweenInfo.new(3)
	local explosionTween = tweenService:Create(explosion, tweenInfoExplosion, finalexplosiongoal)



	explosion.Transparency = .5
	explosionTween:Play()
	task.wait(3)
	explosion:Destroy()

Thanks for the help

To find the collision point on the surface of the planet from the asteroid (assuming that the asteroid is always heading for the planet’s center) is the asteroid’s position subtract the distance between the two subtract the radius of the planet. I threw together a function for this purpose:

local function GetCollisionPoint(planet, asteroid)
	local distance = (asteroid.Position - planet.Position).Magnitude -- Distance of the asteroid
	local direction = (asteroid.Position - planet.Position).Unit -- Direction of the asteroid relative to the planet's position
	
	local planet_radius = planet.Size.X / 2 -- Assuming that it is a sphere.
	local asteroid_radius = asteroid.Size.X / 2 -- Assuming that it is a sphere.
	
	-- The impact position is the asteroid's position subtract
	-- the distance subtract the planet radius times the direction
	
	local impact_position = asteroid.Position - (Vector3.new(1, 1, 1) * (direction * (distance - planet_radius)))
	
	return impact_position
end

I put this in a script and got this:

Please tell me if I misunderstood the question.

Hope this helps!

1 Like

(Asteroid - YouTube)

Video ^^^

This is interesting as the explosion doesn’t seem to be where the asteroid hit. The planet isnt moving, just the camera. Any ideas why?

ah nevermind, got it working, thanks :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.