AI for a marble sort of game

hi,

i m working on a marble game right now for the i phone …i have completed the single player scripting and was thinking about the AI script can someone help me with that …

the main aim of the game is to push the balls out of the playing area by hitting them with your ball …

Why does that need AI? Are they avoiding the pusher ball? Are they attacking the pusher? Simply trying to stay in bounds? Your question needs more precision.

HTHBTH

i am asking for the ai of the other player …

What about it?? You will have to define with some precision what you are seeking. It is not apparent from what you have written.

BTH

well the game play revolves around a circular ring(basically a circular plane) which has a few marbles placed on it the objective of the game is to hit those marbles out of the ring by banging them with your ball(player ball).

But you still haven’t told us how you want the other marbles to act.
Or does the “other player” also get a single ball he can control, and it’s turn based?
If you want help, you’ll have to be more specific.

Wow, amazing how pissy kids who have never played “Marbles” can get.

Okay, for everyone giving this guy shit, I’ll translate:

He’s asking for AI to calculate the best angle and force to fire their marble at his marbles, to push them out of a ring. It’s like “snooker” but without pockets.

One player lines up a shot and tries to shoot out the other’s marbles from a ring. Each player takes turns.

That’s how “marbles” is played.
http://en.wikipedia.org/wiki/Marble_(toy)

I wish I could help you, but that kind of math isn’t my forte, even though I know what marbles is.

Not sure where that came from. I’m giving him shit? I wasn’t sure how the game worked (never was into marbles), and just told him to be more specific so he would get better help. Unfortunately I don’t know the best way to do this either. You could just try to point the marble at the closest marble, or try to find an angle where you hit multiple marbles - but off course that wouldn’t result in the best possible outcome for the AI.

@twitchfactor : thanks for explaining
@smag : i have tried what you are saying but it doesn’t seem to work …

Sorry for giving you shit Smag, just seemed like people were more interested in giving this guy crap than helping and I somehow immediately knew what he was talking about when he said, “marbles”.

In thinking about it, your biggest issues with writing an AI for a pool / snooker / marble game is getting the AI to make intelligent shots. Targeting something is dead simple (especially in Unity). It’s calculating the off-angle shot, the possible ricochet and of course the need to “miss”.

A quick search netted me these couple of articles. Hope they help:

Article on “Intelligent Mistakes”
http://www.gamasutra.com/view/feature/3947/intelligent_mistakes_how_to_.php?print=1

A code project for Snooker, written in C#. Should be good for a nudge in the right direction (even though the physics between flicking a marble from your thumb and striking it with a pool cue are different… but I doubt you would actually be able to properly simulate that, anyway).
http://www.codeproject.com/KB/game/XNASnookerClub.aspx#AI

This is potentially quite a difficult AI task. You would probably want to start by determining which angles of “attack” on each marble are viable. This involves finding which parts of each marble are not obscured by other marbles (this translates into an angle visible from the position of the striking marble). You would generally want to hit a target marble as full in the face as possible to take energy from the striking marble and prevent it from getting deflected out of the ring. From then on, the sky is the limit, really. You could calculate which marbles the target is likely to hit on its way out and use that to modify the launch angle of the striking marble. You could then continue that process to see which additional collisions can be set up and keep going as many steps ahead as you have time for.

However, it’s likely that a simpler heuristic could play quite a good game. Maybe determine which marble has the most others close to it and aim straight at it, regardless of other marbles in the way. Or find the average of all the centre points of the marbles and aim for the marble closest to that point. There are lots of possibilities for heuristics in a game like this and might have to try out a few ideas before finding something that plays acceptably against a human opponent.

The link below is to a marble type game, although not played like marbles normally is. You might be able to pick something up from that based on observation of how the AI player acts.

http://www.shockwave.com/content/shuffle/sis/shuffle.swf

Thanks twitch. It has been thirty some odd, maybe 40 years since I have been referred to as a “kid”, though never “pissy”. Simply trying to get the fellow to post a concise question. And, I was marble champ of my block. We had about fifteen different ways to play with a few dozen rules. Each type of marble was worth multiples of other types. Large cateyes were worth 5 regular cateyes. Ball bearing were worth ten times at the same size. Maybe by the time your generation came along the game narrowed down… Maybe not… I haven’t seen a game of real marbles for a few decades. Last time I was at Toys R Us they had one bag of one hundred cateye marbles and that was it.

BTH

i am implementing the ai for the marble game the code seem to work absolutely perfect on the mac os but the moment i run the game on iphone the ai doesn’t respond .

var target : Transform;
var ball: Transform;
var speed =3.0;
var ai_ball;
private var position =Vector3(1165,76,1194);

function FixedUpdate()
{
		
		var other : player_controller = gameObject.GetComponent(player_controller);
		if(other.setball)
		{
		if (target == null  GameObject.FindWithTag("Marble"))
		target = GameObject.FindWithTag("Marble").transform;
		Attack();
		other.setball = false;
		}	

}

function Attack()
{
				
			var other : player_controller = gameObject.GetComponent(player_controller);
			if (Physics.Linecast (transform.position, target.position, hit))
			{	
			var hit : RaycastHit;
			hit.transform == target;
			ai_ball = Instantiate(ball,position,Quaternion.identity);
			yield WaitForSeconds (1);
			var targetRotation = Quaternion.LookRotation (target.position-transform.position,Vector3.forward);
			transform.rotation = Quaternion.Slerp(transform.rotation,targetRotation,8);
			ai_ball.rigidbody.AddForce(transform.forward *speed);
			}   
}

the moment the main ball(player ball) goes out of the ring the setball variable turns true and and the ai_ball prefab is instantiated and then it fires with the respective speed towards the given direction, this code is working absolutely fine in mac os but when i run the same code in iphone the ai_ball prefab does get instantiated but it doesn’t fire it just stays at its respective position , i have already tried increasing the speed,applied forcemode in the rigidbody.addfoce function but with no success

can anyone help me out with this please.

You need to strictly type all your vars. i.e

var speed : float = 3.0;
var ai_ball : GameObject;
private var position : Vector3;
function Awake () {
     position =Vector3(1165,76,1194);
}

You might also try setting up your component call to other as a var in the document scope and set it in the Awake function to avoid calling it every loop which gets expensive. As well call your components like this so iOS can follow along

var other : player_controller;
function Awake () {
     var otherObject : GameObject = gameObject.Find("ObjectHoldingOthersScript");
     other = otherObject.GetComponent(player_controller);
}

HTH
BTH