This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-04-08 14:41:30

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Might need help but might just need to type this up...

Ok, I'm working on a new basic/advanced AI.  It's basic in that it isn't a path finder or a sprite that can run through a level.  It's advanced because it's a better "follower" sprite.

What it will do is find the player's direction, turn velocity, and magnitude (distance it's moving every time the script runs).  Using this information, it finds the circular path the player is currently on and the center of that circle.  Once this is calculated, it finds the position the player will be at if he/she continues this path for x amount of turns (I'm going to make x=5 but maybe I'll make it so you can determine x) from the current position.  It then determines how much it needs to turn and move to get to that spot and begins adjusting it's direction and speed (it has the same abilities or power as the player).

FINDING PLAYER ATTRIBUTES:

--Magnitude:

Store the players x and y
wait until one or both of them changes
use the Pythagorean Theorem to find the magnitude (a^2+b^2=c^2)

--Direction:

Store the players x and y
wait until one or both of them changes
use tan(x/y) to find direction (create an offset as well in case the direction isn't between -90 and 90)

--Turn Velocity

Store last player direction (see above)
wait until player x and/or y change
take new direction - old direction = turning velocity

CALCULATE CIRCLE PLAYER IS ON:

First, check to make sure not turn velocity = 0

IF SO: Player is on a line
IF NOT: Player is on a circle

--Line:

split player magnitude into x velocity and y velocity
x velocity = sin(player direction)*magnitude
y velocity = cos(player direction)*magnitude
find the position 5 moves ahead
target x = player x + 5*(x velocity)
target y = player y + 5*(y velocity)

--Circle:

use magnitude and turn velocity to find circumference of circle
circumference=(magnitude)*360/(turn velocity)
use circumference to find radius
radius = circumference/(2*pi)
find center using player position, player direction, turn velocity relative to 0, and radius

turn velocity > 0:
direction to center = player direction + 90
magnitude to direction = radius
center x = player x + radius*sin(direction to center)
center y = player y + radius*cos(direction to center)

turn velocity < 0:
direction to center = player direction - 90
magnitude to direction = radius
center x = player x + radius*sin(direction to center)
center y = player y + radius*cos(direction to center)

use the center x and y, radius, and turn velocity to find target x and y
direction to point = direction to center + 180 + 5*(turn velocity)
target x = center x + radius*sin(direction to point)
target y = center y + radius*cos(direction to point)

MOVE ENEMY TOWARDS TARGET POSITION:

needed direction = tan((enemy x-target x)/(enemy y-target y) )
speed up to maximum magnitude


That's really all I have planned at this point...let me know if you think there's something wrong with my math.

Last edited by AtomicBawm3 (2012-04-08 14:41:46)


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

Board footer