Bullet shots - need ideas and help

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Bullet shots - need ideas and help

Postby JernejL » Tue Mar 26, 2019 7:22 pm

I think i need to add some more better way to make bullets react with physical world.

How do you have this implemented? i'm not totally sure how to approach this properly.. i have some terrible code that doesn't work properly and simply needs rewriting. I don't use bodies to simulate bullets as that would be pretty wasteful.

So, what i need is - i know where the body is hit, and i have the full bullet ray from start to end, how would i get this into applying force for this bullet hit at the correct body location and in correct direction of the ray?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Bullet shots - need ideas and help

Postby Dave Gravel » Sat Mar 30, 2019 12:48 pm

Hi JernejL, Maybe it can interesting you.

I have make a quick raycastworld bullet system.
If you like to see it I have make a video.
In the video description you can find the link for download my code exemple, It is very simple.
You need to update the world at 120 or the raycast can miss some shots.
My visual debug is not so precis and it is just a quick helper, I can try to do something better later.

Edited:
If you like to test the code quickly without implement anythings.
In the video description I have add my current early demo and the code for use my plugin too.
You only need to have lazarus 64bit and a video card compatible opengl 3.3 or more.
In OrionX3D.inc file just to set off the define ORIONX3D_MODE_EDITOR
And just to enable the define ORIONX3D_BULLET_SHOOTING
After you can compile the demo.

In the demo F key is use for shot bullets.
The debug bullet is very basic and it display only one bullet at time.

https://www.youtube.com/watch?v=U17IAZ28uGc
Last edited by Dave Gravel on Sat Mar 30, 2019 8:05 pm, edited 1 time in total.
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Bullet shots - need ideas and help

Postby misho » Sat Mar 30, 2019 1:18 pm

This shouldn't be too difficult. You need:

vector: velocity of bullet
scalar: mass of the bullet
vector: point of impact on the body

Then - calculate the linear and angular momenta for the bullet,
Then, use NewtonBodyApplyImpulsePair() to apply these vectors to the body being shot at.

The above method also requires a value of "timestep" - an amount of time this momentum is being applied. This time chunk is the amount of time the bullet is in contact with the body ( a very short time ) - a time interval of one physics cycle.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Bullet shots - need ideas and help

Postby JernejL » Wed Apr 03, 2019 10:02 am

Dave Gravel wrote:Hi JernejL, Maybe it can interesting you.

I have make a quick raycastworld bullet system.
If you like to see it I have make a video.
In the video description you can find the link for download my code exemple, It is very simple.
You need to update the world at 120 or the raycast can miss some shots.
My visual debug is not so precis and it is just a quick helper, I can try to do something better later.


Thank you!! i checked this out very quickly, your method only produces correct force without rotation, but is a good and correct start. i always thought my force at the hit point would be the bullet direction, but i should have been using the hit normal instead - so i took your approach and combined it with something else that i already used for applying off-center forces for simulating car wheels.

Code: Select all

Bullet hit.. i taker your approach to invert normal and convert it to force, and also take the world hit coordinates.
 
        expladd.X := -hitnormal.X * 15000.0;
       expladd.Y := -hitnormal.Y * 15000.0;
       expladd.Z := -hitnormal.Z * 15000.0;   



Now i put this and apply it as global force using second function. I use "expladd" for force and world coords for point:

Code: Select all
 
// Adds force to body at a specified point in global space
procedure AddPointGlobalForce(const body: PNewtonBody; const PrevBodyMatrix: pointer; const Force, Point: Vector);
var
   GlobalForce: vector;
   bodymatrix:  Pmatrix4f;
    bodymatrix2:  Tmatrix4f;
   Torque:      vector;
begin

   if PrevBodyMatrix = nil then begin
      NewtonBodyGetMatrix(body, @bodymatrix2);
        GlobalForce.set_values(Point.x - bodymatrix2.vectors[3][0], Point.y - bodymatrix2.vectors[3][1], Point.z - bodymatrix2.vectors[3][2]);
   end else begin
      bodymatrix:= PrevBodyMatrix;
        GlobalForce.set_values(Point.x - bodymatrix.vectors[3][0], Point.y - bodymatrix.vectors[3][1], Point.z - bodymatrix.vectors[3][2]);
   end;

   Torque := GlobalForce.CrossProduct(Force);

   NewtonBodyAddForce(body, @Force.pointer_to);
   NewtonBodyAddTorque(body, @Torque.pointer_to);

end; 
 


This creates really good results, see video, all shots make vehicles respond properly:



Also, another suggestion, i see you still use old-style operations on vectors in pascal, i'd propose you use operator overloading and methods in records on your vector & matrix structs, it makes vector operations so much cleaner and simplier to use.

You can see example of this in bero's work: https://github.com/BeRo1985/pasvulkan/b ... n.Math.pas

see how "TpvVector3=record" is defined.

@misho

NewtonBodyApplyImpulsePair seems like poorly undocumented - a good example of this would be helpful to me. I'm not sure on relatioships between force, torque and impulses, so this seems a bit beyond reach of my knowledge, but if you know how it works - we could use that to document the wiki pages of 2 functions related to it.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Bullet shots - need ideas and help

