Forum

> > CS2D > Scripts > Imitating CS2D bullet [Solved]
Forums overviewCS2D overview Scripts overviewLog in to reply

English Imitating CS2D bullet [Solved]

7 replies
To the start Previous 1 Next To the start

old Imitating CS2D bullet [Solved]

Mora
User On Online

Quote
Hey, DC or somebody who know how to:

We all know Engineer's custom weapons and so on. But all these methods are laggy, cuz you need to check every 1 pixel before a bullet meet wall.
So i'd like to know, how you DC made that thing?

I wanna do custom bullet, but the one that made by DC is pretty good(in game) when it checking for walls, the one i will make with lua will require to check alot of things but wont work good anyway.
edited 1×, last 16.04.22 10:54:36 am

old Re: Imitating CS2D bullet [Solved]

Masea
Super User Off Offline

Quote
I really wouldn't say whether it checks for every individual pixel because it would be too costly. Pretty sure it checks for every 32 pixels where then again checks if there is a player in a circle whose radius is again 32 pixels. It could be less than 32 since it is a bullet, we are talking about something very narrow. However the lesser you go, it gets costly.

Things like these, if instant, are called ray casting in game development. You could find really cool stuff about them on the web. Some of the games do use realistic bullet simulation where bullets don't collide instantly but they are treated as real-time physical objects so they have factors like velocity modifiers and such. You could glimpse it in games like Battlefield where you can clearly see your sniper bullet moving. Or some others use both methods simultaneously when needed.

When it comes to replicating it using Lua for CS2D, it is not really possible. Even if you did the logic just the way you want, the visuals would be nowhere near good as the original.

old Re: Imitating CS2D bullet [Solved]

MikuAuahDark
User Off Offline

Quote
A slight optimization you could do is to cache the whole map data to Lua tables. Store the data which tile is wall, building, or breakable object and update it when necessary. The expensive operation lies on calling CS2D Lua API.

old Re: Imitating CS2D bullet [Solved]

Mora
User On Online

Quote
Found something in cs2d max:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Function e_add_sprite_line(x1#,y1#,x2#,y2#,r,g,b,alpha#)

	x1=((x1)*2)-640
	y1=-((y1)*2)+480
	x2=((x2)*2)-640
	y2=-((y2)*2)+480

	mesh=CreateMesh()
	EntityFX(mesh,16)
	surf=CreateSurface(mesh)
	verts = 0
	AddVertex surf,x1#,y1#,640,0,0
	AddVertex surf,(x1#+x2#)/2,(y1#+y2#)/2,640,0,0 
	AddVertex surf,x2#,y2#,640,1,0
	AddTriangle surf,verts,verts+2,verts+1
	
	;EntityColor mesh,r,g,b
	;EntityAlpha mesh,alpha#
	
	Return mesh
End Function
Seems like it what Needed right? looks much better.
but only gfx_muzzleflash, where is it?
Found something more about it, maybe DC can translate the things?
edited 2×, last 13.04.22 11:51:41 am

old Re: Imitating CS2D bullet [Solved]

DC
Admin Off Offline

Quote
@user Mora: That code snippet is just for rendering and doesn't help to solve your problem.

I'm sorry to tell you that I'm just doing what you do. It's a raycast and it does check all the pixels. In CS2D it's simply much faster because it's done in compiled code and not in Lua with heavy overhead. It's not even very optimized or good code - so sharing it won't help to optimize it.

What user MikuAuahDark says could actually be a good idea. Every call to a CS2D Lua API method comes with an overhead so caching things directly inside Lua may help. Definitely worth a try.

Also there are many other ways you can optimize things. Some ideas:

• cache the movement vector instead of calling sin/cos every iteration (or even better: use a line algorithm like Bresenham)
• only do tile related checks if the tile the ray touches actually changed (by remembering the last tile it touched and comparing it with the current)
• mark all tiles which are overlapped by a player beforehand so you don't have to iterate all players for every ray pixel / or make a list of players which can potentially overlap the ray beforehand
• consider to skip pixels or to do some checks only every X pixels if you can afford to have less precision. e.g. only check for player collision every 2 pixels.

old Re: Imitating CS2D bullet [Solved]

Mora
User On Online

Quote
Engin33rs code has what i needed, and i took particles idea from ur old code(yeah is not optimized maybe and full shit, but idk how you did such nice particles, at least looks better than just a line):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
local parts = math.floor(D/16)
	local PD = {}
	
	
	for i=1, parts do
		px = px + math.cos(A) * ((i+parts))
		px2 = px + math.cos(A) * ((i+parts+16))
		
		py = py + math.sin(A) * ((i+parts))
		py2 = py + math.sin(A) * ((i+parts+16))
		PD[i] = image("gfx/mora/STALKER-LL/Items/1x1.bmp",0,0,1,0)
		local rD = math.random(-4,4)
		imagepos(PD[i],px+rD,py+rD,math.deg(A)+90)
		tween_move(PD[i],200,px2+rD,py2+rD,math.deg(A)+90)
		imagealpha(PD[i],0.7)
		tween_alpha(PD[i],200,0)
		imagecolor(PD[i],255,255,0)
		timer(200,"parse","lua freeimage("..PD[i]..")")
	end
IMG:https://i.ibb.co/gJWhSv4/image.png

old Re: Imitating CS2D bullet [Solved]

Mami Tomoe
User Off Offline

Quote
@user Mora:

Problems with that code:
•
math
functions are not localised and called globally every iteration
• The variables are not localised so they might conflict with other similar global varibles. This is both slow and just generally bad practice. (px, py, ...)
• This code isn't contained within a function which isn't a big deal but probably means that the average Joe won't be able to as easily implement it in their code
• The timer will free the image even if the round ended which can cause visual glitches and problems.
• Generally not my cup of tea when it comes to spacing, or lack thereof

This code needs to be adjusted before commercial use.

old [Solved]

Mora
User On Online

Quote
Im not about to make it for other's use right now.
I'm now just trying to add different things, before I will learn language and make my own game.

Thank u everyone who helped. The thread may be closed.
edited 2×, last 16.04.22 10:55:15 am
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview