Physics problem
Blitz3D Forums/Blitz3D Beginners Area/Physics problem
| ||
I'm having a little trouble with my game physics. I've defined a type object called Mass and added the mesh to be used as a field, as well as some important info about the mesh (it's current velocity, previous location, etc). What I need to do now is return the type object of the mesh when using the CollisionEntity() function (at the moment it only returns the mesh) so I can get ahold of the velocities etc. Is this possible, or do I need to rethink my strategy here?Type Mass Field name Field mass Field Msh Field x# Field y# Field z# Field xvel# Field yvel# Field zvel# Field xacc# Field yacc# Field zacc# End Type oMsh = CreateSphere() m1.mass = createmass(omsh,200,0,0,10) FreeEntity omsh EntityColor m1\msh,255,0,0 omsh = CreateSphere() m2.mass = createmass(omsh,-200,0,0,10) FreeEntity omsh EntityColor m2\msh,0,255,0 c1 = CreateCamera() PositionEntity c1,0,0,-1000 CameraRange c1,0.1,2000 l1 = CreateLight() PositionEntity l1,0,0,100 m1\xvel = -5 m1\yvel = 0 m1\zvel = 0 m2\xvel = 5 m2\yvel = 0 m2\zvel = 0 Collisions 1,1,1,1 While Not KeyDown(1) Locate 0,0 For m.Mass = Each Mass For Col = 1 To CountCollisions(m\msh) ColEnt = CollisionEntity(m\msh,1) ;need to retrieve colEnt's xvel, yvel and zvel values here Next CalculateNewtonian(m) MoveEntity m\msh,m\xvel,m\yvel,m\zvel Next UpdateWorld RenderWorld Flip Wend many thanks for any help you can give |
| ||
Not that I can help but you may wanna edit your post so that the code bit has a [ instead of a < Now may the helping begin by pepole who know lots more than I |
| ||
Thanks Agamer. This is my first post here so I need all the help I can get :). |
| ||
Why do you FreeEntity the mesh?oMsh = CreateSphere() m1.mass = createmass(omsh,200,0,0,10) FreeEntity omsh |
| ||
You mean this?Type TExample End Type Function CreateType.TExample() newExample.TExample=New TExample Return newExample End Function |
| ||
GitTech -Sorry, I should have mentioned I didn't post all of the code. I have a couple of functions set up including the CreateMass function which does pretty much what is written in your post:Function CreateMass.Mass(msh,x,y,z,mass) m.mass = New mass ScaleEntity msh,100,100,100 m\msh = CopyEntity(msh) PositionEntity m\msh,x,y,z EntityType m\msh,1 EntityRadius m\msh,100 m\x = x m\y = y m\z = z m\mass = mass Return m End Function My problem is not with the object creation...that works fine. What I need to do is find the type object (m) containing the second mesh involved in the collision(colEnt). Any thoughts on this, or am I doing things completely the wrong way round? WolRon - The new code should answer your question. The CreateMass() function copies oMesh, so it's no longer needed, hence using FreeEntity. Thanks for your patience guys, I should have posted this to begin with |
| ||
Is this what you want?For entitysearch.mass = Each mass If ColEnt = entitysearch\msh xvel# = entitysearch\xvel# yvel# = entitysearch\yvel# zvel# = entitysearch\zvel# Exit EndIf Next |
| ||
Also, it appears that you are using a lot of integer variables for you x,y,z coordinates. Is this on purpose or did you mean to use floating point variables (x#, y#, z#)? |
| ||
Doh! Yeah, that's exactly what I'm trying to do. Thanks WolRon! One question though, is this likely to significantly slow down the game (since it has to search through all of the objects for each collision I test) or is it fast enough not to matter? Thanks again for your help! |
| ||
Shouldn't be a problem at all. Computers are fast. Also, the code I showed you exits the For-Each loop as soon as it finds the match, so it saves some time. The greatest slow down will be Blitz itself performing it's collision checking on all of the entities that you set up for collisions. |
| ||
Mido486, What you need to do ( I thought it was well known by now ) is store the pointer integer of the type instance in the entityies name. e.g. m.Mass = new Mass m\Msh = createsphere() NameEntity m\Msh, Handle( m ) Then when you get the entity involved in the collision you can do this to retreive the Type instance .. entity = EntityCollided (m\Msh,ID) If entity <> 0 c.Mass = Object.Mass( EntityName( entity ) ) c\vx = blah c\vz = blah endif For a working example - see 'A Wee Tank Game' in the code archives. Hope this helps. Stevie |