Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] Help with bullet direction?

Author
Message
RigSaw
7
Years of Service
User Offline
Joined: 25th Sep 2016
Location:
Posted: 16th Oct 2018 15:02
I apologize for this, I made a post last night and now realize it wend to the dbpro forum and not to AppGameKit. I am trying to find out how to do something in AppGameKit that parallels something I know how to do in dbpro...

Original post here
https://forum.thegamecreators.com/thread/223098#msg2632015

I'm probably trying to re-invent the wheel with this, but I could not seem to find any basic shooter examples out there. There are a million examples for 2d shooter, but not finding any for 3d...
Any help greatly appreciated.

The author of this post has marked a post as an answer.

Go to answer

fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 16th Oct 2018 16:53 Edited at: 16th Oct 2018 17:10
To shoot in 3d you need to make use of vectors and the physics commands work well for that

The routine I use for my 3d Joust is like the following



you just call this routine when you want to shoot which basically sets the angle speed and mass and the update physics command makes sure it moves



When using a method such as the above you need to do some housekeeping in your code to prevent too many bullets and the code running slower and slower



You need this command in your main loop Step3DPhysicsWorld()
fubar
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
RigSaw
7
Years of Service
User Offline
Joined: 25th Sep 2016
Location:
Posted: 17th Oct 2018 02:59
Thanks blinkok, I tried using that but the problem with SetObjectLookAt is you have to give it specific x y z coordinates, So I can tell the bullet the target, and it will go there every time. What I want is like in a 3d shooter, the bullet goes where ever the center of the camera is pointing, even if you are looking at a wall (think doom, cod, any 1st person shooter)

Man there is a million 3d shooters out there, but I guess I never realized how hard it would be to explain / code what a bullet does when you click the mouse and the gun goes off, and the bullet goes out of the end of the barrel in the direction you are pointing your camera or character. In call of duty the bullet would go forward directly away from you. So far in my game, you pull the trigger and the bullet follows the z axis or x axis(MoveObjectLocalX, MoveObjectLocalY, or MoveObjectLocalZ). So you would shoot the guy next to you instead of the bullet going where you would expect it

Man I really wish AppGameKit gave you a basic 3d shooter example. (I know they have the smash it demo, but that uses the mouse to target, not the camera, and you only move in one direction)

Thanks fubarpk, I think your example is what I'm trying to do, but I have no idea how to grasp what you are showing me yet...
Think of this in its most basic form. In a 3d scene, there is an the player (camera) standing on a flat plane. WSAD controls. Not even looking up or down. You can go forward, back, left or right.


I have 1 bullet and I want to tell it to move from my XYZ coords to what ever direction the camera has rotated. How do I tell the bullet to rotate the same direction as the camera before it starts moving?
Everything is going just ducky if I am facing in one direction. But once I rotate the camera on the Y axis thats when the trouble starts. I can tell the bullet to rotate manually, but how do I some how "map" the bullet rotation to the camera rotation?
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 17th Oct 2018 20:17
This post has been marked by the post author as the answer.
Can't you just copy the camera angles into the bullet.
SetObjectRotation(bullet, 0, 0, 0);
SetObjectRotation(bullet, GetCameraAngleX(1), GetCameraAngleY(1), GetCameraAngleZ(1));
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 17th Oct 2018 20:28 Edited at: 17th Oct 2018 20:59
yes you can in that case SetObjectRotation(projectiledirbox, GetCameraAngleX(1), GetCameraAngleY(1), GetCameraAngleZ(1));

projectilebox in the above is just where the player would be and their current world angle but an object is needed so as it can be moved on z for calculations

Apologies the above might be a little confusing as I used it for the enemy to point at the player hence the current look at commands pointing at a player.id

replace


with



and if you know the object you are aiming at great for the enemy you can use this function where mytarget is the id of the target and objecteId is the object firing the bullet
fubar
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 17th Oct 2018 21:01
Do you still need help? If yes, you only want to shoot straight, not up or down?

You could calculate the angle of the direction in which you shoot (0 could be north, 90 east, 180 south, and 270 west). Then you use trigonometry, imagine your flat playfield viewed from above and 0 still being north, 90 east, and so forth. Then you can use sine and cosine to calculate where to position your bullet. If you understand how sine and cosine work it will make it much easier to work with. Here is a short example of some sine and cosine I have used for some bullet stuff.



https://en.wikipedia.org/wiki/Trigonometric_functions

If you look at this Wiki entry, you will see an image of (we call it "The unit circle" in Denmark, because you should imagine it being 1 unit in radius), if you take an angle from the center and draw a straight line until it hits the circle, you will get what the sine value should be if you draw a line towards the Y-axis, and you will get the cosine value by drawing a line from the circle touch point towards the X-axis. You can use these values to move a bullet along the line from the center. Now you can see that an angle of 30 degrees will produce a sine of 0.5 (draw the line from the center at your specified angle until the line hits the circle, draw a line from there towards the Y-axis and it touches at 0.5). This value, you can now use that to move your bullet one step at a time (0.5 is how much the Y coordinate needs to change every you move a step of 1 whole unit, but the unit could be anything, also a float of 0.7 or something else if that suits your bullet movement better. This is also the same for X coordinate but just using the cosine instead to manipulate the X coordinate of the bullet). My +90 is just to make my stuff suit my stuff, IRC my north was not at the top of my playfield, but I made it be at the top, therefore I needed to add 90 degrees.
13/0
RigSaw
7
Years of Service
User Offline
Joined: 25th Sep 2016
Location:
Posted: 17th Oct 2018 23:17
Guys I can't thank you all enough! blink0k that was exactly what I was looking for and it worked perfect for what I am doing. Thank you fubarpk & Cybermind as well. I hope to be able to use the methods you both suggested in the future. My grasp of the language is still very weak, so I have trouble with anything but the most basic examples, but I will keep at it and circle back here later to try these other methods later. Compute and control \m/!

Login to post a reply

Server time is: 2024-04-20 02:56:38
Your offset time is: 2024-04-20 02:56:38