2D game like worm reloaded,how to calculate grenade?

Started by
7 comments, last by Mats1 10 years ago

trying to make such game like worm reloaded.

first solved common rocket luncher shoot,this one is easy,just solves equation,pick 0 - 90 degree and cut off collide terration situation.

that's it.

but grenade,i have no idea,it seems it can bounce a lot with different velocity.

no idea how to calculate grenade.

for example such a map.

0 means empty

1 means terrain

5 means player and enemy

how can i use grenade to attack?

1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,5,5,0,0,0,0,0,0,0,0,1,
1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,1,1,1,1,1,1,1,1,1,1,1,1,1,

Advertisement

Hedgewars has an amazing AI, including in terms of accuracy with some weapons (including the grenade). Perhaps consider grabbing the source code for it ( available here: http://www.hedgewars.org/download.html ) and looking at what they are doing. It might not only help with your grenade, but any other issues you have with a worms style game.

thank you and i have download the source,but can not understand what's going on...

so i post this thread,trying to write something myself.sometimes i found it's easy to write my own code than read others.

anyway i just need some general idea on how to do this.

if anybody have read that source code and can explain to me a little bit,that will be appreciated

anything else direct on how to do that is also appreciated

You have the position, velocity and acceleration (wind and gravity). When an object bounces, you reflect the velocity off of the surface. You'll have to google how to reflect vectors, but in a simple example, if something bounce on a flat floor, you would simply flip the Y component of the velocity. You will probably also want some "bounce multiplier" to make it stop after a while. You _may_ also want to add some minor variation to the velocity after the bounce.


gren.velocity += acceleration;
gren.position += gren.velocity;

if(HitFloor(gren))
{
   gren.velocity.Y *= -1;  // NOTE: this is actually a reflect, just a special case when hitting a flat floor
   gren.velocity.Y *= 0.8; // bounce multiplier, possibly apply to full velocity
   gren.velocity.X += random(-0.01, 0.01); // simulate some roughness in the terrain
}

Not that this is not a physically accurate model at all, written from the top of my head for this post, but it will hopefully provide "good enough" results that are relatively easy to tweak for interesting effects (ie. a super bouncer grenade). If you want more accurate or more complex physics, I highly recommend looking at physics libraries such as Box2D.

Hang on, are we talking about how to reflect the grenades here, or are we talking about how to get the computer to do accurate shots with grenades, which already have physics coded for them?

er,i guess i mean how to get the computer to do accurate shots with grenades to attack player.

my english is bad,maybe the title is wrong.what should i use,how to calculate computer enemy to attack player with grenade?or something?

why i post this question here in math and physics,it's because i post this before in AI sub forum,and seems it is not AI question or something,so i post it here this time.

i have written reflect part,it is dirVector2d add ( surfaceNormVector2d.multiply(2) ) or outVector2D = inVector2D + 2 * normVector2D

other part like DvDmanDT written.

my friend suggest to let the computer calculate all the situation,0 - 360 degrees shooting with various power level,and see which one get the nearest distance to player or enemy.

i think it is not a good idea


1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1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0,x,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,x,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,x,0,0,0,0,0,0,0,0,0,x,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,x,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,x,0,0,0,0,0,0,0,x,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,x,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,x,0,0,0,0,0,x,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,x,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,x,0,0,0,x,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,5,5,0,x,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,x,0,x,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,5,5,x,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,x,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,

The line you are looking for is something like that, with probably a parabola going roughly through point 8, instead of my straight line x.

I think this particular example is extremely extremely difficult and the suggestion of trying many powers and angles is probably the only way, as you're basically looking for some lucky bounces, if you have added in some randomness to bounces, which you hinted at in your last post (grenade is basically a bad weapon to choose in this case). For easier scenarios though, perhaps you can try a method like this (I'm assuming you have gravity?):

1)-First, see if the distance player is close enough for you to drop a grenade on them (and then walk away)

2)-If not, see if the player is close enough for a straight line shot to hit them (i.e. They are close enough for gravity to not have to be taken into account) and that there are no obstacles in the way. You can find the distance between the player and AI taking the shot and adjust power/grenade timer accordingly.
3)-If not, see if there is a line to the player following a parabola (a clear artillery shot).

You could test 3 by firing a battery of invisible shots, that behave like a grenade, at a range of angles and powers, towards the player. Save the angle each 'grenade' is thrown at and if one registers a collision or distance < grenade_damage_radius, save the angle and power. Obviously, save the shot information from the best shot - One that collides or go through the distances away to find the best one.

If you want to do it without test shots, you might consider using some trajectory equations to find a decent shot angle ( http://en.wikipedia.org/wiki/Trajectory_of_a_projectile ).

4) - Then it gets really hard... Probably consider using a different weapon, or otherwise, you can fire a random shot or a battery of test shots (as your friend already suggested).

thank you,Mats,this really helps a lot,i haven't consider too much about weapon.

actually right now i only consider rocky luncher and grenade,

in this case,if grenade is not best weapon to use,what else weapon should AI use?

i found in worm reloaded,computer AI always give a crazy grenade bounce angle and hit me,

i found in worm reloaded,computer AI always give a crazy grenade bounce angle and hit me,

It's probably best to focus on making the weapons function well in (kind of) simple ways first. Once your game is running and the computer can use weapons with some competence, then start looking at refining your AI to make it stronger with each weapon.

in this case,if grenade is not best weapon to use,what else weapon should AI use?

In this case, let's say the computer has done test 1,2 and 3 and not found a hit, you might consider:

Checking using your pathfinding if your can move closer to the player. If you can move closer, test some shots (with tests 1-3) from along points along there. This is why I suggested using equations in preference to trial shots, You will be able to check a lot of shots with each weapon quite quickly doing this. You may also want to consider checking both directions (not just moving closer to the player), as some shots will be blocked by obstacles from where you are, but you might make a parabola over them if you are a bit further away.

As for what other weapons to use, I'm not sure what weapons you intend to add to the game? And whatever weapons you add, you will probably want the AI on a turn to run checks through lots of weapons and stuff to find the one that does the most damage to enemies, with the least damage to comrades.

This topic is closed to new replies.

Advertisement