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 / 3d collision again: raycast or any type of it

Author
Message
hoyoyo80
7
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 12th Dec 2017 10:42
Hi all,
I still some curiosity with basic collision with raycast command.

The tricky part is having the "old" X,Y,Z position in order to detect collision. In my game, i ignore these, i put old and new is in the same values, it works but if the object move too fast sometime it fail.
My question is there any work around for the recording tricky "old" x,y,z position?

PS: To AppGameKit Creator, is it possible to make basic 3d collision simpler in the future e.g
1.setobjectcollision(ID,collision on/off,shape of collision) and the check if entitycollide etc etc OR
2.raycasting always start from the object and outward(line or radius)

Thanks
Stab in the Dark software
Valued Member
21
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 12th Dec 2017 13:21
Which 3D collision commands are you using?
How about a code example.

Also check the 3D physics demos.

https://forum.thegamecreators.com/thread/216683
The coffee is lovely dark and deep,and I have code to write before I sleep.
Conjured Entertainment
AGK Developer
18
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 12th Dec 2017 13:27 Edited at: 12th Dec 2017 13:51
Quote: "
The tricky part is having the "old" X,Y,Z position in order to detect collision. In my game, i ignore these, i put old and new is in the same values, it works but if the object move too fast sometime it fail.
My question is there any work around for the recording tricky "old" x,y,z position?"

The same values would be a cast of a single spot and that is why it is not working at fast speeds.

If you want to use the new values, then maybe use them as old and add a little to them for the direction you are traveling for the new.

That way you will be casting out in front a little even at a high speed.

Or even use the new as new and subtract a few for the old, so you cast will be a line and not a dot and have a better chance of detecting a hit.

I have trouble with raycasting too, so I have to play with the numbers a little, but I finally end up with something usable.

Quote: "PS: To AppGameKit Creator, is it possible to make basic 3d collision simpler in the future e.g
1.setobjectcollision(ID,collision on/off,shape of collision) and the check if entitycollide etc etc OR
2.raycasting always start from the object and outward(line or radius)"

Having the option to do things different is better, instead of always having to do it from center as #2 suggests.

#2 is how i use most of my raycasting now, but sometimes I do it other ways.

I use old values in the center of the object, then use new values in direction of travel or desired check.

Since it ignores the backfaces, casting from inside the object works well for me.

Sometimes I even have hidden boxes that are relocated as an object moves to stay with it.

Then, I use them for old and new to cast lines in the shape of the object.

Sometimes is take a lot of hidden boxes for a big oddly shaped object, but it works like a charm.

That is how I am doing the player ship in my anti-poaching game, and it works great to detect running into other ships or icebergs.

I am now working on using this hidden boxes method for the NPC ships to detect icebergs while navigating.

Using 3 boxes for the latter, one in center of ship, and two out in front moved to left and right side a little (width of ship+).

That will allow 3 lines of raycasting for those NPC ships, 2 being 1 from center to each in front, and 1 between the two in front.

That way I will know if the iceberg out in front is left, or right, or dead center. (those ships do not reverse, so bow checks are all that is needed)

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 12th Dec 2017 14:23
It is possible to test for collision of objects using GetObjects3DPhysicsContactPositionVector (I used that in my small game template "Smash-It"). Take a look here for an example.
hoyoyo80
7
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 13th Dec 2017 01:15
Thanks for the ideas. I will try it.

It is possible to use real physic to check regular movement, cause i dont need real physic for now..thanks
hoyoyo80
7
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 15th Feb 2018 01:33
Quote: "Sometimes I even have hidden boxes that are relocated as an object moves to stay with it."


My thread is old, but to let it die when *maybe* i found the solution is not quite nice for other beginner like me.

Ok, with the above idea at make a cube, attach to it with fixobjecttoobject, and use getobjectworldx finally get collision right but i found that when i export to GameGuru, my model that is 1.8 unit in blender need to be rescale 40% in the end it is 72 unit in GG.

When i check back my agk2 model, the one that have problem is only 0.2 in size(its a ball), i rescale and modify my codes raycast work perfect now. So, can i conclude that the scale of a 3d model must be in a bigger scale as in physic?

Thanks
Santman
12
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 15th Feb 2018 10:53
I got round 3d collision in a clumsy but easy way: create 3 variables called oldx, oldy, and oldz.....in these store the camera position. Then use MoveCameraLocalZ however far forward you want to cast the ray. Grab the position again and store it as newx, newy, newz or something. Then just set the camera back to the old position, and you have two sets of recast coords.

Works with objects too.

