More information about collisions Documentation sample

Hey guys,

In this section we have this snippet of code:

function on_message(self, message_id, message, sender)
    -- Handle collision
    if message_id == hash("contact_point_response") then
        -- Get the info needed to move out of collision. We might
        -- get several contact points back and have to calculate
        -- how to move out of all accumulatively:
        if message.distance > 0 then
            -- First, project the penetration vector on
            -- accumulated correction
            local proj = vmath.project(self.correction, message.normal * message.distance)
            if proj < 1 then
                -- Only care for projections that does not overshoot.
                local comp = (message.distance - message.distance * proj) * message.normal
                -- Apply compensation
                go.set_position(go.get_position() + comp)
                -- Accumulate the corrections done
                self.correction = self.correction + comp
            end
        end
    end
end

But i can’t figure out a corect value to self.correction variable.

Can anyone help me?

self.correction in the code is a vector that holds the accumulated compensation needed to move an object out of a collision. It should be zeroed at each update() so the next round of collisions can be accumulated.

It is needed because if there are multiple collisions each collision holds data for that collision only and you need a way to combine the penetration vectors. Simply adding them won’t be correct. This is one way of doing that.

2 Likes

Thanks for this informations. Like you said, I did the compensation variable with a zero value vector3 in the update function:

function update(self, dt)
	self.correction = vmath.vector3(0, 0, 0);
	managerAnimation(self)
	
end

Now i get this behavior:

with more objects:

Question 1: Is this a normal behavior? how can i improve this?

Question 2:

have you others public samples teaching how to handle this kind of collisions?

In your game example, do each of these little guys solve collisions separately?

If so, the order they solve will have an impact. For example, one character could first solve against the yellow circle and move out of it, then be pushed into the circle when solved against another character.

1 Like

What you a trying to achieve called “group behavior” and combined from multiple behaviors: obstacle avoidance, separation, flocking, etc.
Read Chapter 3 “How to Create Autonomously Moving Game Agents” from “Programming Game AI by Example” book by Mat Buckland.

4 Likes

yes, they do. Each of these little guys solve collisions separately.

But, how can I solve this?

in my last improve, i did the hypotenuse’s calculate to find the little guy nearest to clickpoint and now i have a unique velocity to each member.

see the diference:

oh god :sweat_smile: more 521 pages. hahaha nice i will read it. Really thanks man!

Is there some reason why you don’t want to use dynamic collision objects? It seems like that would solve the problem—just apply a force in the direction of movement and you are done.