Page 1 of 1

More Interesting Collisions? [SOLVED]

Posted: Tue Jul 07, 2015 1:42 am
by Ethan-Taylor
Hey guys,
I'm using Hardon Collider for my project. At the moment I'm using it for everything including bullets that the player shoots.

Code: Select all

function ammo.update(dt)
	for i,v in ipairs(ammo) do
		if v.id == nil then
			v.id = ammo.id
			ammo.id = ammo.id + 1
		end

		v.bullet.type = v.id
		v.xvel = v.xvel + 12000 * dt * math.cos(v.angle)
		v.yvel = v.yvel + 12000 * dt * math.sin(v.angle)
		if v.reverse == 1 then
			v.x = v.x - (v.xvel + 1) * dt
			v.y = v.y - (v.yvel + 1) * dt
		else
			v.x = v.x + v.xvel * dt
			v.y = v.y + v.yvel * dt
		end

-- Continued
So basically when the bullet hits something v.reverse = 1
But is there a more realistic approach, rather than just reversing the bullet direction?
If so could you give me some code that will make the velocity do that?

Oh, and explain it too!

Cheerus :3

Re: More Interesting Collisions?

Posted: Tue Jul 07, 2015 12:45 pm
by ivan
Hello Ethan,
It depends if you're going for 'realistic' physics,
in which case you can't just reverse the velocity during a collision.
For example, imagine a bullet hitting a wall.
If the wall is perpendicular to the bullet's path, then you can reverse its velocity:

Code: Select all

        |
------> |
        |
Now imagine a bullet hitting an inclined/slope tile.
It would look kind of strange if you reversed the velocity in the latter case:

Code: Select all

         /
------> /
       /
Usually you would want to 'deflect' the bullet based on the collision normal (axis of shortest separation).
One approach is:
collision_normal = separation / len(separation)
relative_velocity = bullet.velocity - wall.velocity
pen_speed = dot (relative_velocity, collision_normal)
pen_component = (normal * pen_speed)
restitution = [0-1] (0 restitution means non-elastic, 1 means perfectly elastic)
dt_velocity = pen_component*restitution

bullet.velocity = bullet.velocity - dt_velocity

Unfortunately, it gets more complicated if you want to have 'friction'.
Take a look at my short tutorial on game physics.

Re: More Interesting Collisions?

Posted: Wed Jul 08, 2015 1:24 am
by Ethan-Taylor
ivan wrote:Hello Ethan,
It depends if you're going for 'realistic' physics,
in which case you can't just reverse the velocity during a collision.
For example, imagine a bullet hitting a wall.
If the wall is perpendicular to the bullet's path, then you can reverse its velocity:
How can the wall have velocity?!

Re: More Interesting Collisions?

Posted: Wed Jul 08, 2015 2:13 am
by davisdude
May just be me, but I think he means the bullet's velocity. Plus a wall does have velocity. It just has a magnitude of 0 and a direction of 0 degrees. ;)

Re: More Interesting Collisions?

Posted: Wed Jul 08, 2015 5:14 am
by ivan
Yea, I didn't word it properly, I meant reversing the bullet's velocity.

And yes, as davis mentioned, stationary objects have velocity of 0, where 0 doesn't affect the equation:
relative_velocity = object1_velocity - object2_velocity

Re: More Interesting Collisions?

Posted: Wed Jul 08, 2015 7:12 am
by Ethan-Taylor
ivan wrote:Yea, I didn't word it properly, I meant reversing the bullet's velocity.

And yes, as davis mentioned, stationary objects have velocity of 0, where 0 doesn't affect the equation:
relative_velocity = object1_velocity - object2_velocity
How do we convert the original code into v.xvel and v.yvel?
Sorry If I'm being a bit of a noob XD

Re: More Interesting Collisions? [SOLVED]

Posted: Wed Jul 08, 2015 10:23 pm
by Ethan-Taylor
Nvm.
I've got it up and running now.
Thanks for the support!