Probably not the most elegant solution but is perfectly fast for me.
PHeMoX
6
Years of Service
User Offline
Joined: 9th Jan 2018
Location:
Posted: 15th Feb 2018 15:11
I'm not 100% sure, but I think the trick Santman is referring to is basically a trick to get the correct raycasting angle / directions going, which can be a bit tricky indeed.

Quote: "So, can i conclude that the scale of a 3d model must be in a bigger scale as in physic?"


Probably not necessarily true. Maybe the raycasting might simply misses the smaller object?? I would kind of wish we would have a raycasting 'debug' function that would show the exact shape of how it casts rays. It took me a while in my own coding to realize the raytracing from camera position actually went backwards, instead of forward. I wanted code to return objects clicked for future RPG usage of picking up items by clicking on them. I modified the raycasting example from the Projects folder.
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 15th Feb 2018 16:50 Edited at: 15th Feb 2018 16:52
@Santman that is the same way I do it in my old school FPS project. The first thing I looked for was a method to cast a ray/sphere in direction an object is currently facing. Did not find one. Found the methods needed begin and end points of the cast. So I just move an object to get the end points then can use the cast methods.

I think in AppGameKit this is basically the only way to do it other than writing a function ourselves to calculate the end position based on angle facing and length of cast (i.e. distance moved).

There might be something in the math api to do it. Not sure but I thought I gave it a quick glance and didn't notice anything. But it almost seems like I have seen some math functions in the api for this kind of thing in the past. Might be thinking of a different api.

All of that to say I think our "clumsy" way is actually a very precise and straightforward way of getting that critical end position. It works at least. Lol
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
puzzler2018
User Banned
Posted: 15th Feb 2018 17:26
we are using it minecraft revisited too
Santman
12
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 15th Feb 2018 17:40
Puzzler, I know......I did it.

I'm sure there will be some super complex way of doing It by maths, but for me this is the very point of AGK.....Paul did all that work for us. Lol. It gives the start and end point thoigh, then you just cast for the first hit object and its perfect for fps. Objects can also use SetObjectLookAt - so you can create a dummy object at an eneies position and then set it to look at any other object (for example the player, another object) and you can get the vector from it.

These commands can be really powerful. The only thing I'm not sure we can control is the cast precision? What i mean is, if ypu cast a ray 100 points forward, AppGameKit steps along this in interval X and checks each collision object at this point to see if it falls within the bounding box. But we can't control the interval. So in theory, if the interval was 1 3d unit, and your object was 0.5, it could be missed? In which case you'd need to step the start forward 0.25 and cast a second time. Not played enough to know how impactful it would be.
puzzler2018
User Banned
Posted: 15th Feb 2018 17:55
And indeed I will always give credit where things are due - thank you for your bits of info and snippets on that thread
Santman
12
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 15th Feb 2018 18:49 Edited at: 15th Feb 2018 18:50
Sure only because i skipped ahead and tried that bit first. You guys seem to be making awesome progress though.
hoyoyo80
7
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 15th Feb 2018 21:00
If im not mistaken, in blitz3d the linepick command they use is just a matter setting the source,the direction and length.

But glad i get the idea of the "dummy" mesh method from this community.

Phew....even in simplified programming this is hard for me. But AGK2 is super fun.
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 15th Feb 2018 21:15 Edited at: 15th Feb 2018 21:18
@Santman I agree and same thing I was thinking. Had the thought of writing a function to return a Vector3 type with the end coord for about 1/10th of a second and said ah just move an object forward get its new position... done. Simple is GOOD.

On detecting collisions with tiny objects or very fast moving objects yes I don't know how AppGameKit api steps but what I do is figure out (kind of an inherit knowledge thing we developers have I mean knowing the smallest object sizes and max speeds) the step size needed and then would loop check.

This is how I do it in 2D too with my own line casts. I always use tiles/map cells to detect collisions with level environment. So if speed of object has them moving more distance in one frame than the width or height of a tile cell then I divide that out first to get the step. So tile size of 16x16 and say a tiny bullet moving crasy fast 40 pixels per frame would become 3 loop iterations of approximately 14, 14 and 12.

It could be the internal methods always do x checks say 100 steps per cast so a ray of distance 100 would be incrementing 1 unit per check and distance of 10 would be 0.1 increments per check. OR it could be they always do a fixed step increment regardless of distance. I don't know how they implemented it but I know most apis have issues with this eventually in some cases so it is advised to manually do multiple checks ourselves.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 17th Feb 2018 16:54 Edited at: 17th Feb 2018 16:59
Quote: "I got round 3d collision in a clumsy but easy way: create 3 variables called oldx, oldy, and oldz.....in these store the camera position. Then use MoveCameraLocalZ however far forward you want to cast the ray. Grab the position again and store it as newx, newy, newz or something. Then just set the camera back to the old position, and you have two sets of recast coords.

