## More Interesting Collisions? [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

### More Interesting Collisions? [SOLVED]

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
Last edited by Ethan-Taylor on Wed Jul 08, 2015 10:22 pm, edited 1 time in total.

ivan
Party member
Posts: 1619
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: More Interesting Collisions?

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.

Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

### Re: More Interesting Collisions?

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?!

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

### Re: More Interesting Collisions?

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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim

ivan
Party member
Posts: 1619
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: More Interesting Collisions?

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

Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

### Re: More Interesting Collisions?

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

Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

### Re: More Interesting Collisions? [SOLVED]

Nvm.
I've got it up and running now.
Thanks for the support!

### Who is online

Users browsing this forum: No registered users and 22 guests