Forum

> > CS2D > Scripts > [Lua Problem] Asteroid issues
Forums overviewCS2D overview Scripts overviewLog in to reply

English [Lua Problem] Asteroid issues

8 replies
To the start Previous 1 Next To the start

old [Lua Problem] Asteroid issues

EngiN33R
Moderator Off Offline

Quote
Code of the script: http://pastebin.com/w7AunZiJ

So basically I create a new asteroid field using the createAsteroidField function with arguments (1,1,19,4,25,10).

Problems:
• It creates the field NOT where I wrote (top-left corner is 4;1 instead of 19;4, bottom-right corner is 10;5 instead of 25;10)
• It doesn't create only 12 asteroids like it should, it creates 205 of them
• When I shoot anywhere once with a USP, all 205 asteroids + some additional 7 are 'hit' and their health decreases to -24 EXCEPT for the asteroids #19-25, health of which decreases to 76 as it should, plus they aren't removed like they should be
• Only one type of asteroids is used (1st) instead of a random one from 1 to 5

Screenies of the thing:
How the asteroid field itself looks like:

The console output after shooting anywhere (not full due to 212 printed messages):


Additional functions not seen in the script code but used (external files): http://pastebin.com/AeP1fSfi

It may be just a few typos made by me, but I really don't know.

Thanks in advance!

old Re: [Lua Problem] Asteroid issues

Lee
Moderator Off Offline

Quote
Line 40:

Should be createAsteroid(id, xx,yy,type,mineral)


2.
The collision algorithm is not complete, so every shot effectively harms multiple asteroids. Since you don't break the loop after a single asteroid is shot, each asteroid will be harmed multiple times based on how many of the "ray components" collides with each asteroid.

There's a much more efficient way to detect whether a ray will collide with an object, the only problem being that there's no way to break early from the algorithm. However, you can queue up the asteroids and obstacles (walls, blocks, etc) and sort them based on distance, then returning after a single hit.

The collision detection assumes that the objects are circles (which may not be true, but is a close approximation)

IMG:https://i560.photobucket.com/albums/ss44/leegao/collision1.jpg


So essentially, get the rotation of your player and see if it's between the arctan minus term and the arctan plus term.

old Re: [Lua Problem] Asteroid issues

EngiN33R
Moderator Off Offline

Quote
Do you mean I should use the last formula? Check if the player's rotation is between the formula's result with - and + (between the first tan and the second tan there's a plus-minus sign)? If so, which arguments do I use instead of delta x and delta y? Cause to be honest I only understood 1% of what you've shown.

And also, what about the 212 instead of 12 asteroid thing? It's still present.
edited 1×, last 27.05.11 08:33:55 pm

old Re: [Lua Problem] Asteroid issues

archmage
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- Angle is the angle at which the projectile was fired (should be in radians!)
-- x,y are the projectiles coords
-- x2,y2 are the asteroid coords

function collisionTest(angle,x,y,x2,y2)
	local atan1 = math.atan2(y2-y,x2-x)
	local atan2 = math.atan2(32, 2*math.sqrt((x2-x)^2+(y2-y)^2))

	local pi2 = math.pi*2
	local test1 = (atan1-atan2)%pi2
	local test2 = (atan1+atan2)%pi2

	return angle > test1 and angle < test2
end
edited 2×, last 27.05.11 09:54:47 pm

old Re: [Lua Problem] Asteroid issues

Lee
Moderator Off Offline

Quote
Well, you're using

1
local area=(x1+x2)*(y1+y2)

which will spawn around 260 asteroids. I didn't know if this was intentional or not

Also note that in the collision detection, x2 and y2 are at the CENTER of the meteor, so you'll have to add half of its width to the x and y coordinate as well if you only keep track of the image positions.
edited 1×, last 27.05.11 10:42:51 pm

old Re: [Lua Problem] Asteroid issues

EngiN33R
Moderator Off Offline

Quote
But Dark, how can I know the projectile's position if I'm gonna shoot with weapons such as pistols or rifles? I can make so you can only mine asteroids with grenade launcher, but is there any way to make it without projectile's position?

P.S. Derp what does the function return and should the projectile hit the exact center of the asteroid? Because if so then it's virtually impossible to do that for a player.

old Re: [Lua Problem] Asteroid issues

Lee
Moderator Off Offline

Quote
The x,y is the player's coordinate. The x2,y2 is the center of the meteoroid. This function returns true if the shot will hit a circle of radius 16 (a circle circumscribing a single tile). It's not exclusive to projectiles.

You can iterate over every meteor to test whether the shot succeeds.

old Re: [Lua Problem] Asteroid issues

EngiN33R
Moderator Off Offline

Quote
As a matter of fact I did what I wanted to in an easier and probably much less effective way, but I succeeded. Still, thanks a lot guys.
edited 1×, last 29.05.11 03:12:20 pm

old Re: [Lua Problem] Asteroid issues

Lee
Moderator Off Offline

Quote
Meh, checking for about 150 conditions during a single shot has around the same amortized time if we assume that there will be very few asteroids. Anyways, this is the implementation in case you still want it. This can be easily modified to take walls and other obstacles into account. You may want to look into a quad-tree or some similar particle to do wall/obstacle collision as these make it rather trivial.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function collide(id, meteor)
	if not m then return end
	local x,y, x2,y2 = player(id,"x"), player(id, "y"), astpos[meteor][1] + 16, astpos[meteor][2] + 16
	local static= math.atan2((y2 - y)/(x2 - x))
	local orient = math.atan2(16/math.sqrt((x2-x)^2 + (y2 - y)^2))
	local min, max = (360 + static-orient)%360, (360 + static+orient)%360
	if min>max then min, max = max, min end
	local rot = (player(id, rot) + 360)%360
	return rot >= min and rot <= max
end
for meteor in ipairs(astpos) do
	if collide(id, meteor) then
		-- collision detection.
	end
end

This wouldn't really effect the efficiency unless you have many asteroids.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview