Here is the answer given by Lord Buddha to a similar matter...
There were many religious groups and cults in India at Lord Buddha's time and almost each had their own signature practices, Some were naked,Some were living like animals (Cows,Dogs) and many more.All of them had one purpose, Finding the end.
There was a group who said to their followers
"You must boil the water you drink because if you drink it any other way those very small beings in water would die"
(An amazing early reference to Micro organisms).
When people asked for Lord Buddha's take on this Lord Buddha's reply was....
Yes there are beings in water and yes there is a chance they might die, but if you boil the water you are obviously going to kill them as an effect of boiling. But what can you do? you must drink water.As you have no intention to kill those beings you are not responsible for the deaths that might occur from consuming water.
Here's the answer to your last analogy....
What kinds of circumstances come in this category an what does not?
- What is an accident and what is compassion?
An accident is the correct set of circumstances.And you are correct with your first example.Here you see the ants after the action really happen.
If we add another element....
- What if you knew about ants before running on the grass?
Now you know there are ants and you are in a hurry.You have stepped on the grass and suddenly you remember about the ants.You see some dead because of your running.
Here it does not generate bad karma because even though you knew about the ants you still did not had any idea or a plan to kill the ants by stepping on them.
- What if you saw the ants and still went on anyway?
Here you have lost Compassion and you show no regard to the lives of these ants, now this is bad. This kind of action will generate bad karma.
But there's a catch....
Say you knew about the ants and walked on the grass thinking "Hopefully there won't be any now". And you did not saw any for a moment and suddenly you see that you have stepped on some.Now you panic and try to walk away from the ants but you are surrounded.So you quickly step away trying your best to not to harm any ants.
Here when you try to walk away you might increase the amount of dead ants but you still have no idea of a killing or hurting.Strangely even after many dead ants you are still clean.
So where does the compassion comes to play?
Say you are hungry and you can hunt.
If you go hunting it is bad Karma.
If you let an animal die so you can eat later it is bad Karma.
Say you are poor
If you steal that is bad karma. But if you take someone's pen thinking it is yours (given that both pens look alike) you do not gain bad karma because you had no idea.