implementing obstacle collision mechanism in a spaceship game

Started by
5 comments, last by AmitOfer 8 years, 10 months ago

Hi, I'm trying to create a simple game prototype from the Flying template provided with unreal engine 4 (3rd person spaceship game).

Basically I just took the Blueprints version of the template and change the control schemes of the spaceship to suite my needs.

Now I'm trying to figure out how to handle collisions with obstacles, in the template these are handled by changing the speed to 0 which of course is not good enough.

I'm thinking that destroying the ship would make the game too hard so I figured I could reduce the health and bounce the ship into some direction.

The problem is that I'm not sure what the right way to approach this from physics perspective, in what direction and what force would you bounce the ship? maybe divert it on the other direction in yaw axis? or maybe in a direction which is perpendicular to the surface it hits?

I can't think of a good example for a game that implements this mechanic but I guess there are many, I just can't name one right now, maybe rogue squadron?

Anyway what do you think would work best here? how would you go and implement this mechanic?

I'm working in UE4 blueprints though I might convert it to C++ so the implementation doesn't have to be UE4 specific, I'm just trying to figure out how to approach it.

Thanks,

Amit

Advertisement

unity probably has stuff to do this built-in, but here's a quick and dirty way to do it yourself:

at the time of collision, get a direction vector from the ship to the obstacle:

direction_vector x = obstacle x - ship x

direction_vector y = obstacle y - ship y

direction_vector z = obstacle z - ship z

flip it around to point the other way - from the ship, in a direction away from the obstacle:

direction_vector x = - direction_vector x

direction_vector y = - direction_vector y

direction_vector z = - direction_vector z

now normalize it so it's unit length - divide each component by the length of the vector to get a vector of length 1:

the magnitude = sqrt(x*x + y*y + z*z)

so:

unit_vector x = direction_vector x / magnitude

unit_vector y = direction_vector y / magnitude

unit_vector z = direction_vector z / magnitude

now, multiply the unit vector by the collision speed, so faster collisions bounce more...

unit_vector x = unit_vector x * collision_speed

unit_vector y = unit_vector y * collision_speed

unit_vector z = unit_vector z * collision_speed

now multiply the unit vector by some constant. this lets you adjust the amount of bounce by just changing the constant...

bounce_multiplier = 0.1f

unit_vector x = unit_vector x * bounce_multiplier

unit_vector y = unit_vector y * bounce_multiplier

unit_vector z = unit_vector z * bounce_multiplier
finally, add the unit_vector to the ship's position to move it to its new location after the bounce...
ship x += unit_vector x
ship y += unit_vector y
ship z += unit_vector z
note that you can do a similar thing with velocity vectors to make it bounce off.
in the real world, one or both objects would sustain damage, and one or both objects would "bounce". lookup "impulse and momentum", and "conservation of momentum".
how much collision energy was converted to damage, and how much remained as bounce would depend on the object. a spaceship hitting a planet at hundreds of miles an hour might bounce a few tens of feet, and take all the rest as damage, while the planet would not really bounce at all due the the difference in mass, and would absorb pretty much all the energy in the form of damage as an impact crater.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

Thanks, I will try it.

BTW I'm using UE4 and not unity smile.png

There are a couple of gotchas you might come across.

Firstly start by finding the collision point accurately.

You detect the collision and do a very simple version of the above solution. Create a vector from the centre of the collision object and the player ship. Reflect it. Normalise. Multiply by player speed. Move the player.

Sounds fine doesn't it?

Well what might happen is that the vector actually points into the collision object pushing the player ship into the object instead of away from it. This is because the player ship has penetrated into the collision object.

Easy to get caught out by that one.

Secondly it may not be enough to just take the centres of the objects.

When you do that you are effectively doing a sphere against sphere collision. This may be enough for you, but consider the case where the player ship collides with a cube.

It's just going to feel wrong.

What you may have to do is do a ray triangle intersection test and extract the normal of the triangle you collide with to get a direction to throw the player ship.

This works a lot better.

It is common to define a collision mesh for an object, which is a very low poly version of the physical shape of the object, then use this for all collision processing.


Well what might happen is that the vector actually points into the collision object pushing the player ship into the object instead of away from it. This is because the player ship has penetrated into the collision object.

good point, the algo i described is for stepwise collision detection, or for other methods once you "move the object back to the collision point". not for non-stepwise collision detection before fixup is applied.

that's why i said "at the time of collision". IE for non-stepped, once you've detected a collision and have moved the objects back to the collision point, and are now ready to model the effects of the collision. obviously, modeling the effects of the collision once the object is already part way through or passed entirely through the obstacle is not going to work.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php


Secondly it may not be enough to just take the centres of the objects.

When you do that you are effectively doing a sphere against sphere collision. This may be enough for you, but consider the case where the player ship collides with a cube.

It's just going to feel wrong.

also true. the method of collision detection and its accuracy are up to you. note that i did not specify any specific collision check method. any method can be used, with varying degrees of implementation difficulty, runtime speed, and accuracy.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

Thank you all for your responses.

I think I don't need to worry about the problems mentioned as unreal will take care of most of them for me.

The player's ship is using a low poly mesh for collision, basically in unreal its called 18DOP simplified collision, I'm attaching the screenshot so you can see what I'm talking about.

I have an event called "Event Hit" which gives me 3 vectors: hit location, hit normal and hit impulse. so I guess I can use these instead of the vector calculations you mentioned.

Also I assume this event is generated at the right time that the hit occurs as its also set by the engine to block, so that when the objects collide the ship will not get inside the cube.

So I guess that what I need here is to change the position and velocity vectors so that the ship will bounce and I guess also rotate the ship to its new direction.

the problem is that I think I can't just do these instead instantaneously right? I will need to do it according to delta time right?

I haven't tried anything yet, I tried to use physical material but I still haven't got the right results so I think I want to do it mathematically like you suggested.

This topic is closed to new replies.

Advertisement