Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Top down car game — Gideros Forum

Top down car game

jdbcjdbc Member
edited April 2015 in Suggestions & requests
I was developing a prototype of a top down car racing game and now I need to integrate objects collisions (without box2d because it is too complex). I was using TNT Collision engine and oBoxToObox function to check collision between car and objects due to the fact the car rotates, but it does not work.

Do you have any lua code to check collision between rectangle rotated?

You can download the source code to check it from here:
https://github.com/jdbcdev/topdown_racing

Comments

  • hgvyas123hgvyas123 Guru
    Accepted Answer
    try this code
    local rect1 = Bitmap.new(Texture.new("images/yellow.png"))
    rect1.width = rect1:getWidth()
    rect1.height = rect1:getHeight()
    rect1:setPosition(100, 100)
    rect1:setRotation(45)
    stage:addChild(rect1)
     
    local rect2 = Bitmap.new(Texture.new("images/blue_car.png"))
    rect2.width = rect2:getWidth()
    rect2.height = rect2:getHeight()
    rect2:setPosition(200, 200)
    stage:addChild(rect2)
     
    function onMouseDown(self, event)
    	if self:hitTestPoint(event.x, event.y) then
    		self.isFocus = true
     
    		self.x0 = event.x
    		self.y0 = event.y
     
    		event:stopPropagation()
    	end
    end
     
    function onMouseMove(self, event)
    	if self.isFocus then
    		local dx = event.x - self.x0
    		local dy = event.y - self.y0
     
    		self:setX(self:getX() + dx)
    		self:setY(self:getY() + dy)
     
    		self.x0 = event.x
    		self.y0 = event.y
     
    		event:stopPropagation()
    	end
    end
     
    function onMouseUp(self, event)
    	if self.isFocus then
    		self.isFocus = false
    		event:stopPropagation()
    	end
    end
     
    rect1:addEventListener(Event.MOUSE_DOWN, onMouseDown, rect1)
    rect1:addEventListener(Event.MOUSE_MOVE, onMouseMove, rect1)
    rect1:addEventListener(Event.MOUSE_UP, onMouseUp, rect1)
     
    rect2:addEventListener(Event.MOUSE_DOWN, onMouseDown, rect2)
    rect2:addEventListener(Event.MOUSE_MOVE, onMouseMove, rect2)
    rect2:addEventListener(Event.MOUSE_UP, onMouseUp, rect2)
     
    function checkForIntersection(rectangle1, rectangle2)
     
       local function chkForRotated(r1, r2)
          local x1 = r2:getX()-r1:getX()
          local y1 = r2:getY()-r1:getY()
     
          local x2 = x1*math.cos(math.rad(r1:getRotation())) + y1*math.sin(math.rad(r1:getRotation()))
          local y2 = y1*math.cos(math.rad(r1:getRotation())) - x1*math.sin(math.rad(r1:getRotation()))
     
          local angle1 = math.cos(math.rad(r2:getRotation() - r1:getRotation()))
          local angle2 = math.sin(math.rad(r2:getRotation() - r1:getRotation()))
          local arr1 = { r2.width*angle1, -r2.height*angle2, r2.width*angle1 - r2.height*angle2}
          local arr2 = { r2.width*angle2, r2.height*angle1, r2.width*angle2 + r2.height*angle1}
     
          local minX = 0 
          local maxX = 0
          local minY = 0
          local maxY = 0
          for i=1,3 do
                    if minX > arr1[i] then
                            minX = arr1[i]
                    end
                    if maxX <  arr1[i] then
                             maxX = arr1[i]
                    end
                    if minY > arr2[i] then
                            minY = arr2[i]
                    end
                    if maxY <  arr2[i] then
                             maxY = arr2[i]
                    end
          end
     
          return x2 + maxX < 0 or x2 + minX > r1.width or y2 + maxY < 0 or y2 + minY > r1.height
       end
       return not (chkForRotated(rectangle1,rectangle2) or chkForRotated(rectangle2,rectangle1))
    end
     
     function onEnterFrame(self, event)
        myvar = checkForIntersection(rect1, rect2)
        if myvar then
    		rect1:setColorTransform(0,0,0,1)
        else
    		rect1:setColorTransform(1,1,1,1)
    	end
     end
     
     stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)

    Likes: jdbc

    +1 -1 (+1 / -0 )Share on Facebook
  • jdbcjdbc Member
    Your source code works fine!!

    I had setAnchorPoint(0.5, 0.5), do you know how to change this code to work with this anchorpoint?
  • Yes 0.5,0.5 is a must for that type of game - you also need to account for scaling possibly?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • no i don't know about how to change this code for anchorpoint .5,.5 but how about to set anchor point to 0,0 at the time of checking collision and resetting it to .5,.5 again once collision event finished. i guess it will do the job
  • jdbcjdbc Member
    no i don't know about how to change this code for anchorpoint .5,.5 but how about to set anchor point to 0,0 at the time of checking collision and resetting it to .5,.5 again once collision event finished. i guess it will do the job
    I will try this. The anchorpoint must be 0.5, 0.5 for gameplay.
  • jdbcjdbc Member
    edited April 2015
    Finally the prototype is working with Box2d + XY scrolling to simply car movement. I have just used a array of tiles to generate the track.

    It is based on http://appcodingeasy.com/Gideros-Mobile/Gideros-Camera-Move



    Performance seems to be good on my BQ Android mobile.
  • Hey @jbdc, looks good, but you need drift! :)

    I did that in No Brakes by applying the force based on the rotation of an invisible sprite that had a slight slowness to it. It worked well, and a good substitute for "real" physics in a 0 gravity environment.

    Nic

    Likes: jdbc, pie

    My Gideros games: www.totebo.com
    +1 -1 (+2 / -0 )Share on Facebook
  • jdbcjdbc Member
    edited April 2015
    Hey @jbdc, looks good, but you need drift! :)

    I did that in No Brakes by applying the force based on the rotation of an invisible sprite that had a slight slowness to it. It worked well, and a good substitute for "real" physics in a 0 gravity environment.

    Nic
    You can download source code from https://github.com/jdbcdev/topdown_racing and try to implement it. Feel free to use it.

    Now I was trying that computer cars follow the right way.
  • jdbcjdbc Member
    edited April 2015
    I was reading this fantastic online book about Artificial Intelligence and Autonomous Agents to get a realistic movement of computer cars:

    http://natureofcode.com/book/chapter-6-autonomous-agents/

    In my case path following algorithm seems to be my solution.

    Likes: bali001

    +1 -1 (+1 / -0 )Share on Facebook
  • jdbcjdbc Member
    edited April 2015
    I was moving computer car using path following algorithm, but it is not working in the third curve. Any idea? I was using my function TrackScene:updateCars() to get it.
  • jdbcjdbc Member
    edited May 2015
    Using the Path Following algorithm to provide computer car IA (yellow one, the blue is controlled by me)

    +1 -1 (+3 / -0 )Share on Facebook
  • totebototebo Member
    Nice!
    My Gideros games: www.totebo.com
  • jdbcjdbc Member
    edited May 2015
    Next step is to combine Avoid Collision and Path Following algorithm to achieve a more realistic car behaviour:

    This is my entrypoint for Avoid Collision:
    http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-collision-avoidance--gamedev-7777

    I have tested with 4 cars and they are colliding at the beginning of the race.

    +1 -1 (+3 / -0 )Share on Facebook
Sign In or Register to comment.