Jump to content

Need a little help for simple pong script


KilljoyHeathen
 Share

Recommended Posts

Hey,

I need a little help with a simple project I am trying to create, yes I am a novice at this. I made a simple Pong board and have my moving paddles on each side with associated keys, I have tried multiple ways to get the ball to act the way I want, all to no avail.

 

I am trying to get the ball to move in a way you would want it to for this kind of game, ride on the Y-axis platform while moving on the X and Z. I get normal behaviour to begin with, then it gets quite out of control fast. I don't know what I am doing wrong, any help in the right direction would be appreciated.

 

This is the ball's script.

local dirX = 1
local dirZ = 1
local ballSpeed = 0.1
--local ballTimer = 1.5
--local gameTimer = 0
function Script:Start()

self.entity:SetKeyValue("event", "true")

end
function Script:UpdatePhysics()
if self.entity:GetKeyValue("event") == "true" then
self.entity:Move((dirX * ballSpeed),0 , (dirZ * ballSpeed),true)
end
end
function Script:Collision(entity, position, normal, speed)
if (entity:GetKeyValue("type") == "player") then
dirX = dirX * -1
end

if (entity:GetKeyValue("type") == "boundary") then
dirZ = dirZ * -1
end
end

 

Player's script (left and right paddle scripts are identical except for differing move keys)

Script.paddleSpeed = 0.2 --float "Paddle Speed"
function Script:Start()
self.entity:SetKeyValue("event", "none")
self.entity:SetKeyValue("type", "player")
end
function Script:UpdateWorld()
local window = Window:GetCurrent()
local padSpeed = self.paddleSpeed

padSpeed = padSpeed + (Time:GetSpeed()/100)

self.entity:AlignToVector(Vec3(0,0,0),2)

if window:KeyDown(Key.Q) then
if self.entity:GetKeyValue("event") == "hit" then
self.entity:Move(0,0,-padSpeed,true) --Have to move paddle back a bit or get collision issues
self.entity:SetVelocity(Vec3(0,0,0),true)
self.entity:SetKeyValue("event", "none")
elseif (self.entity:GetKeyValue("event") == "none") then
self.entity:Move(0,0,padSpeed,true)
end
end

if window:KeyDown(Key.Z) then
if self.entity:GetKeyValue("event") == "hit" then
self.entity:Move(0,0,padSpeed,true) --Have to move paddle back a bit or get collision issues
self.entity:SetVelocity(Vec3(0,0,0),true)
self.entity:SetKeyValue("event", "none")
elseif (self.entity:GetKeyValue("event") == "none") then
self.entity:Move(0,0,-padSpeed,true)
end
end
end
function Script:Collision(entity, position, normal, speed)
if (entity:GetKeyValue("type") == "boundary") then
self.entity:SetKeyValue("event", "hit")
end
end

 

Finally boundary script

function Script:Start()
self.entity:SetKeyValue("type", "boundary")
end

 

I had much more code in the ball's script but after so may times of deleting trying something else I have no clue what the best course is.

Link to comment
Share on other sites

I'm no expert but looking at your script, you don't control over real speed of the ball.

 

I personally would not use :

self.entity:Move((dirX * ballSpeed),0 , (dirZ * ballSpeed),true)

Why you are multiplying ? This makes you ball to accelerate very quickly.

 

In your pad movement you are using

self.entity:Move(0,0,padSpeed,true)

which makes more sense as you moving your object by a constant speed , here padSpeed. Have you tried applied same schema to ball movement ?

Link to comment
Share on other sites

I am using dirX * ballSpeed because that is simply changing its direction, the dirX and dirZ are equal to 1 so any speed multiplied by 1 would simply be the same speed, it's when it hits a boundary or player that it changes to -1 which again leaves the speed the same but changes the direction. The ball works for the first 4 hits then it starts hovering and doing loop-da-loops and eventually falling off the board. I have read a post on here about reflecting projectiles and am trying to incorporate that, suggestions were raycasting and turning gravity off....but no luck so far.

Link to comment
Share on other sites

Is this a physics object? If so, it might not be the best idea to combine absolute position commands with the natural physics of the ball. I would suggest either doing one or the other. Also, there are two issues with using SetVelocity like you are doing. One is that it's not in the UpdatePhysics() loop, which means that it may not synchronize completely with the physics. The other is that the ball still has acceleration, and collisions with absolute movement can cause physics glitches with super acceleration. Have you taken a look at the marble demo? It might be a good place to look for how to handle the ball.

Link to comment
Share on other sites

Yes I have went through the marble demo, thanks for the point out of the SetVelocity() I will modify that part, I am playing with adding force like the marble project, but need a way to limit its speed which I am actually trying right now. Thanks for the responses guys I believe I am on the right track now...will update if I fail or succeed.

  • Upvote 1
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...