spawn gun aiming

About advanced scenes, and the Thyme scripting language used in Algodoo.

spawn gun aiming

Postby bosiefus » Sat Jun 04, 2011 5:04 pm

i have made a machine gun that spawns .30 cal bullets and every time i want to aim it, it shoots bullets out in the same place every time. can someone give me a code and instructions to fix this? i would appreciate it :(
Attachments
bullet spawner.phz
(59.83 KiB) Downloaded 51 times
bosiefus
 
Posts: 5
Joined: Sat Jun 04, 2011 4:58 pm

Re: spawn gun aiming

Postby hiltropper » Sat Jun 04, 2011 6:48 pm

do they spawn at the same point or dont they have any velocity?
if they spawn at the same point you have to go into the spawncode and type in
Code: Select all
pos:=e.pos
then they will spawn at the place where the code is triggered (like the point a box gets hit by a laser)

is it that what you meant?
yup yup
yuuuuuuup
yupyupyupyupyupyupyupyupyyyuuuup
hm... signatures...
hiltropper
 
Posts: 85
Joined: Mon Dec 20, 2010 12:02 pm
Location: Germany

Re: spawn gun aiming

Postby bosiefus » Sat Jun 04, 2011 8:43 pm

not realy, i want it to be aim-able so if i turn the gun up it will shoot that way. just download the scene and you will know what i am talking about
bosiefus
 
Posts: 5
Joined: Sat Jun 04, 2011 4:58 pm

Re: spawn gun aiming

Postby daniels220 » Sun Jun 05, 2011 2:31 am

It's extremely difficult in general to rotate objects while spawning them. It's doable with a simple box, less so with polygons.

So if that's what you want...ask one of the Gods of Thyme for a function to rotate a polygon. The rest is just trig to figure out the angle based on e.normal—there's sample code all over this forum. Search for "atan".

If you're okay with spawning a textured box instead, it's much simpler, but still not simple. Again you'll need the angle, but if you're just spawning a single box, there's a property (just "angle" I think) that you can use to directly set the angle.
daniels220
 
Posts: 95
Joined: Mon Aug 31, 2009 11:30 pm

Re: spawn gun aiming

Postby hiltropper » Sun Jun 05, 2011 2:46 am

ok
you take two boxes , the line trough both will be the direction into the bulets fly later
one you give the code onlaserhit=
Code: Select all
scene.my.firstpos=e.pos
the second box you give the same but with
Code: Select all
scene.my.secpos=e.pos
. now when you spawn your bullet , the value "vel" is one position minus the other so you have the vector and they fly right
it should look like
Code: Select all
...
vel:=(scene.my.firstpos-scene.my.secpos);
...

now the direction is right , if they fly too slow , put "5*" or any other in front of the code
Code: Select all
vel:=5*(scene.my.firstpos-scene.my.secpos);

the right angle of the bullet itself (not the angle it flies) can be done with math.atan
Code: Select all
angle:= math.atan((scene.my.firstpos(1)-scene.my.secpos(1))/(scene.my.firstpos(0)-scene.my.secpos(0)))

i will test it and if you have any more question ,ask
[EDIT]
it works
ill upload you a sample scene so you can look at the codes and what i mean with the two boxes
here (it was made with the new 1.9.5 beta , if there are any problems i could make one for the 1.8) :
Rating: rated 5
Filesize: 246.4 kB
Comments: 1
Ratings: 1
download

[EDIT2]: updated it to a bullet surface code that is similar to yours
so you see the only thing you have to mention is : select your bullet, press str+v into any code area and copy the huge matrix that stands behind "surfaces:=" and let this polygon spawn
yup yup
yuuuuuuup
yupyupyupyupyupyupyupyupyyyuuuup
hm... signatures...
hiltropper
 
Posts: 85
Joined: Mon Dec 20, 2010 12:02 pm
Location: Germany

Re: spawn gun aiming

Postby bosiefus » Sun Jun 05, 2011 5:48 am

i realy apreciate the help but i cant understand a thing you guys said. :(
bosiefus
 
Posts: 5
Joined: Sat Jun 04, 2011 4:58 pm

Re: spawn gun aiming

Postby Someone Else » Sun Jun 05, 2011 7:26 am

The intention, I think, is to use various methods to spawn a bullet at the right position and with the right velocity.

You can get the angle out of e.normal by this code:
Code: Select all
angle := (math.acos(e.normal(1)) * {math.asin(e.normal(0)) < 0} ? {1} : {-1}) - math.pi

You may need to adjust this code a bit so it actually works. I think it will, but I am not sure. You may need to change the less than to a greater than, or add math.pi at the end, or add (2 * math.pi)...

The velocity of the gun can be calculated by:
This bit goes out of the spawn code. Also put it on an alternating collider. Let me know if you don't know what that is.
Code: Select all
textureMatrix = [e.pos(0) - textureMatrix(2), e.pos(1) - textureMatrix(3), e.pos(0), e.pos(1), 0.0, 0.0, 0.0, 0.0, 0.0]

This bit goes in the spawn code:
Code: Select all
vel := [textureMatrix(0), textureMatrix(1)] * 50

This code will spawn the geom already moving away from whatever spawned it:
Code: Select all
vel := e.normal * 100

However, when you combine the above two codes to get this:
Code: Select all
vel := ([textureMatrix(0), textureMatrix(1)] * 50) + (e.normal * 100)
It doesn't work. The spawned object has the wrong velocity, for some so far undisclosed reason.
Matthias Wandel is epic, in my humble opinion.
I love my brain...
TC42 wrote:Also, your sig is too big, please change it.

ARE YA HAPPY NOW?????

Thymechanic/Phundamentalist

Recently, I discovered something a lot of you probably already knew: Minecraft is awesome.
Due to this, I may not be as active as usual for a while.
User avatar
Someone Else
 
Posts: 1147
Joined: Sun Nov 21, 2010 10:53 pm
Location: The Milky Way Galaxy

Re: spawn gun aiming

Postby hiltropper » Sun Jun 05, 2011 12:16 pm

my method is actually not more than geometry...you use only two points for everything.
if you subtract one point from another , you get the vector. the vector shows a force that has a direction and a velocity. it is a line through both points .so you place this vector into the "vel:=" value and anything with this vel flies parallel to the line.
the angle is another thing but even with both points :
for an angle you have to get the tangens of an angle first so you take the y coordinates from both points and subtract them and divide it through both subtracted x coordinates.
now you have a value and now take the arcus tangens for a degree value , what is an angle.
yup yup
yuuuuuuup
yupyupyupyupyupyupyupyupyyyuuuup
hm... signatures...
hiltropper
 
Posts: 85
Joined: Mon Dec 20, 2010 12:02 pm
Location: Germany

Re: spawn gun aiming

Postby niffirg1 » Wed Jun 22, 2011 12:59 am

I remembered how i used to make the angle right, its simple and easy, but ive only tried it with boxes not polygons:
Code: Select all
angle=atan(e.normal(0)/e.normal(1))

running off memory here(from awhile ago) so might be a typo in there, but it looks right
User avatar
niffirg1
 
Posts: 376
Joined: Mon Aug 31, 2009 10:31 pm
Location: The Great American South!

Re: spawn gun aiming

Postby Gear97 » Fri Jul 01, 2011 4:24 pm

Well I think I have a solution..
Rating: rated 6.1
Filesize: 37.16 kB
Comments: 1
Ratings: 3
download

This scene is the RA2lover's response to my original scene that shoots polygons that are bullet shaped. The polygons did not pointed at the right place so RA2lover made this response showing how to make the bullets have the right angle.
Here is it:
Code: Select all
angle := 2 * math.atan( - e.normal(1) / ( - e.normal(0) + 1))
Gears are my favorite mechanism. Why do you think I have this username ? ;)
Make sure to spend some time to see my work http://www.algodoo.com/algobox/profile.php?id=96, just to kill sometime ;)
PS: Gear97™ Is a trademark from me. Any unauthorized use of the term "Gear" for usernames or other naming ideas will be punished.
User avatar
Gear97
 
Posts: 146
Joined: Sat Jan 02, 2010 5:34 pm
Location: Brazil


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 7 guests