Move asteroid realistically in 0 Gravity from hit (impact plus pressure wave)
Heeeelllooo! <3
I found a way, to actually move away an asteroid, when hit by a bullet. It always goes into the opposite direction of the laser(bullet) that hit it. However this is quite unrealistic. If I hit it on its left side, it should go to the right and also pic up some rotation. The bullet uses a capsule collider, the asteroid is polygon collider.
I use this script:
private void OnTriggerEnter2D(Collider2D collisionData)
{
var magnitude = -25; // How much to moove asteroid? Negative = Away from laser/player
var force = transform.position - collisionData.transform.position;
force.Normalize ();
GetComponent<Rigidbody2D> ().AddForce (-force * magnitude);
Of course this script ony creates a linear direction. Let me explain, what I mean:
I want to detect the angle at which the bullet collides with the polygon collider. If I got a complex asteroid shape, this is required to react in a realistic manner. Say it is an "S" shape and I hit it at its tail on the inside. It basically hits on the edge, but the force still should not go to the center, rather outwards, since it still hit inside. Also it should - depending on the angle between 1) the laser and 2) the line between center and the hitpoint - create some rotation. In space this would happen.
So how do I extract the angle, of wich it collided with and use it do determine the rotational force applied?
In less than 20 words: - Laser hit creates presure wave - Laser hits with kinetic force How do move asteroid realistically?
Thanks for your help.
@JPhilipp Unfortunately it doesn't. I am searching for a realistic way to archive this. Explosion force also creates rotation on any object, that is not a perfect circle (globe). That means, a linear force is unrealistic. It also needs to spin at a given speed. This depends on the angle of the surface. But to actually get the angle of the surface, I need to use a Raycast (multiple, to be precise). Doing some semi-basic math, this results in some angles and lines of given length.
This is my plan:
-Bullet hits asteroid. -impact.position // how do I get it on 2 trigger colliders? Regular collider can NOT be used for this case -from midpoint to impact position get a vector (basically direction) and add 1 distance = raycast.start.position -from raycast.start.pos (ammount not decided yet) raycast in 15° steps, to get the surface details -get the distance from each ray to the hit object - total.force gets divided by raycasts_ammount (360/15=24) and than divided by distance (get a vector out of this, where the length represents the strength) - combine the vectors to get the precise direction of force and the precise strength - get the angle between the line representing given vector (starting from impact.position) and the angle between impact.position and asteroid.midpoint. - 180° angle means all force converted to linear force - 90° angle means all force converted to rotation - everything in between is possible
Yeah, this is not perfect, but actually relatively close to reality in many ways.