Jump to content

Physics bug (falling through floor)


Brutile
 Share

Recommended Posts

I decided to make a mini golf game for the game tournament, but I'm being haunted by some physics bug. I have a ball that is 0.1 scale and after it gets hit, once it gets to a specific point, it falls throught the floor. It happens without fail. The hotspot seems to be in the middle of the CSG where the two triangles meet. Even when I scale the ball to 1, it doesn't fall through the floor, but it does hit a bump and put it off course.

 

Here's my script in case you can see anything that might be wrong.

 

Script.mouseSensitivity = 2.0
Script.camOffset = Vec3(0, 1, -3)
Script.camSmoothing = 10.0
Script.hitForce = 350
function Script:Start()
--initialize camera
self.camera = Camera:Create()
self.camera:SetFOV(70)
self.camera:SetRange(0.05, 1000)
self.camera:SetMultisampleMode((System:GetProperty("multisample", "4")))
self.camera:SetRotation(self.entity:GetRotation(true))
self.camera:SetPosition(self.entity:GetPosition(true) + self.camOffset)
--add listener as camera
self.listener = Listener:Create(self.camera)
--create player model
self.entity:SetPosition(self.entity:GetPosition(true) + Vec3(0, 0.1, 0), true)
self.model = Model:Sphere()
self.model:SetScale(0.1, 0.1, 0.1)
self.model:SetPosition(self.entity:GetPosition(true), true)
self.model:SetParent(self.entity)
self.model:SetColor(1,1,1)
--setup ball physics
self.entity:SetMass(1)
self.entity:SetFriction(0.1, 0.1)
self.entity:SetSweptCollisionMode(true)
--Create a shape
local shape = Shape:Sphere(0,0,0, 0,0,0, 0.1,0.1,0.1)
self.entity:SetShape(shape)
shape:Release()
--set collision type of player
self.entity:SetCollisionType(Collision.Prop)
local window = Window:GetCurrent()
local context = Context:GetCurrent()
window:SetMousePosition(Math:Round(context:GetWidth() / 2), Math:Round(context:GetHeight() / 2))

self.mouseDifference = Vec3(0, 0, 0)
self.camRotation = Vec3(0, 0, 0)
self.camera:SetRotation(self.camRotation)
end
function Script:UpdateWorld()
local window = Window:GetCurrent()
local context = Context:GetCurrent()
--mouse look
self.currentMousePos = window:GetMousePosition()
window:SetMousePosition(Math:Round(context:GetWidth() / 2), Math:Round(context:GetHeight() / 2))
self.currentMousePos.x = Math:Round(self.currentMousePos.x)
self.currentMousePos.y = Math:Round(self.currentMousePos.y)
self.mouseDifference.x = Math:Curve(self.currentMousePos.x - Math:Round(context:GetWidth() / 2), self.mouseDifference.x, 3)
self.mouseDifference.y = Math:Curve(self.currentMousePos.y - Math:Round(context:GetHeight() / 2), self.mouseDifference.y, 3)
self.camRotation.x = Math:Clamp(self.camRotation.x + self.mouseDifference.y / self.mouseSensitivity, 0, 70)
self.camRotation.y = self.camRotation.y + (self.mouseDifference.x / self.mouseSensitivity)
--camera orbit around ball
self.camera:SetRotation(self.camRotation)
self.camera:SetPosition(self.entity:GetPosition(true), true)
self.camera:Move(self.camOffset)
--hit the ball
if window:MouseHit(1) then
 local c = self.camera:GetPosition(true)
 local p = self.entity:GetPosition(true)
 local dir = p - Vec3(c.x, p.y, c.z)
 dir = dir:Normalize() * self.hitForce
 self.entity:AddForce(dir)
end
end

 

All I did was create a new map, added a pivot at 0,0,0 with this script assigned, then created a CSG cube at pos: 0, -16, 2688 and size: 512cm, 32cm, 5888cm.

Hit the ball without moving the mouse and it will get half way down and fall through.

I even tried a small 512x512 floor with walls and just hit it around. Eventually it would fall through.

Link to comment
Share on other sites

Thats sound like same issue I was having when I made my minigolf game couple years ago. Physics accuracy for the ball were attrocious. After a lot of fumbling about I gave up.

The ball seemed to be filled with water or something, it would roll straight for a while and then all of the sudden it would roll in to a different direction. This mostly happened when slowing down.

 

Things to try (which some of you already have):

  1. Swept collision on ball and level geometry
     
  2. Setting a higher physics resolver interval
     
  3. instead of a csg sphere, using a model with a different physics shapes, going from relatively basic, to extreme high polycount (for a ball).
     
  4. Larger model/csg sphere instead of the tiny scale you are using now.

  • Upvote 1
Link to comment
Share on other sites

It is possible you might get better results with a larger ball. You would need to make everything in your level larger, of course.

 

I would like to see a demo of this in action.

 

Jorn, without having the framerate displayed it is difficult to tell what is happening there.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...