Postby Dave Gravel » Wed Apr 03, 2019 11:57 am

Nice it seen to work good.
i checked this out very quickly, your method only produces correct force without rotation, but is a good and correct start.


Yes I have just quickly implement the base because in my system I like to have more bullet models not only raycast. I don't have finish to think about how I implement it in my system this is only a quick test.
In some demos I need real bullet body and some more options, In my case the bullet is not only shot from weapons the bullet can become a fracture object or anythings.

Also, another suggestion, i see you still use old-style operations on vectors in pascal, i'd propose you use operator overloading and methods in records on your vector & matrix structs, it makes vector operations so much cleaner and simplier to use.


Thanks for the suggestion but I don't think to change my vector and matrix stuff.
In my c++ plugin I use glm math lib and it working very good with my current vector&matrix method.
I can share all my maths too with simple pointer glm vs my project.
It can become a very long process to change all everywhere.
I use the old GLScene vector&matrix tools, I have write many helpers for work with this tools.
I prefered to have the vector and matrix record separate from the tools function, I don't like the idea to use it like a record class. My vector and matrix and quaternion is very similar to newton math tools. It is more easy for me to translate the joints code and more from newton sdk to pascal. I use it from 2006 I can't imagine all the changes that I need to do for change it with a other method hehe.
My name is not present in the page but many of my work come from far in time, You can see in this page one of my first project about physics&GLScene.
http://wiki.freepascal.org/GLScene
I'm the first with a other GLScene user to have make work GLScene inside lazarus ide.
About bero's for sure he do nice work but like I have say before I'm not so fan about the coding method. I let's it for others.

NewtonBodyApplyImpulsePair seems like poorly undocumented

I never have try the NewtonBodyApplyImpulsePair.
I have already use the normal impulse command but it make very longtime that I don't have use and test it.

I'm not sure if you have see only my bullet code or my sdk for the plugin.
Currently the sdk is not really clean and not so user friendly,
Like I have say I have many projects and I slowly add it all in my plugin sdk.
My current online sdk for the plugin is a very early version.
I have let's it in public anyway because I have some users using my plugin with personal project and they resquested some code to start her project.

If you like to see what is my plugin and how it work, I'm going to out a new sdk version this friday on the night.
I have work to separate the plugin code from the demos, I have do a big refactorization in my code.
What people need to understand about my plugin and my code, The demos is only some exemples for show how to use my plugin.
The cool thing if a user don't like my plugin GL he can use a personal engine and use my demo code as exemple model only.
The user is not in the obligation to use my vector&matrix tools,
The plugin can work with any math tools.
The plugin don't hide anythings to the user, Exemple the plugin have a default camera and mouse pick system.
If the user don't like to use my implementation about cam&mouse, In the sdk I have camera and mouse pick code exemple to let's the user create self method.

PS: In a other forum I have see that you have trouble to build the enet dll.
In the project you need to change the lib option for dll, and take a look to the file name too.
The vs c++ don't rename it to dll by self, You need to rename the .lib to .dll in the project config.
You need to add ENET_DLL flag and you need this lib too WSock32.lib;WS2_32.lib;Winmm.lib
In the general config you need to change Whole Program Optimization for No Whole Program Optimization.
https://sites.google.com/site/evadlevarg/abcd.png
If you are interested to see my plugin and demos code, Just to take a look this week-end in my youtube you can find my newer sdk version with the refactorization code.
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Bullet shots - need ideas and help

Postby JernejL » Thu Apr 04, 2019 1:38 am

You can now make use of my additions to improve your ray bullets :)

Glscene was also one of my first 3d libraries, i still use it nowdays for some things, it can prototype things fast. I also used three.js and it reminds me of glscene in many ways, regarding vectors it was just a suggestion - i've been adapting myself to newer pascal syntax and found out i can do more and make source code clearer.

Regarding enet: yes i had this problem, and super thanks for your instructions! I did find precompiled binaries in the end but i'd rather compile my own. It also happens that bero has once again did a wonder.. he has 2 projects, one is enet translated to pascal (but is buggy so i can't use it) and another he has a realtime network library RNL which i might use in the future as the feature set seems very good!

But for now enet will more than do - it is working very good for me, i hope i can compile it for other plafrorms too.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Bullet shots - need ideas and help

Postby Dave Gravel » Thu Apr 04, 2019 3:35 am

Yes and thanks for the share too.
About enet you can take a look on this project, It seen to work good.
I think to have read about no IPV6 support for this one, but it have work good for me before with IPV4.
https://github.com/rasberryrabbit/enet-freepascal

Currently I use this one with my dll, I have do little modifications for my use.
https://github.com/DJMaster/enet-fpc/tr ... er/binding
My client server work ok but I don't have get time to test it more.

If you get again problem with the dll you can pm me and I can make a video for show what I do.
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Bullet shots - need ideas and help

Postby JernejL » Thu Apr 04, 2019 5:56 am

Amazing - another project i was totally unaware of - another enet pascal translation! I will try this with my stress test if it works, if this one works then i can possibly port bero's enet ipv6 patches to it (that part of his does work), and get a working ipv6 pascal enet out of it.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 7 guests

cron