I'm sure there will be some super complex way of doing It by maths, but for me this is the very point of AGK.....Paul did all that work for us. Lol. It gives the start and end point thoigh, then you just cast for the first hit object and its perfect for fps."


Its not clumsy. Id call it clever. This is actually a very good way of doing it as its very intuative and simple but gives good results

Technically you can get the camera forward vector with:
x# and y# are the middle of the screen
VecX# = Get3DVectorXFromScreen( x#,y# )
VecY# = Get3DVectorYFromScreen( x#,y# )
VecZ# = Get3DVectorZFromScreen( x#,y# )

Obviously the camera is at GetCameraX(),Y(),Z() etc.. and the vector above points in the direction of the camera by one unit - you can make it as big as you want by multiplying the values by a constant.

The VERY mathematical solution can be from using the CameraAngles functions or even using the CameraQuaternion values...

Camera Forward Vector (where qx-qw are the GetCameraQuaternionX(),Y(),Z(),W()
x# = 2 * (qx#*qz# + qw#*qy#)
y# = 2 * (qy#*qz# - qw#*qx#)
z# = 1 - 2 * (qx#*qx# + qy#*qy#)

But why calculate this when its so eay to move a dummy object or camera and get easy results.

Quote: "
These commands can be really powerful. The only thing I'm not sure we can control is the cast precision? What i mean is, if ypu cast a ray 100 points forward, AppGameKit steps along this in interval X and checks each collision object at this point to see if it falls within the bounding box. But we can't control the interval. So in theory, if the interval was 1 3d unit, and your object was 0.5, it could be missed? In which case you'd need to step the start forward 0.25 and cast a second time. Not played enough to know how impactful it would be."


As far as I know, the cast precision is almost infinately precise

Well, as accurate as a float can be as its a mathematical intersection formula that is used to get the cast impact point. Its not multiple tests at iterations of the movement. There are loads of formulas for collisons of Ray-Plane, Ray-Triangle, Ray-Circle formulas on the web which essentially have infinite resolution (well as accurate as a float value can be). The maths can be daunting but we dont have to care how it works...just that it does work and its basically just doing 3d maths to find intersections between lines.


The physics intersections in AppGameKit is much more complex topic though and typically bullet physics collision engine is used and so fast moving items can effectively pass through other objects if there are not enough iterations of positions. id cast a fast moving object into a scene along a vector only and then calculate if it intersects with other objects first. Only if it does then let the physics engine work out exactly where.
Stab in the Dark software
Valued Member
21
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 17th Feb 2018 19:40
Quote: "The physics intersections in AppGameKit is much more complex topic though and typically bullet physics collision engine is used and so fast moving items can effectively pass through other objects if there are not enough iterations of positions. id cast a fast moving object into a scene along a vector only and then calculate if it intersects with other objects first. Only if it does then let the physics engine work out exactly where."


This unnecessary since the Bullet Physics engine has constant collision detection and a max linear velocity.
The coffee is lovely dark and deep,and I have code to write before I sleep.
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 17th Feb 2018 19:57 Edited at: 17th Feb 2018 19:59
It has been well documented that bullet and other physics simulators dont necessarily detect collsions between objects if one is moving so fast that it completley passes through another object without it spending at least one rendered frame where the two objects intersect each other. Its a matter of what the simulation speed is and how small the physics time step is. Typically its only a problem for bullets/missiles etc that move very fast. I have had this exact problem before with this physics engine (bullet) .... im not sure how its been solved in AppGameKit??

Its even been discussed on the Bullet forums and others suggest a ray cast for very fast objects

Stab in the Dark software
Valued Member
21
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 18th Feb 2018 04:23
Quote: "I have had this exact problem before with this physics engine (bullet) .... im not sure how its been solved in AppGameKit??"


AGK has constant collision detection turned on for all dynamic objects.
A max linear velocity is setup in a callback when steping the world.
There is also a minium size for dynamic objects.

The coffee is lovely dark and deep,and I have code to write before I sleep.
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 8th May 2018 17:34 Edited at: 9th May 2018 01:30
I have some examples that use raycasting for collisions here
https://forum.thegamecreators.com/thread/222027

I found when I used proper models that were scaled down to much the collisions didn't work
I fixed this by creating a dummy model box in AppGameKit and placed it at the same location of the model
The dummy model was only used for collisions so had visibility off but collisions set to on. That
really helped when I added a proper car model to my driving game


I hope that helps
fubar

Login to post a reply

Server time is: 2024-03-28 20:36:00
Your offset time is: 2024-03-28 20:36:00